• 【JavaSE】集合专项练习篇(附源码)


    💁 个人主页:黄小黄的博客主页
    ❤️ 支持我:👍 点赞 🌷 收藏 🤘关注
    🎏 格言:All miracles start from sometime somewhere, make it right now.
    本文来自专栏:JavaSE从入门到精通
    本文题目摘自: 韩顺平 Java P548 ~ P552
    在这里插入图片描述


    1 CollectionTest01.java

    按照要求实现:
    (1)封装一个新闻类,包含标题和内容属性,提供get、set方法,重写toString方法,并打印对象时只打印标题;
    (2)只提供一个带参数的构造器,实例化对象时,只初始化标题,并且实例化两个对象:
      新闻一:新冠确诊病例超千万,数百万印度教徒赴恒河“圣浴”
    引民众担忧;
      新闻二:男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生;
    (3)将新闻对象添加到ArrayList集合中,并进行倒序遍历;
    (4)在遍历集合的过程中,将新闻标题进行处理,超过15字的只保留前15个字,然后加上"…";
    (5)在控制台打印结果。

    🐱 参考代码及运行结果:

    import java.util.ArrayList;
    
    /**
     * @author 兴趣使然黄小黄
     * @version 1.0
     */
    public class CollectionTest01 {
        public static void main(String[] args) {
            ArrayList arrayList = new ArrayList();
            arrayList.add(new News("新冠确诊病例超千万,数百万印度教徒赴恒河\"圣浴\"引民众担忧"));
            arrayList.add(new News("男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生"));
    
            // 倒叙遍历
            int size = arrayList.size();
            for (int i = size - 1; i >= 0; i--) {
                News news = (News) arrayList.get(i);
                System.out.println(processTitle(news.getTitle()));
            }
        }
    
        // 处理新闻标题
        public static String processTitle(String title) {
            if (title == null) {
                return "";
            }
            if (title.length() > 15) {
                return title.substring(0, 15);  // [0, 15)
            }
            return title;
        }
    }
    
    class News {
        private String title;
        private String content;
    
        public News(String title) {
            this.title = title;
        }
    
        public String getTitle() {
            return title;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        @Override
        public String toString() {
            return "News{" +
                    "title='" + title + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    在这里插入图片描述


    2 CollectionTest02.java

    使用ArrayList完成对 对象 Car{name,price}的各种操作
    (1)add:添加单个元素;
    (2)remove:删除指定元素;
    (3)contains:查找元素是否存在;
    (4)size:获取元素的个数;
    (5)isEmpty:判断是否为空;
    (6)clear:清空;
    (7)addAll:添加多个元素;
    (8)containsAll:查找多个元素是否都存在;
    (9)removeAll:删除多个元素;
    使用增强for和迭代器来遍历所有的car,需要重写Car的toString方法。

    🐰 参考代码及运行结果:

    import java.util.ArrayList;
    import java.util.Iterator;
    
    /**
     * @author 兴趣使然黄小黄
     * @version 1.0
     */
    public class CollectionTest02 {
        public static void main(String[] args) {
            ArrayList arrayList = new ArrayList();
            Car car1 = new Car("宝马", 400000);
            Car car2 = new Car("宾利", 5000000);
    
            // 添加
            arrayList.add(car1);
            arrayList.add(car2);
            System.out.println("add:" + arrayList);
            // 删除
            arrayList.remove(car1);
            System.out.println("remove:" + arrayList);
            // 判断
            System.out.println(arrayList.contains(car2));
            System.out.println(arrayList.isEmpty());
            // 元素个数
            System.out.println(arrayList.size());
            // 清空
            arrayList.clear();
            System.out.println(arrayList);
            // 添加多个
            arrayList.add(car1);
            arrayList.add(car2);
            arrayList.addAll(arrayList);
            System.out.println(arrayList);
            // 查找多个是否存在
            System.out.println(arrayList.containsAll(arrayList));
    
            for (Object o :
                    arrayList) {
                System.out.println(o);
            }
    
            Iterator iterator = arrayList.iterator();
            while (iterator.hasNext()) {
                Object next = iterator.next();
                System.out.println(next);
            }
        }
    }
    
    class Car {
        private String name;
        private int price;
    
        public Car(String name, int price) {
            this.name = name;
            this.price = price;
        }
    
        @Override
        public String toString() {
            return "Car{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    '}';
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setPrice(int price) {
            this.price = price;
        }
    
        public int getPrice() {
            return price;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    在这里插入图片描述


    3 CollectionTest03.java

    按照要求完成下列任务
    (1)使用HashMap类实例化一个Map类型的对象m,键(String)和值(int)分别用于存储员工的姓名和工资,存入数据如下:
    jack-650元;tom-1200元;smith-2900元;
    (2)将jack的工资更改为2600元;
    (3)为所有员工工资加薪100元;
    (4)遍历所有员工;
    (5)遍历所有工资。

    🦁 参考代码及运行结果:

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * @author 兴趣使然黄小黄
     * @version 1.0
     */
    public class Collection03 {
        public static void main(String[] args) {
            Map map = new HashMap();
            map.put("jack", 650);  // int - Integer
            map.put("tom", 1200);
            map.put("smith", 2900);
            System.out.println("初始数据: " + map);
    
            // 更新jack工资
            map.put("jack", 2600);
            System.out.println("jack工资更新: " + map);
    
            // 所有员工加薪100元
            Set keySet = map.keySet();
            for (Object key :
                    keySet) {
                // 更新
                map.put(key, (Integer)map.get(key) + 100);
            }
            System.out.println("所有员工加薪100元: " + map);
    
            // 遍历员工与工资
            Set entrySet = map.entrySet();
            Iterator iterator = entrySet.iterator();
            while (iterator.hasNext()){
                Map.Entry entry = (Map.Entry)iterator.next();
                System.out.println(entry.getKey() + "-" + entry.getValue());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    在这里插入图片描述


    4 简答题

    4.1 试分析 HashSet 与 TreeSet 分别如何实现去重的?

    ❓ 试分析 HashSet 与 TreeSet 分别如何实现去重的?

    1. HashSet的去重机制: hashCode() + equals(),底层先通过存入对象,进行运算得到一个 hash值,通过 hash值得到对应的索引,如果发现table索引所在位置,没有数据,则直接存放;如果有数据,则进行equals比较(由程序员决定),遍历该链表,如果遍历比较后都不相同,则加入,否则不加入。
    2. TreeSet的去重机制: 如果传入了一个 Comparator匿名对象,则使用实现的 compare去重,如果方法返回0,则认为是相同数据,则不添加。如果没有传入 Comparator匿名对象,则以添加的对象实现的 Compareable 接口的 compareTo 去重。

    5 代码分析题

    5.1 下面的代码是否会抛出异常,并从源码层面说明原因?

    TreeSet treeSet = new TreeSet();
    treeSet.add(new Person());
    
    • 1
    • 2

    答: 会。因为 TreeSet() 构造器没有传入 Comparator接口的匿名内部类,所以在底层会把 Person 对象 转成 Comparable 类型,但是,Person并没有实现该接口,所以会报异常:ClassCastException。

    5.2 Person类按照id和name重写了hashCode和equals方法,问下面代码输出什么?(容易踩坑)

    import java.util.HashSet;
    import java.util.Objects;
    
    /**
     * @author 兴趣使然黄小黄
     * @version 1.0
     */
    public class MyTest {
        public static void main(String[] args) {
            HashSet set = new HashSet();
            Person p1 = new Person(1001, "AA");
            Person p2 = new Person(1002, "BB");
            set.add(p1);
            set.add(p2);
            p1.name = "CC";
            set.remove(p1);
            System.out.println(set);
            set.add(new Person(1001, "CC"));
            System.out.println(set);
            set.add(new Person(1001, "AA"));
            System.out.println(set);
        }
    }
    
    class Person{
        public int id;
        public String name;
    
        public Person(int id, String name) {
            this.id = id;
            this.name = name;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Person)) return false;
            Person person = (Person) o;
            return id == person.id && Objects.equals(name, person.name);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(id, name);
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    🦁 注意陷阱!

    在添加第一个 Person{1001, “AA”}时,hash值已经确定,而当修改AA为CC时,并不会更改hash值,而后,通过remove删除时,找不到原来的AA位置,所以导致删除失败!
    在添加CC时,重新计算了hash值,和AA修改CC的索引并不相同,因此可以成功加入;
    在最后添加AA时,计算出索引位置为第一个 Person的位置,但是其内容已经为CC,因此,能够以链表的形式接在其后面,也能添加成功!

    在这里插入图片描述


    写在最后

    🌟以上便是本文的全部内容啦,后续内容将会持续免费更新,如果文章对你有所帮助,麻烦动动小手点个赞 + 关注,非常感谢 ❤️ ❤️ ❤️ !
    如果有问题,欢迎私信或者评论区!
    在这里插入图片描述

    共勉:“你间歇性的努力和蒙混过日子,都是对之前努力的清零。”
    在这里插入图片描述

  • 相关阅读:
    消灭对账,提升资金周转率,实现购销双方共赢
    2023年成都.NET线下技术沙龙活动即将到来!
    C++:vector容器是否包含给定元素
    node.js--模块化
    CSS 滚动驱动动画 scroll()
    MyBatis 面试题(八)
    Feign 实现 GET 方法传递 POJO
    Mysql高级语句(视图表 、存储过程、条件语句、循环语句)
    Rust入门基础
    Windows 10 docker 容器添加新端口映射的方法与步骤
  • 原文地址:https://blog.csdn.net/m0_60353039/article/details/126472348