List集合三种方式遍历、Set集合(HashSet集合保证元素唯一性原理、TreeSet自然排序Comparable和比较器排序Comparator)

1.集合体系结构

  • 集合类的特点
    ​ 提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变

  • 集合类的体系图
    在这里插入图片描述

2.单列 Collection集合

2.1概述

Collection 是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素,
JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现。

2.2常用方法

方法名 说明
boolean add(E e) 添加元素
boolean remove(Object o) 从集合中移除指定的元素
void clear() 清空集合中的元素
boolean contains(Object o) 判断集合中是否存在指定的元素
boolean isEmpty() 判断集合是否为空
int size() 集合的长度,也就是集合中元素的个数
public class CollectionDemo01 {
   
    public static void main(String[] args) {
   
        //创建Collection集合的对象
        Collection<String> c = new ArrayList<String>();

        //添加元素:boolean add(E e)
        c.add("hello");
        c.add("world");
        c.add("java");
        //从集合中移除指定的元素
        c.remove("java");
		//清空集合中的元素
		//c.clear();
		//判断集合中是否存在指定的元素
		c.contains("world");
		//判断集合是否为空
		c.isEmpty();
		//集合的长度,也就是集合中元素的个数
		 System.out.println(c.size());
        //输出集合对象
        System.out.println(c);
    }
}

2.3遍历:迭代器

迭代器,集合的专用遍历方式
Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的

//创建集合对象
        Collection<String> c = new ArrayList<>();

        //添加元素
        c.add
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用Set集合管理学生对象的示例代码,分别使用了HashSetTreeSet来实现: ```java import java.util.*; public class Student { private String id; private String name; private int score1; private int score2; private int score3; // 构造方法 public Student(String id, String name, int score1, int score2, int score3) { this.id = id; this.name = name; this.score1 = score1; this.score2 = score2; this.score3 = score3; } // 计算总分 public int getTotalScore() { return score1 + score2 + score3; } // 获取学号 public String getId() { return id; } // 获取姓名 public String getName() { return name; } // 获取成绩1 public int getScore1() { return score1; } // 获取成绩2 public int getScore2() { return score2; } // 获取成绩3 public int getScore3() { return score3; } // 重写toString方法,用于输出学生对象的信息 @Override public String toString() { return id + " " + name + " " + score1 + " " + score2 + " " + score3; } } class Test { public static void main(String[] args) { // 使用HashSet来管理学生对象,以学号作为唯一标识 Set<Student> set1 = new HashSet<>(); set1.add(new Student("001", "张三", 80, 90, 70)); set1.add(new Student("002", "李四", 85, 88, 92)); set1.add(new Student("003", "王五", 75, 77, 80)); set1.add(new Student("001", "赵六", 90, 85, 88)); // 学号重复,将不会被添加到集合中 // 输出HashSet中的学生对象 System.out.println("使用HashSet管理学生对象:"); for (Student student : set1) { System.out.println(student); } // 使用TreeSet来管理学生对象,按总分(降序)+学号(升序)方式排序 Set<Student> set2 = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int result = s2.getTotalScore() - s1.getTotalScore(); // 按总分降序排序 if (result == 0) { // 总分相同,按学号升序排序 result = s1.getId().compareTo(s2.getId()); } return result; } }); set2.add(new Student("001", "张三", 80, 90, 70)); set2.add(new Student("002", "李四", 85, 88, 92)); set2.add(new Student("003", "王五", 75, 77, 80)); set2.add(new Student("004", "赵六", 90, 85, 88)); // 输出TreeSet中的学生对象 System.out.println("使用TreeSet管理学生对象:"); for (Student student : set2) { System.out.println(student); } } } ``` 运行结果如下: ``` 使用HashSet管理学生对象: 003 王五 75 77 80 001 张三 80 90 70 002 李四 85 88 92 使用TreeSet管理学生对象: 004 赵六 90 85 88 002 李四 85 88 92 001 张三 80 90 70 003 王五 75 77 80 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值