JAVA集合框架02

public class ListTest {

    public static void main(String[] args) {
        //创建几个新闻对象
        News news1=new  News(1, "新闻1");
        News news2=new  News(2, "新闻2");
        News news3=new  News(3, "新闻3");
         //创建一个List集合
        List list=new ArrayList(); //现在 集合中存放的Object
         list.add(1);
         list.add("abc");
         list.add(news1);
        //遍历集合
         for (Object object : list) {
            System.out.println(object);
        }
         
         
         System.out.println("**************************");
        /*
         * 现在 我只想存放 News 新闻 
         * 泛型集合
         * 在创建集合的时候,就已经规定了 集合中允许存放的类型!
         * 如果后续进行新增操作时候,插入了类型不一致的数据,会编译报错!
         */
        List<News>  news=new ArrayList<News>();  //new ArrayList();
         //news.add(1);  编译报错
        //news.add(null);  可以存放 Null         
        news.add(news1);         
        news.add(news2);         
        news.add(news3);
        System.out.println("集合的大小:"+news.size());  //4
        //遍历
        for (News n : news) {
            System.out.println(n.getTitle());  //不需要强制类型转化 直接输出
        }
        System.out.println("***********************");
        
        
        //List <int> i=new ArrayList();  不能使用基本数据类型
        List<Integer> i=new  ArrayList();  //对应类型的封装类
        List<String> s=new  ArrayList();  //只能存放String类型
        
    }
}
复制代码

Map的泛型集合

创建Student实体类

复制代码
public class Student {
    private  Integer  studentNo;  //学生的编号  是唯一的
    private  String  name;
    private  Integer  age;
    private  String  sex;
    
    @Override
    public String toString() {
        return "Student [studentNo=" + studentNo + ", name=" + name + ", age="
                + age + ", sex=" + sex + "]";
    }
    public Student() {
        super();
    }
    public Student(Integer studentNo, String name, Integer age, String sex) {
        super();
        this.studentNo = studentNo;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    
    public Integer getStudentNo() {
        return studentNo;
    }
    public void setStudentNo(Integer studentNo) {
        this.studentNo = studentNo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
复制代码

创建测试类

复制代码
public class MapTest {
    public static void main(String[] args) {
        /*
         * 创建一个Map的泛型集合
         * 之前
         * Map   map=new HashMap();
         * key和value全都是 Object类型
         */
        Map   m=new HashMap();
        m.put(1, 2);
        m.put("1","2");
        m.put(1.0, 2.0);
        System.out.println("map集合的大小:"+m.size());  //3
        System.out.println("************************************");
        
        
        //创建几个新闻对象
        Student stu1=new Student(1, "奥黑驴1", 1000, "女");
        Student stu2=new Student(2, "奥黑驴2", 10000, "女");
        Student stu3=new Student(3, "奥黑驴3", 1000, "女");
        //泛型集合   key只能是Integer
        Map<Integer,Student> map=new HashMap<Integer, Student>();
        //map.put(1, 1); 默认装箱操作  int基本数据类型转换成Interge封装类
        map.put(stu1.getStudentNo(), stu1);
        map.put(stu2.getStudentNo(), stu2);
        map.put(stu3.getStudentNo(), stu3);
        //遍历之前获取keySet
        Set<Integer> keySet = map.keySet();
        //01. for加强
        for (Integer key : keySet) {
            //根据key取得对应的Student对象
            System.out.println(map.get(key));
        }
        System.out.println("***************************");
        //02. iterator
        Iterator<Integer> it = keySet.iterator();
        while (it.hasNext()) {
            /*Integer key=it.next();
            map.get(key);*/
            System.out.println(map.get(it.next()));
        }
        System.out.println("***************************");
        /*
         * 03. entrySet() 同时可以获取 key和value 
         *    之前都是先获取key 之后拿key取得value
         */
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry entry = (Entry) iterator.next();
            System.out.println("map的key===>"+entry.getKey());
            System.out.println("map的value===>"+entry.getValue());
        }
    }
}
原文地址:https://www.cnblogs.com/xiaobaizhang/p/7761436.html