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接口表示的是不可重复的集合 这里指的重复是元素是... 阅读详情

相关推荐

ControlCAN.dll开发适配USBCAN_E/2E_U设备

摘要:本文提供了USBCAN_E/2E_U设备的配置指南:1)安装专用驱动;2)获取最新的ControlCAN.dll文件;3)修改VCI调用顺序,新增VCI_SetReference函数设置波特率;4)使用工具计算波特率配置值,并给出了常见波特率对应的代码参考值(如500K对应0x060007)。配置过程强调使用特定工具计算精确的波特率参数,并提供了相关软件下载链接。

weixin_43969033的博客 412

java集合Collection

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

LXMXHJ的博客 1614

磁栅尺的基本原理

一、磁栅尺简介 磁栅尺(Magnetic scale)是一种常见于工业自动化设备领域的线性位移测量方案,因为其耐用性强,对环境要求不高,逐渐替代了对环境要求苛刻的光学直线测量方案。 作为自动化设备领域里不多见的精密位移检测方案,磁栅尺使用广泛,尤其适用于检测行程长、测量精度要求高、工况复杂的大型机床。磁栅尺安装方便,抗干扰能力强,抗冲击性强,不易受到振动、粉尘、水垢、油污等影响,耐高温和耐低温性能也较为突出。 二、磁栅尺基本组成与原理 磁栅尺主要由两个部分组成,磁尺和读磁头。 磁尺 磁尺是磁栅尺

Luckboy_shy的博客 1万+

【java-学习】day11

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

hahamd的博客 269

kubeadm init 运行时kebelet启动失败问题

解决k8s kubeadm init时遇到的kubelet问题

ZDPZN的博客 6080

OASIS协议标准文档的解读_第一部分

OASIS标准协议的文档的解读。完整原协议的中文翻译和解读。

洋葱的专栏 4920

Java进阶-Collection集合

Java进阶-Collection集合

qq_52699757的博客 702

Spark数据倾斜问题+解决方案

1、数据倾斜 数据倾斜指的是,并行处理的数据集中,某一部分(如Spark或Kafka的一个Partition)的数据显著多于 其它部分,从而使得该部分的处理速度成为整个数据集处理的瓶颈 数据倾斜俩大直接致命后果 1)数据倾斜直接会导致一种情况:Out Of Memory 2)运行速度慢 主要是发生在Shuffle阶段。同样Key的数据条数太多了。导致了某个key(下图中的80亿条)所在的Task数 据量太大了。远远超过其他Task所处理的数据量 一个经验结论是:一般情况下,OOM的原因都是数据倾斜 2、如

诸葛搏毅 6779

第十三章、集合---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...

爱码者的博客 1558

告别重复劳动:用Python+pywinauto打造你的微信个人助理(自动回复/收款/定时发消息)

本文介绍如何利用Python和pywinauto库构建微信个人助手,实现自动回复、定时消息、智能收款等功能,告别重复劳动。通过详细的代码示例和配置指南,帮助开发者快速掌握微信自动化技术,提升工作效率。

weixin_42568801的博客 340

YOLOv8改进策略【卷积层】| CVPR-2023 部分卷积 PConv 轻量化卷积,降低内存占用

本文记录的是利用优化的目标检测方法研究。深度可分离卷积可以减少FLOPs,但会导致更高的内存访问,引起延迟并减慢整体计算。部分卷积利用逐点卷积处理通道冗余,减少模型计算量和内存访问量。Run, Don’t Walk: Chasing Higher FLOPS for Faster Neural Networks及其改进的实现代码如下: 四、创新模块 4.1 改进点⭐ 模块改进方法:直接加入(第五节讲解添加步骤)。模块加入如下:模块改进方法:基于的(第五节讲解添加步骤)。第二种改进方法是对中的进行改进,并将

Limiiiing的博客 1037

集合框架

【代码】集合框架。

m0_64703222的博客 109

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

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

欢迎大家一起讨论交流 1056

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

java集合之——Collection接口

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

you do ,you win 4311

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

JAVA学习-集合-Collection

认识集合,操作集合。

LOVEcao_YU的博客 980

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

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

未见花闻的博客 1836

【Java】集合之Collection概述

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

weixin_60719453的博客 2438

Java——Collection集合

Collection集合

m0_72786443的博客 416

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 3449

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

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

2302_80073130的博客 2065

java集合之Collection

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

qq_46940519的博客 380

Java—— 单列集合 Collection

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

zjy_ccccc的博客 760

java collection详解_Java集合-Collection详解

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

weixin_28771631的博客 444

Java集合之Collection集合详解

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

m0_62729850的博客 1398
上一篇: 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、付费专栏及课程。

余额充值
⌂ 换网址