Java集合list和map的用法

in 备忘 with 1 comment
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/*
 * list是对象集合,允许对象重复。map是键值对的集合,不允许key重复。
 * list的输入方法和输出方法
 * map的输入方法和输出方法
 * 利用迭代器将list里的值取出来
 *
 */
public class ListTest {
    public static void main(String[] args) {
        List<Object> list=new ArrayList<Object>();
        list.add("dd");
        list.add("aa");
        list.add("cc");
        for(Iterator<Object> iterator=list.iterator();iterator.hasNext();) {
            Object string=iterator.next();
            System.out.println(string);
        }
        Map<String, String> map0=new HashMap<String, String>();
        map0.put("name", "zhangsan");
        map0.put("***", "female");
        String nameString=map0.get("name");
        String ***String=map0.get("***");
        System.out.println(nameString "\n" ***String);


        List<Map<String, Object>> lists=new ArrayList<Map<String,Object>>();
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("name", "ddd");
        map.put("age", 23);
        Map<String, Object> map1=new HashMap<String, Object>();
        map1.put("name", "ccc");
        map1.put("age", 43);
        lists.add(map);
        lists.add(map1);

        Iterator<Map<String, Object>> it=lists.iterator();
        for(;it.hasNext();) {
            Map<String, Object> map2 =it.next();
            String name=(String) map2.get("name");
            Object age= map2.get("age");        
            System.out.println("name=" name "\nage=" age);
        }
    }
}
Comments are closed.