Java集合--Collection集合

【Java集合 1】Java集合,Collection接口,Iterator接口 全网最细Java零基础手把手入门教程,系列课程包括:基础篇、集合篇、Java8新特性、多线程、代码实战,持续更新中(每周1-2篇),适合零基础和进阶提升的同学。 阅读详情


集合: 对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能。

  • 和数组的区别
    • 数组长度固定,集合长度不固定
    • 数组可以存储基本类型和引用类型,集合只能存储引用类型

Collection集合

Collection集合由List和Set组成。Collection,List,Set都是接口,具体方法由ArrayList,HashSet等实现类实现。
Collection集合的构成

Collection接口:

常用方法:

在这里插入图片描述

案例

使用ArrayList实现Colleciton接口,存储String类型数据

public class testCollectionSet {
    public static void main(String[] args) {
        //创建集合
        Collection collection=new ArrayList();
        addElement(collection);
        traverseElement(collection);
        judgeElement(collection);
        deleteElement(collection);

    }

    //判断
    private static void judgeElement(Collection collection) {
        System.out.println(collection.contains("西瓜"));
        System.out.println(collection.isEmpty());
        System.out.println("----------判断元素结束-------------");

    }

    //删除元素
    private static void deleteElement(Collection collection) {
        collection.remove("香蕉");
        System.out.println(collection);
        collection.clear();
        System.out.println("clear后元素个数:"+collection.size());
        System.out.println("----------删除元素结束-------------");

    }

    //遍历元素
    private static void traverseElement(Collection collection) {

        //增强for
        /*for (Object object:collection) {
            System.out.println(object);
        }*/
        //使用迭代器
        Iterator iterator=collection.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
            //使用迭代器删除集合中的元素
            iterator.remove();
        }
        System.out.println(collection.size());
        System.out.println("----------遍历元素结束-------------");

    }

    //添加元素
    private static void addElement(Collection collection) {
        collection.add("西瓜");
        collection.add("火龙果");
        collection.add("香蕉");
        System.out.println(collection);
        System.out.println("元素个数:"+collection.size());
        System.out.println("----------添加元素结束-------------");
    }
}

使用ArrayList实现Colleciton接口,存储自定义类对象

Student类(后面的测试实现类所使用的都是该类)

public class Student{
	private String name;
    private int age;
    public Student(){}

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    //alt+insert自动重写toString方法
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    //先按姓名比较再按年龄比较
    @Override
    public int compareTo(Student o) {
        int n1=this.getName().compareTo(o.getName());
        int n2=this.age-o.age;
        return n1==0?n2:n1;
    }
}

实现方法

public class testCollectionSetForStudent {
    public static void main(String[] args) {
        Collection collection=new ArrayList();
        Student s1=new Student("木头人",18);
        Student s2=new Student("java",20);
        Student s3=new Student("python",21);
        addElement(collection, s1, s2, s3);
        traverseElement(collection);
        judgeElement(collection, s1);
        deleteElement(collection, s3);
    }

    //删除
    private static void deleteElement(Collection collection, Student s3) {
        System.out.println("-------------删除----------");
        collection.remove(s3);
        System.out.println(collection);
        System.out.println("元素个数:"+collection.size());
        collection.clear();
        System.out.println("clear后元素个数:"+collection.size());
    }

    //判断
    private static void judgeElement(Collection collection, Student s1) {
        System.out.println("----------判断-------------");
        System.out.println(collection.contains(s1));
        System.out.println(collection.isEmpty());
    }

    //遍历
    private static void traverseElement(Collection collection) {
        System.out.println("-------------遍历----------");
        //增强for
        /*for (Object object:collection) {
            Student s=(Student) object;
            System.out.println(s.toString());
        }*/
        //迭代器
        Iterator iterator=collection.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
//            iterator.remove();
        }
    }

    //添加
    private static void addElement(Collection collection, Student s1, Student s2, Student s3) {
        System.out.println("----------添加-----------");
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        //List接口实现类,可重复添加元素
        collection.add(s3);
        System.out.println(collection);
        System.out.println("元素个数:"+collection.size());
    }
}

代码执行结果:

在这里插入图片描述

List接口

常用方法


特点: 有序,有下标,元素可重复。
实现类: ArrayList,LinkList,Vector(不常用,了解即可)

案例

List接口添加基本数据类型时会包含一个自动装箱(包装类)的操作。

public class testListInterface {
    public static void main(String[] args) {
        //创建集合
        List list=new ArrayList();
        addElement(list);
//        traverseElement(list);

        judgeElement(list);

        //subList
        System.out.println("----------子列表-------------");
        List subList=list.subList(0,2);
        System.out.println(subList);

        deleteElement(list);
        //获取元素位置
        System.out.println("----------获取元素位置-------------");
        System.out.println(list.indexOf("火龙果"));

    }

    //判断
    private static void judgeElement(List list) {
        System.out.println("----------判断-------------");
        System.out.println(list.contains("西瓜"));
        System.out.println(list.contains(20));
        System.out.println(list.isEmpty());

    }

    //删除元素
    private static void deleteElement(List list) {
        System.out.println("----------删除-------------");
        list.remove("香蕉");
        list.remove(0);
        //删除基本类型元素时,需要类型转换
//        list.remove(20);会下标越界
//        list.remove((Object) 20);
        list.remove(new Integer(20));
        System.out.println(list);
        list.clear();
        System.out.println("clear后元素个数:"+list.size());

    }

    //遍历元素
    private static void traverseElement(List list) {
        System.out.println("----------遍历-------------");
        //增强for
        System.out.println("------增强for------");
        for (Object object:list) {
            System.out.println(object);
        }
        //for循环
        System.out.println("------for循环------");
        for (int i=0;i<list.size();i++) {
            System.out.println(list.get(i));
        }
        //使用迭代器
        System.out.println("------迭代器------");
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
            //使用迭代器删除集合中的元素
//            iterator.remove();
        }
        System.out.println(list.size());
        //使用列表迭代器
        System.out.println("------列表迭代器------");
        //ListIterator可以从前或者从后开始遍历,添加,删除,修改元素
        ListIterator listIterator=list.listIterator();
        System.out.println("------从前往后------");
        while (listIterator.hasNext()){
            System.out.println(listIterator.nextIndex()+":"+listIterator.next());
        }
        System.out.println("------从后往前------");
        while (listIterator.hasPrevious()){
            System.out.println(listIterator.previousIndex()+":"+listIterator.previous());
        }



    }

    //添加元素
    private static void addElement(List list) {
        System.out.println("----------添加-------------");
        list.add("西瓜");
        list.add("火龙果");
        list.add(20);
        list.add(0,"香蕉");
        System.out.println(list);
        System.out.println("元素个数:"+list.size());
    }
}

代码执行结果
在这里插入图片描述

ArrayList

特点
  • 数组结构实现,需要开辟连续的空间,查询快,增删慢
  • jdk1.2版本引入,运行效率快,线程不安全
  • 默认容量大小:DEFAULT_CAPACITY=10
    • 没有添加元素时容量为0,size为0,添加元素以后,容量变为10,size等于实际添加的元素个数
  • 存放元素的数组:Object[] elementData
案例
public class testArrayList {
    public static void main(String[] args) {
        ArrayList arrayList=new ArrayList();
        Student s1=new Student("木头人",18);
        Student s2=new Student("张学友",20);
        Student s3=new Student("周杰伦",21);

        //增加元素
        addElement(arrayList, s1, s2, s3);

        //遍历元素
        traverseElement(arrayList);

        //判断元素
        judgeElement(arrayList, s1);

        //查找元素
        System.out.println("-------------查找----------");
        System.out.println(arrayList.indexOf(new Student("张学友",20)));
        //删除元素
//        deleteElement(arrayList, s3);

    }
    //删除
    private static void deleteElement(ArrayList arrayList, Student s3) {
        System.out.println("-------------删除----------");
        arrayList.remove(s3);
        System.out.println(arrayList);
        System.out.println("元素个数:"+arrayList.size());
        arrayList.clear();
        System.out.println("clear后元素个数:"+arrayList.size());
    }

    //判断
    private static void judgeElement(ArrayList arrayList, Student s1) {
        System.out.println("----------判断-------------");
        System.out.println(arrayList.contains(s1));
        System.out.println(arrayList.isEmpty());
    }

    //遍历
    private static void traverseElement(ArrayList arrayList) {
        System.out.println("-------------遍历----------");
        //增强for
        /*for (Object object:arrayList) {
            Student s=(Student) object;
            System.out.println(s.toString());
        }*/
        //迭代器
        System.out.println("------迭代器--------");
        Iterator iterator=arrayList.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
//            iterator.remove();
        }
        //使用列表迭代器
        System.out.println("------列表迭代器--------");
        //ListIterator可以从前或者从后开始遍历,添加,删除,修改元素
        ListIterator listIterator=arrayList.listIterator();
        System.out.println("------从前往后------");
        while (listIterator.hasNext()){
            System.out.println(listIterator.nextIndex()+":"+listIterator.next());
        }
        System.out.println("------从后往前------");
        while (listIterator.hasPrevious()){
            System.out.println(listIterator.previousIndex()+":"+listIterator.previous());
        }
    }

    //添加
    private static void addElement(ArrayList arrayList, Student s1, Student s2, Student s3) {
        System.out.println("----------添加-----------");
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        //List接口实现类,可重复添加元素
        arrayList.add(s3);
        System.out.println(arrayList);
        System.out.println("元素个数:"+arrayList.size());
    }

}

代码执行结果
在这里插入图片描述

LinkList

特点
  • 链表结构实现,不需要连续的空间,增删快,查询慢
案例
public class testLinkList {
    public static void main(String[] args) {
        LinkedList linkedList=new LinkedList();
        Student s1=new Student("木头人",18);
        Student s2=new Student("张学友",20);
        Student s3=new Student("周杰伦",21);
        //增加元素
        addElement(linkedList, s1, s2, s3);

        //遍历元素
        traverseElement(linkedList);

        //判断元素
        judgeElement(linkedList, s1);

        //查找元素
        System.out.println("-------------查找----------");
        //如果想要使用这中方法进行查找或者删除的话,需要重写Student的equals方法
        System.out.println(linkedList.indexOf(new Student("张学友",20)));
        //删除元素
        deleteElement(linkedList, s3);

    }
    //删除
    private static void deleteElement(LinkedList linkedList, Student s3) {
        System.out.println("-------------删除----------");
        linkedList.remove(s3);
        System.out.println(linkedList);
        System.out.println("元素个数:"+linkedList.size());
        linkedList.clear();
        System.out.println("clear后元素个数:"+linkedList.size());
    }

    //判断
    private static void judgeElement(LinkedList linkedList, Student s1) {
        System.out.println("----------判断-------------");
        System.out.println(linkedList.contains(s1));
        System.out.println(linkedList.isEmpty());
    }

    //遍历
    private static void traverseElement(LinkedList linkedList) {
        System.out.println("-------------遍历----------");
        //增强for
//        System.out.println("------增强for-------");
//        for (Object object:linkedList) {
//            System.out.println(object);
//        }
        //for循环
        System.out.println("------for循环------");
        for (int i=0;i<linkedList.size();i++) {
            System.out.println(linkedList.get(i));
        }
        //迭代器
        System.out.println("------迭代器---------");
        Iterator it=linkedList.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
//            iterator.remove();
        }
        //使用列表迭代器
        System.out.println("------列表迭代器--------");
        //ListIterator可以从前或者从后开始遍历,添加,删除,修改元素
        ListIterator listIterator=linkedList.listIterator();
        System.out.println("------从前往后------");
        while (listIterator.hasNext()){
            System.out.println(listIterator.nextIndex()+":"+listIterator.next());
        }
//        System.out.println("------从后往前------");
//        while (listIterator.hasPrevious()){
//            System.out.println(listIterator.previousIndex()+":"+listIterator.previous());
//        }
        }
    }

    //添加
    private static void addElement(LinkedList linkedList, Student s1, Student s2, Student s3) {
        System.out.println("----------添加-----------");
        linkedList.add(s1);
        linkedList.add(s2);
        linkedList.add(s3);
        //List接口实现类,可重复添加元素
        linkedList.add(s3);
        System.out.println(linkedList);
        System.out.println("元素个数:"+linkedList.size());
    }
}

代码执行结果
在这里插入图片描述

Vector

特点
  • 数组结构实现,查询快,增删慢
  • jdk1.0版本引入,运行效率慢,线程安全
案例
public class testVector {
    public static void main(String[] args) {
        Vector vector=new Vector();
        Student s1=new Student("木头人",18);
        Student s2=new Student("张学友",20);
        Student s3=new Student("周杰伦",21);

        //增加元素
        addElement(vector, s1, s2, s3);

        //遍历元素
        traverseElement(vector);

        //判断元素
        judgeElement(vector, s1);
//
        //查找元素
        System.out.println("-------------查找----------");
        System.out.println(vector.indexOf(new Student("张学友",20)));
        //删除元素
        deleteElement(vector, s3);

    }
    //删除
    private static void deleteElement(Vector vector, Student s3) {
        System.out.println("-------------删除----------");
        vector.remove(s3);
        System.out.println(vector);
        System.out.println("元素个数:"+vector.size());
        vector.clear();
        System.out.println("clear后元素个数:"+vector.size());
    }

    //判断
    private static void judgeElement(Vector vector, Student s1) {
        System.out.println("----------判断-------------");
        System.out.println(vector.contains(s1));
        System.out.println(vector.isEmpty());
    }

    //遍历
    private static void traverseElement(Vector vector) {
        System.out.println("-------------遍历----------");
        //增强for
        /*for (Object object:vector) {
            Student s=(Student) object;
            System.out.println(s.toString());
        }*/
        //迭代器
        System.out.println("------迭代器--------");
        Iterator iterator=vector.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
//            iterator.remove();
        }
        //使用列表迭代器
        System.out.println("------列表迭代器--------");
        //ListIterator可以从前或者从后开始遍历,添加,删除,修改元素
        ListIterator listIterator=vector.listIterator();
        System.out.println("------从前往后------");
        while (listIterator.hasNext()){
            System.out.println(listIterator.nextIndex()+":"+listIterator.next());
        }
//        System.out.println("------从后往前------");
//        while (listIterator.hasPrevious()){
//            System.out.println(listIterator.previousIndex()+":"+listIterator.previous());
//        }
        }
        System.out.println("------枚举器--------");
        Enumeration en=vector.elements();
        while (en.hasMoreElements()){
            System.out.println(en.nextElement());
        }

    }

    //添加
    private static void addElement(Vector vector, Student s1, Student s2, Student s3) {
        System.out.println("----------添加-----------");
        vector.add(s1);
        vector.add(s2);
        vector.add(s3);
        //List接口实现类,可重复添加元素
        vector.add(s3);
        System.out.println(vector);
        System.out.println("元素个数:"+vector.size());
    }
}

代码执行结果
在这里插入图片描述

Set接口

特点: 无序,无下标,元素不可重复。
实现类: HashSet,SortedSet,TreeSet

案例

public class testSet {
    public static void main(String[] args) {
        Set<String> set=new HashSet<>();
        addElement(set);
        traverseElement(set);
        judgeElement(set);
        deleteElement(set);
    }

    private static void judgeElement(Set<String> set) {
        System.out.println("----------判断-------------");
        System.out.println(set.contains("李四"));
        System.out.println(set.contains("木头人"));
        System.out.println(set.isEmpty());
    }


    //遍历元素
    private static void traverseElement(Set<String> set) {
        System.out.println("----------遍历-------------");
        //无序没有下标,所以不能使用常规for循环遍历
        //增强for
        System.out.println("------增强for------");
        for (String string:set) {
            System.out.println(string);
        }
        //使用迭代器
        System.out.println("------迭代器------");
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
            //使用迭代器删除集合中的元素
//            iterator.remove();
        }
        System.out.println(set.size());
    }

    private static void deleteElement(Set<String> set) {
        set.remove("张三");
        System.out.println(set);
        //清空
        set.clear();
        System.out.println(set);
    }

    private static void addElement(Set<String> set) {
        set.add("张三");
        set.add("李四");
        set.add("王五");
        set.add("张三");
        //[李四, 张三, 王五]      无序
        System.out.println(set);
        //元素个数:3            不可重复
        System.out.println("元素个数:"+set.size());
    }
}

代码执行结果
在这里插入图片描述

HashSet

特点
  • 基于HashCode计算元素存放位置,存入元素哈希码相同时,通过equals确认元素是否相同,相同则拒绝后者进入
  • 存储结构:哈希表(数组+链表+红黑树(红黑树是一种含有红黑结点并能自平衡的二叉查找树。))
  • 存储过程:
    (1)根据hashcode计算存储位置,为空,直接存入,不为空执行第二步
    (2)执行equals方法,如果为true,元素重复,不存入;否则,形成链表
案例

HashSet存储字符串信息

public class testHashSet {
    public static void main(String[] args) {
        HashSet<String> set=new HashSet<>();
        addElement(set);
        traverseElement(set);
        judgeElement(set);
        deleteElement(set);
    }

    private static void judgeElement(Set<String> set) {
        System.out.println("----------判断-------------");
        System.out.println(set.contains("李四"));
        System.out.println(set.contains("木头人"));
        System.out.println(set.isEmpty());
    }

    //遍历元素
    private static void traverseElement(Set<String> set) {
        System.out.println("----------遍历-------------");
        //增强for
        System.out.println("------增强for------");
        for (String string:set) {
            System.out.println(string);
        }
        //使用迭代器
        System.out.println("------迭代器------");
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
            //使用迭代器删除集合中的元素
//            iterator.remove();
        }
        System.out.println(set.size());
    }

    private static void deleteElement(Set<String> set) {
        System.out.println("------删除------");
        set.remove("张三");
        System.out.println(set);
        //清空
        set.clear();
        System.out.println(set);
    }

    private static void addElement(Set<String> set) {
        set.add("张三");
        set.add("李四");
        set.add("王五");
        set.add("张三");
        //[李四, 张三, 王五]      无序
        System.out.println(set);
        //元素个数:3            不可重复
        System.out.println("元素个数:"+set.size());
    }
}

代码执行结果
在这里插入图片描述
HashSet存储自定义类对象

public class testHashSetForStudent {
    public static void main(String[] args) {
        HashSet<Student> hashSet=new HashSet<>();
        Student s1=new Student("小明",18);
        Student s2=new Student("小红",20);
        Student s3=new Student("小华",19);
        Student s4=s1;

        addStudent(hashSet, s1, s2, s3, s4);

        traverseElement(hashSet);

        judgeElement(hashSet, s1);

        deleteElement(hashSet, s2);

    }

    private static void judgeElement(HashSet<Student> hashSet, Student s1) {
        System.out.println("----------判断-------------");
        System.out.println(hashSet.contains(s1));
        System.out.println(hashSet.isEmpty());
    }

    private static void traverseElement(HashSet<Student> hashSet) {
        System.out.println("-------------遍历------------");
        //增强for
        System.out.println("------增强for--------");
        for (Object object:hashSet) {
            Student s=(Student) object;
            System.out.println(s.toString());
        }
        //迭代器
        System.out.println("------迭代器--------");
        Iterator iterator=hashSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
//            iterator.remove();
        }
    }

    private static void deleteElement(HashSet<Student> hashSet, Student s2) {
        System.out.println("----------删除---------");
        hashSet.remove(s2);
        hashSet.remove(new Student("小明",18));
        System.out.println(hashSet);
        hashSet.clear();
        System.out.println(hashSet);
    }

    private static void addStudent(HashSet<Student> set1, Student s1, Student s2, Student s3, Student s4) {
        System.out.println("----------添加---------");
        set1.add(s1);
        set1.add(s2);
        set1.add(s3);
        set1.add(s4);
        set1.add(new Student("小明",18));
        System.out.println(set1);
        System.out.println("元素个数:"+set1.size());
    }
}

代码执行结果
在这里插入图片描述

TreeSet

特点
  • 基于排序顺序实现元素不重复
  • 实现了SortedSet接口,对集合元素自动排序
  • 元素对象的类型必须实现Comparable接口,指定排序规则
  • 通过CompareTo来确定重复元素
案例
public class testTreeSet {
    public static void main(String[] args) {
        TreeSet<String> treeSet=new TreeSet<>();
        addElement(treeSet);
        traverseElement(treeSet);
        judgeElement(treeSet);
        deleteElement(treeSet);
    }

    private static void judgeElement(TreeSet<String> TreeSet) {
        System.out.println("----------判断-------------");
        System.out.println(TreeSet.contains("李四"));
        System.out.println(TreeSet.contains("木头人"));
        System.out.println(TreeSet.isEmpty());
    }

    //遍历元素
    private static void traverseElement(TreeSet<String> TreeSet) {
        System.out.println("----------遍历-------------");
        //增强for
        System.out.println("------增强for------");
        for (String string:TreeSet) {
            System.out.println(string);
        }
        //使用迭代器
        System.out.println("------迭代器------");
        Iterator iterator=TreeSet.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
            //使用迭代器删除集合中的元素
//            iterator.remove();
        }
        System.out.println(TreeSet.size());
    }

    private static void deleteElement(TreeSet<String> TreeSet) {
        System.out.println("------删除------");
        TreeSet.remove("张三");
        System.out.println(TreeSet);
        //清空
        TreeSet.clear();
        System.out.println(TreeSet);
    }

    private static void addElement(TreeSet<String> TreeSet) {
        TreeSet.add("李四");
        TreeSet.add("张三");
        TreeSet.add("王五");
        TreeSet.add("张三");
        //[李四, 张三, 王五]      无序
        System.out.println(TreeSet);
        //元素个数:3            不可重复
        System.out.println("元素个数:"+TreeSet.size());
    }
}

代码执行结果
在这里插入图片描述
指定排序规则有两种方法:

  • 自定义类对象实现Comparable接口,指定排序规则(见Student类中的compareTo方法)
  • 创建集合时指定排序规则
public class testTreeSetForStudent {
    public static void main(String[] args) {
        TreeSet<Student> treeSet=new TreeSet<>();
        Student s1=new Student("小明",18);
        Student s2=new Student("小红",20);
        Student s3=new Student("小华",19);
        Student s4=new Student("小华",20);

        addStudent(treeSet, s1, s2, s3, s4);
        traverseElement(treeSet);
        judgeElement(treeSet, s1);
        deleteElement(treeSet, s2);

        //创建集合并指定比较规则
        TreeSet<Student> students=new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int n1=o1.getAge()-o2.getAge();
                int n2=o1.getName().compareTo(o2.getName());
                return n1==0?n2:n1;
            }
        });
        System.out.println("===========指定比较规则后======");
        addStudent(students, s1, s2, s3, s4);
        traverseElement(students);
        judgeElement(students, s1);
        deleteElement(students, s2);
    }

    private static void judgeElement(TreeSet<Student> treeSet, Student s1) {
        System.out.println("----------判断-------------");
        System.out.println(treeSet.contains(s1));
        System.out.println(treeSet.isEmpty());
    }

    private static void traverseElement(TreeSet<Student> treeSet) {
        System.out.println("-------------遍历------------");
        //增强for
        System.out.println("------增强for--------");
        for (Object object:treeSet) {
            Student s=(Student) object;
            System.out.println(s.toString());
        }
        //迭代器
        System.out.println("------迭代器--------");
        Iterator iterator=treeSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
//            iterator.remove();
        }
    }

    private static void deleteElement(TreeSet<Student> treeSet, Student s2) {
        System.out.println("----------删除---------");
        treeSet.remove(s2);
        treeSet.remove(new Student("小明",18));
        System.out.println(treeSet);
        treeSet.clear();
        System.out.println(treeSet);
    }

    private static void addStudent(TreeSet<Student> treeSet, Student s1, Student s2, Student s3, Student s4) {
        System.out.println("----------添加---------");
        treeSet.add(s1);
        treeSet.add(s2);
        treeSet.add(s3);
        treeSet.add(s4);
        treeSet.add(new Student("小明",18));
        System.out.println(treeSet);
        System.out.println("元素个数:"+treeSet.size());
    }
}

代码执行结果
在这里插入图片描述
在这里插入图片描述

其他相关文章

java集合框架Collection java集合框架 java.util.Collection接口 Collection接口中定义了所有集合都要具备的功能,它也是所有集合的顶级接口。 Collection下面有几种不同集合的分类,常见的两个: 1.java.util.List:List接口表示的是线性表,是有序的且可以存放重复元素的集合 * 2.java.util.Set:Set接口表示的是不可重复的集合 这里指的重复是元素是... 阅读详情

相关推荐

医院网络设计(完整文档+思科拓扑图)

大家好,我是小华学长,一名计算机领域的博主。经过多年的学习和实践,我积累了丰富的计算机知识和经验,在这里我想与大家分享我的学习心得和技巧,帮助你成为更好的程序员。作为一名计算机博主,我一直专注于编程、算法、软件开发等领域,在这些方面积累了大量的经验。我相信分享是一种双赢的方式,通过分享,我可以帮助他人提升技术水平,同时也能够得到学习交流的机会。在我的文章中,你将会看到我对于各种编程语言、开发工具以及常见问题的解析和分析。我会结合自己的实际项目经验,为你提供实用的解决方案和优化技巧。

qq1325513482的博客 2万+

java集合Collection

Collection集合:分为单列集合 和 多列集合 单列集合:List、Set、Queue集合;多列集合:Map集合

LXMXHJ的博客 1610

第六节 旅烨cms实战练习 -2021.03.11

第六节 旅烨cms实战练习 -2021.03.11 前期使用win10里小皮搭建旅烨cms,以为自己的各项配置有问题,但是小皮能够正常带动DVWA,始终带动不了旅烨cms。 后来与其他安全人员交流,发现win7可以正常运行旅烨cms。 所以目前为何出现问题尚不清楚 1.环境搭建 win7+小皮 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nOSTEFUw-1615475228402)(C:\Users\LuXian\AppData\Roaming\Typora\typora

lyx3429的博客 573

【java-学习】day11

迭代器依赖于集合而存在,在判断成功后,集合中添加了新的元素,而迭代器并不知道,所有就报错了。其实这个问题说的是:迭代器遍历集合中的元素的时候,不要使用集合对象去修改集合中的元素。我们学习的是面向对象的编程语言,面向对象的编程语言对事物的描述都是通过对象体现的,遍历集合的时候,在控制台输出学生对象的成员变量值。选择数组了,而数组的长度固定,不能适应变化的需求,在这种情况下,Java就提供了集合类供我们使用。我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现。

hahamd的博客 267

电磁流量计模拟前端电路设计与优化

模拟前端电路(AFE)是传感器信号处理的核心环节,通过放大、滤波和模数转换实现信号调理。其设计需重点考虑放大器共模抑制比(CMRR)和ADC分辨率等关键参数,在电磁流量计等工业测量应用中,AD8220仪表放大器与AD7172-2 Σ-Δ型ADC的组合能有效简化传统多级架构。现代过采样技术结合数字滤波可替代模拟滤波器,显著提升系统集成度与测量精度,这种设计方法同样适用于压力传感、生物电信号采集等需要高精度信号调理的场景。

weixin_42602241的博客 618

深信服PT1-AF实验考试

深信服PT1-AF实验考试实验练习可以去深信服的在线实验平台(链接如下):深信服在线实验平台 (sangfor.com.cn)实验考试其实就是把所有的每一部分实验综合了起来以下内容仅做参考,不确保一样。

m0_72019240的博客 7116

Java进阶-Collection集合

Java进阶-Collection集合

qq_52699757的博客 702

ISP算法:宽动态HDR

动态范围(Dynamic Range):在很多领域来表示某个变量的最大值和最小值的比值。在数字图像处理领域里,动态范围也被称为图像对比度,表示图像可以显示范围内,像素最大亮度值和最小亮度值的比值。 高动态范围图像(High Dynamic Range Image): 可以反映真实场景中高动态范围亮度信息图像。 高动态技术:能自动调整图像动态范围的一种技术。该技术是一种利用计算机高速运算产生高

a200800170331的专栏 1万+

第十三章、集合---Collection集合、迭代器、增强for、List集合、Set集合

Collection 目录 文章目录Collection目录主要内容学习目标第十三章 集合(续)13.2 集合框架13.3 Collection 常用功能13.4 Iterator迭代器13.4.1 Iterator接口13.4.2 迭代器的实现原理13.4.3 使用Iterator迭代器删除元素13.4.4 增强for练习1:遍历数组练习2:遍历集合13.4.5 java.lang.Iterab...

爱码者的博客 1556

笔试题-2023-星宸-数字IC设计【纯净题目版】

4 笔试题-2023-星宸-数字IC设计【纯净题目版】

w0shishabi的博客 1172

如何用 Claude Code 做代码重构,而不是重写

摘要: 在使用Claude Code进行代码优化时,AI容易“重写”而非“重构”,导致改动过大、review困难。本文提出3个关键约束方法:1)限制修改范围(如仅修改指定函数);2)明确不改变代码行为;3)要求最小必要修改。通过分步骤重构、先获取重构建议再修改、使用标准模板(保持函数签名/输入输出/结构不变)等方式,可使AI输出更符合工程需求的小范围安全重构。对于彻底混乱的代码,才建议直接重写。核心在于通过提示词精准约束AI的修改行为,将其转化为高效的代码重构助手。

sg_knight的专栏 981

Java基础学习笔记11——集合(Collection)接口

目录 1、ArrayList类 1.1、ArrayList集合的构造方法和添加方法 1.1、ArrayList集合的常用方法 集合类的特点:提供一种存储空间可变的存储模型,存储的数据容量可以发生改变。 1、ArrayList类 集合类有很多,如ArrayList类,该类在java.util包下,使用前需要导包。 ArrayList<E>: 可调整大小的数组实现; <E>:是一种特殊的数据类型,泛型。 在出现E的地方我们使用数据类型替换即可。举例:ArrayList&

weixin_44623055的博客 1414

基于YOLOv26的智能农业无人机巡检系统

本文提出了一种基于YOLOv26的智能农业无人机巡检系统。系统采用模块化设计,包含数据采集、预处理、目标检测、图像拼接、分析决策和部署控制六大核心模块。无人机搭载多光谱相机、RGB相机和热红外相机进行农田数据采集,通过YOLOv26实现作物病虫害、杂草及灌溉设备的检测。系统采用PyTorch、OpenCV等技术栈,支持无人机自动路径规划和多传感器数据采集,并包含数据增强和预处理功能。该系统可有效提升农业巡检效率,为精准农业提供智能化解决方案。

一键难忘的博客 808

java集合进阶篇-《Collection集合》

java中的集合也是一个重点,同样也是难点,涉及到的东西较多较杂,希望大家能够和我一起来学习java集合进阶方面的知识。预计一周的时间将java集合进阶部分拿下,加油。在Java中,是一个接口,它是所有Java集合框架的根。它定义了集合类必须实现的基本操作,如添加元素、删除元素、迭代等。所有的集合类(如ListSetQueue等)都是Collection接口的直接或间接实现。

欢迎大家一起讨论交流 1053

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

java集合之——Collection接口

前言 老师在教我们学习集合的时候,大多都是以数组为砖,列举了对象数组有那些问题?比如普通的对象数组的最大问题在于数组中的元素个数是固定的,不能动态的扩充大小,所以最早的时候可以通过链表实现一个动态对象数组。但是这样做毕竟太复杂了,所以在 Java 中为了方便用户操作各个数据结构,所以引入了类集的概念,有时候就可以把类集称为 java 对数据结构的实现。 在整个类集中的,这个概念是从 JDK 1.2(Java 2)之后才正式引入的,最早也提供了很多的操作类,但是并没有完整的提出类集的完整概念。 类集中最大的几

you do ,you win 4310

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

JAVA学习-集合-Collection

认识集合,操作集合。

LOVEcao_YU的博客 979

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

【JavaSE系列】Java集合框架之Collection,Map接口

本篇文章带大家认识Java基础知识——集合框架,从本篇文章开始正式拉开数据结构的序幕,要深入了解一件事,那必然先得见个面,本文带大家认识Java集合框长什么样,以及怎么使用Collection和Map接口。

未见花闻的博客 1832

【Java】集合之Collection概述

目录 一、前言 二、常见的集合概览图 三、Collection集合 1.Collection集合常用方法 2.Collection集合之迭代器 迭代器介绍 迭代器常用方法: 一、前言 集合在Java中的作用是显而易见的,他可以存储我们的信息,可以实现增删减改的 操作,相当于一个模拟的"数据库"。 二、常见的集合概览图 集合都是在java.util包下的,所以在使用时先导包。 三、Collection集合 public interface Collection&.

weixin_60719453的博客 2437

Java——Collection集合

Collection集合

m0_72786443的博客 415

Java 集合框架(Collection)

这个方法允许你指定数组的类型,并且可以避免类型转换的开销。相关的方法,当然,Collection的功能List也都继承了。1.LinkedList集合的底层是基于什么实现的?① 如果集合支持索引,可以使用for循环遍历,每删除数据后做。获取集合的迭代器对象,对迭代器对象使用.next()方法。基于双链表的这种特点,LinkedList新增了很多。写累了,承受了这个年纪不该承受的(惆怅脸)…函数式接口的实现,这个实现可以是一个。就是removeFirst方法的套皮,来实现的,但隐藏了迭代器的实现细节。

m0_70484213的博客 1169

Java中的Collection集合

JAVASE提供了满足各种需求的API,在使用这些API前,先了解其继承与接口操作架构,才能了解何时采用哪个类,以及类之间如何彼此合作,从而达到灵活应用。 集合按照其存储结构可以分为两大类,分别是单列集合java.util.Collection和双列集合java.util.Map本文主要讲Collection集合,后续再补充Map集合。

write less , do more 3443

Java集合进阶 单列集合collection和list

Java当中一共有很多种集合之前学过的ArrayList只是其中的一类。Collection单列集合:添加一个元素Map双列集合:添加俩个元素。

2302_80073130的博客 2063

java集合之Collection

java集合之Collection集合Collection体系结系Collection集合概述和基本使用Collection集合的常用方法List集合Set集合3.Collection集合的常用方法4Collection集合的遍历功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML

qq_46940519的博客 379

Java—— 单列集合 Collection

单列集合体系结构。Collection介绍。常用方法。Collection的遍历方式:迭代器遍历。增强for遍历。Lambda表达式遍历。

zjy_ccccc的博客 760

java collection详解_Java集合-Collection详解

基础知识:什么是集合?集合是一个容器。把多个对象放入容器中。有一个水杯,你可以选择把水不断往里装,也可以选择装牛奶。但是不能两种不同的东西混合装一个杯子。集合这个容器里装的一定是同一类型的东西。(引用类型,不能是基本类型)看到这个介绍,我们可能想到数组,数组要求的也是里面必须存放的是一种数据类型的结构。但数组和集合的区别呢?数组大小是固定的,集合的大小理论上是不限定。数组里的元素可以是基本类型,也...

weixin_28771631的博客 443

Java集合之Collection集合详解

JAVA集合之Collection集合详解,List接口,ArrayList集合,LinkedList集合,List集合遍历,TreeSet集合遍历,TreeSet集合,HashSet集合,forEach,ArrayList集合遍历

m0_62729850的博客 1396
上一篇: Java常用类--BigDecimal类和System类
下一篇: Java集合--泛型集合
likehack
博客等级 码龄8年 526粉丝 35原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

likehack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值