Java学习2023.7.17(集合框架和泛型)

本文详细介绍了Java中ArrayList、LinkedList、HashSet和HashMap的基本操作,包括添加元素、遍历集合、删除元素、检查元素存在性以及集合与数组的转换。还提到了泛型在集合中的应用,增强了代码的类型安全。
摘要由CSDN通过智能技术生成

目录

ArrayList

 1.添加元素

2.普通for循环遍历集合元素

3.增强for循环遍历集合元素

4.迭代器遍历集合元素

5.取出元素(删除)

6.查看是否含元素

7.集合转数组

LinkedList(多数与ArrayList相同)

1.添加元素

2.迭代器遍历

3.获取元素

4.删除元素

HashSet

1.添加元素

2.增强for循环遍历Set集合

3.迭代器遍历Set集合

4.多态引用

 HashMap

1.添加元素

2.获取值

3.移除、查询元素

4.获取Map集合中所有键值对中的键、值

5.增强for循环遍历Map集合

6.迭代器遍历Map集合

7.使用键值对的形式遍历Map集合

泛型


ArrayList

//第一步:准备好数据-》创建NewsTitle类对象

NewsTitle newsTitle1 = new NewsTitle(1001,"合肥今日暴雨","合肥气象局");
NewsTitle newsTitle2 = new NewsTitle(1002,"震惊,这个人居然干那种事情","张三");
NewsTitle newsTitle3 = new NewsTitle(1003,"微信中了丝袜病毒?省府办惊现比光腚照更咸湿聊天:穿丝袜等我","大风文字");

//数据准备好了,还需要准备一个容器:ArrayList集合
//第二步:创建ArrayList类对象

 1.添加元素

第三步:将数据存储到集合中
boolean add(E e):将指定的元素添加到此列表的尾部。

        arrayList.add(newsTitle1);
        arrayList.add(newsTitle2);
        arrayList.add(newsTitle3);

void add(int index, E element):将指定的元素插入此列表中的指定位置。

        NewsTitle newsTitle4 = new NewsTitle(1004,"有抑郁症的人,常说5句“口头禅”,若你身边有,要多留心","骨科邱医生");
        NewsTitle newsTitle5 = new NewsTitle(1005,"这家民营巨头,半年巨亏超42亿","中国基金报");
        arrayList.add(1,newsTitle4);

集合中长度用size()表示,例:arrayList.size();

2.普通for循环遍历集合元素

        for(int i =0;i<arrayList.size();i++){
            Object object =arrayList.get(i);
            NewsTitle newsTitle= (NewsTitle)object;
            System.out.println(newsTitle);
        }

3.增强for循环遍历集合元素

        for(Object object :arrayList){
            NewsTitle newsTitle= (NewsTitle)object;
            System.out.println(newsTitle);
        }

4.迭代器遍历集合元素

        迭代器也是一个容器,用来存储元素,将ArrayList集合中的元素取出来后,存入到迭代器容器中,然后通过迭代器调用相应方法获取元素

        通过循环的方式从迭代容器中取出元素,有元素就取出来,所以需要先使用hasNext()方法判断迭代器中有没有元素,如果有,使用next()方法获取里面的元素

        Iterator iterator=arrayList.iterator();
        while(iterator.hasNext()){
            //取出元素
            Object object =iterator.next();
            NewsTitle newsTitle = (NewsTitle) object;
            System.out.println(newsTitle);
        }

5.取出元素(删除)

 E remove(int index):移除此列表中指定位置上的元素。
boolean remove(Object o):移除此列表中首次出现的指定元素(如果存在)。

        Object object= arrayList.remove(1);
        System.out.println("你移除的元素是:"+object);
        boolean result3 = arrayList.remove(newsTitle2);
        System.out.println("newsTitle2移除成功:"+result3);

void clear():移除此列表中的所有元素。

6.查看是否含元素

boolean contains(Object o):如果此列表中包含指定的元素,则返回 true。

        boolean result1 =arrayList.contains(newsTitle4);
        System.out.println("集合中包含newsTitle4对象:"+result1);

        boolean result2 =arrayList.contains(newsTitle5);
        System.out.println("集合中包含newsTitle5对象:"+result2);

boolean isEmpty()如果此列表中没有元素,则返回 true

        System.out.println("集合中元素为空:"+arrayList.isEmpty());
//        void clear():移除此列表中的所有元素。
        arrayList.clear();
        System.out.println("集合中元素为空:"+arrayList.isEmpty());
        System.out.println("集合中元素个数:"+arrayList.size());

7.集合转数组

 Object[] toArray() 按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。

        Object[] objs =arrayList.toArray();
        for (Object obj :objs){
           NewsTitle newsTitle = (NewsTitle)obj;
            System.out.println(newsTitle);
        }
        System.out.println("----------------------------------------------------");
        System.out.println(Arrays.toString(objs));

LinkedList(多数与ArrayList相同)

1.添加元素

        linkedList.add(newsTitle1);
        linkedList.add(newsTitle2);
        linkedList.add(newsTitle3);
        linkedList.add(newsTitle2);

指定第一个和最后一个位置的添加

2.迭代器遍历

        Iterator iterator  =linkedList.iterator();
        while(iterator.hasNext()){
            Object object =iterator.next();
            NewsTitle newsTitle = (NewsTitle) object;
            System.out.println(newsTitle);

        for循环也可

3.获取元素

获取第一个和最后一个元素有多种写法

4.删除元素

首位和最后一位删除有其余方法

HashSet

 准备数据

        Student student1 = new Student("张三",25);
        Student student2 = new Student("李四",23);
        Student student3 = new Student("王五",24);
        Student student4 = new Student("赵六",27);

准备容器:使用HashSet集合存储元素

1.添加元素

        hashSet.add(student1);
        hashSet.add(student2);
        hashSet.add(student3);

再次添加元素,输出长度,相同元素再次添加不增加该集合长度

        hashSet.add(student2);
        System.out.println("集合中元素个数:"+hashSet.size());
        hashSet.add(student4);
        System.out.println("集合中元素个数:"+hashSet.size());

     Set集合中的元素是无序的,所以没有下标值类标识元素的位置,也就没有get()方法来获取元素

2.增强for循环遍历Set集合

        for(Object object :hashSet){
            Student student =(Student) object;
            System.out.println(student);
        }

        通过遍历查询出来的结果,可以发现取出元素的顺序与存入元素的顺序是不一样的,由此说明,Set集合是无序的

3.迭代器遍历Set集合

        Iterator iterator =hashSet.iterator();
        while(iterator.hasNext()){
            //取出来
            Object object =iterator.next();
            Student student =(Student) object;
            System.out.println(student);
        }

4.多态引用

注意结果:第一个输出2,第二个输出1

 //多态:接口的引用指向可一个实现类的对象(向上转型)
        Set set=new HashSet();
        String s1=new String("java");
        String s2=s1;
        String s3=new String("JAVA");
        set.add(s1);
        set.add(s2);
        set.add(s3);
        System.out.println(set.size());//2
        Set set=new HashSet();
        String s1=new String("java");
        String s2=s1;
        String s3=new String ("java");
        set.add(s1);
        set.add(s2);
        set.add(s3);
        System.out.println(set.size());//1

 HashMap

准备存储数据的容器:创建HashMap对象

HashMap hashMap = new HashMap();

1.添加元素

        hashMap.put("CN","中华人民共和国");

        String key = "JP";
        String value = "日本国";

        hashMap.put(key,value);
        hashMap.put("RU","俄罗斯联邦");
        hashMap.put("UK","大不列颠及北爱尔兰联合王国");
        hashMap.put("US","美利坚合众国");

2.获取值

获取元素:通过键来获取值

        String str1 =(String )hashMap.get("CN");
        System.out.println("CN对应的国家中文全称:"+str1);

3.移除、查询元素

移除元素

 查看集合中是否包含指定的元素

        boolean result1 = hashMap.containsValue("日本国");
        System.out.println("集合中包含日本国:"+result1);
        boolean result2 = hashMap.containsKey("JP");
        System.out.println("集合中包含JP这个键:"+result2);

4.获取Map集合中所有键值对中的键、值

获取键

Set<K> keySet()返回此映射中所包含的键的 Set 视图。

         Set keys= hashMap.keySet();
         for(Object object :keys){
             String key2 =(String)object;
             System.out.println(key2);
         }

获取值

Collection<V> values():返回此映射所包含的值的 Collection 视图。

        Collection values =hashMap.values();
        for(Object object :values){
            String value2 = (String)object;
            System.out.println(value2);
        }

5.增强for循环遍历Map集合

 

 遍历Map集合,可以先获取所有的键,然后通过get(键)方法获取值

Set keys  =hashMap.keySet();
        for(Object object :keys){
            String key = (String) object;
            //使用key取获取值
            Object obj =hashMap.get(key);
            //将值转换成正的类型
            Student student = (Student) obj;
            System.out.println(key+"-"+student);
        }

6.迭代器遍历Map集合

        Iterator iterator =keys.iterator();
        while(iterator.hasNext()){
            Object object =iterator.next();
            //将键转换为真正的类型String
            String key = (String) object;
            //根据键获取值
            Object object2 =hashMap.get(key);
            //将值转换成真正的类型
            Student student = (Student) object2;
            System.out.println(key+"-"+student);
        }

7.使用键值对的形式遍历Map集合

调用方法将hashMap集合中得元素存储到Set集合中

Set keyValues =hashMap.entrySet();

从kayValues集合中取出一个一个的键值对

        for(Object object :keyValues){
            //键值对实际类型是Map.Entry
            Map.Entry me =(Map.Entry)object;
            //键值对的真正类型获取以后,可以调用Map.Entry里的方法获取键值对的键和值
//            K getKey():返回与此项对应的键。
            Object obj11=me.getKey();
            String key = (String)obj11;
//            V getValue():返回与此项对应的值
            Object obj22 =me.getValue();
            Student student = (Student) obj22;
            System.out.println(key+"-"+student);
        }

泛型

加<>号,在里面写入数据类型,让接收的数据不再转成Object类,而是转换成指定的类

例:

ArrayList<NewsTitle> arrayList= new ArrayList<NewsTitle>();
        arrayList.add(newsTitle1);
        arrayList.add(newsTitle2);
        arrayList.add(newsTitle3);
        for(int i =0;i<arrayList.size();i++){
            //NewsTitle newsTitle=arrayList.get(i);
            //System.out.println(newsTitle);
            System.out.println(arrayList.get(i));
        }
        //增强for循环
        for(NewsTitle newsTitle :arrayList){
            System.out.println(newsTitle);
        }

这里传入的数据就是NewsTitle类型,不用再永Object类接收,可以直接用NewsTitle对象接收

注:

 转换成数组toArray();依旧要用Object类对象接收!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值