首页 > 其他分享 >Day05集合-Collection

Day05集合-Collection

时间:2022-08-26 19:22:17浏览次数:63  
标签:name System Day05 collection println Collection student 集合 out

集合

1.集合的概念

  • 对象的容器,实现了对对象的常用操作,类似数组功能
  • 位置:java.util.*

2.集合和数组的区别

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

Collection体系结构

1.collection父接口

  • 代表一组任意类型的对象,无序,无下标,可存放重复元素(List)也可以存放不重复元素(Set)
  • 方法:

  • A.removeAll(Collection c):在A集合中移除A,B的交集,即A-B
  • iterator( ):返回在此元素上进行迭代的迭代器(遍历集合)
    • hasNext():是否有下一个元素
    • next(): 获取下一个元素
    • remove():获取当前元素

2.Collection使用

/*
Collection接口的使用1
(1).增加元素
(2).删除元素
(3).遍历集合
(4).判断(是否为空,是否含有某元素)
 */
public class demo01 {
    public static void main(String[] args) {
        //1.增加
        Collection collection = new ArrayList();
        collection.add("melon");
        collection.add("peach");
        collection.add("apple");
        System.out.println("集合长度为" + collection.size());
        System.out.println(collection);

        //2.删除
        collection.remove("melon");
        System.out.println("集合长度为" + collection.size());

        //3.遍历(important!!)
        //3.1使用增强for
        System.out.println("--------增强for--------");
        for (Object object:collection) {
            System.out.println(object);
        }
        //3.2使用iterator迭代器(返回一个Object类型数据)
        /*
        迭代器中的三个方法:
        hasNext():是否有下一个元素
        next(): 获取下一个元素
        remove():获取当前元素
         */
        System.out.println("---------迭代器----------");
        Iterator it = collection.iterator();
        while (it.hasNext()){
            Object ob = it.next();
            System.out.println(ob);
            /*不可以在迭代器内使用collection的方法,出现并发修改异常
            collection.remove(ob);
             */
            it.remove();
        }
        System.out.println(collection);

        //4.判断
        System.out.println(collection.contains("apple"));
        System.out.println(collection.isEmpty());
    }
}



//输出结果:
集合长度为3
[melon, peach, apple]
集合长度为2
--------增强for--------
peach
apple
---------迭代器----------
peach
apple
[]
false
true
/*
Collection接口的使用2(对象)
 */
public class demo02 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        student s1 = new student("赵美延",26);
        student s2 = new student("ssw",29);
        student s3 = new student("pjh",32);

        //1.添加对象
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println(collection);

        //2.删除
        collection.remove(s1);
        System.out.println(collection);

        //3.遍历
        //增强for循环
        for (Object s:collection
             ) {
            student stu = (student)s;
            System.out.println(stu.toString());
        }
        //iterator:返回一个Object类型数据
        Iterator it = collection.iterator();
        while (it.hasNext()){
            student student =(student)it.next();
            System.out.println(student);
        }

        //4.判断
        System.out.println(collection.isEmpty());
        System.out.println(collection.contains(s2));
    }
}

//输出结果:
[student{name='赵美延', age=26}, student{name='ssw', age=29}, student{name='pjh', age=32}]
[student{name='ssw', age=29}, student{name='pjh', age=32}]
student{name='ssw', age=29}
student{name='pjh', age=32}
student{name='ssw', age=29}
student{name='pjh', age=32}
false
true

标签:name,System,Day05,collection,println,Collection,student,集合,out
From: https://www.cnblogs.com/workplace-blog/p/16628931.html

相关文章

  • C#集合:ICollection和IList接口
    虽然枚举接口IEnumerable提供了一种向前迭代集合的协议,但是它们并没有提供确定集合大小、根据索引访问成员、搜索以及修改集合的机制。为了实现这些功能,.NETCore定义了ICo......
  • 集合集成关系
    collection集合继承实现关系(单列集合)Collection有个重要子接口List和Set,他们的实现子类都是单列集合===========================================================......
  • 列表、元素、字典和集合
    Pythonlist`list`可存储任意类型,支持`list()`。list方法增删查appendpopindexextendremovecountinsertclear子串是Python序列操作的一......
  • 集合.Collection体系集合
    Collection父接口特点:代表一组任意类型的对象,无序,无下标,不能重复方法:booleanadd(Objectobj);添加一个对象booleanaddAll(Collectionc);将一个集合中的所有对象......
  • ArrayList集合存储基本数据类型
    ArrayList集合存储基本数据类型 如果希望向集合ArrayList当中存储基本类型数据,必须使用基本类型对应的“包装类”基本类型包装类byteByteshor......
  • ArrayList集合概述和基本使用和常用方法和遍历
    ArrayList集合概述和基本使用数组的长度不可以发生改变但是ArrayList集合的长度是可以随意变化的对于ArrayList来说,有一个尖括号<E>代表泛型泛型:也就是转正集合当中的......
  • Java-Java集合
    一、List,Set,Map三者的区别二、Arraylist与LinkedList区别三、ArrayList与Vector区别呢?为什么要⽤Arraylist取代Vector呢四、HashMap和Hashtable的区别五......
  • Java-Java集合排序
    一、ListMap排序修订记录版本是否发布2020-01-25v1.0是一、ListMap排序Java中list里面存放map,根据map中的某一个或多个字段进行排序importjava.u......
  • Java8 对list集合中的bigdecimal进行分组求和,均值,最大值,最小值
     文章目录需求中对数值进行求和的非常多,但java8对bigdecimal求和没有封装新建接口ToBigDecimalFunction新建工具类CollectorsUtil实体类Person 需求中对......
  • day25--Java集合08
    Java集合0815.HashTable15.1HashTable的基本介绍存放的元素是键值对:即K-VHashTable的键和值都不能为nullHashTable的使用方法基本上和HashMap一样HashTable是线程安......