当前位置: 首页 > 后端技术 > Java

【Java基础13】单列集合Collection

时间:2023-04-01 18:27:38 Java

1.集合类似于数组。它是一个将一些元素存储在一起的容器。不同的是数组的长度和类型是固定的,集合初始化后可以改变。数组类型可以是基本类型也可以是引用类型,集合只能是引用类型。2.集合系统集合主要分为两类:①单列集合Collection②双列集合Map单列集合:类似于数组,每个元素只包含一个值。两列集合:类似于二维数组,每个元素包含一个键值对。3.单列集合体系常用的集合实现类如下:List:元素有序、重复、索引。ArrayList和LinkedList:有序、重复和索引Set:元素是无序的、非重复的和索引的。HashSet:无序,不重复,无索引TreeSet:按大小排序,不重复,无索引LinkedHashSet:有序,不重复,无索引集合特点:集合存储引用类型,不是基本类型,如果保存基本类型需要一个包装类。,这种类型称为泛型,泛型只能是引用类型。使用泛型后添加。4.公共方法Collection是所有单列集合的基类。它的功能是所有单列集合都需要实现的。同时,单列集合也可以扩展自己的功能。不同的收藏也会有自己独立的功能。方法说明publicbooleanadd(Ee)将给定对象添加到当前集合publicvoidclear()清除集合中的所有元素publicbooleanremove(Ee)从当前集合中移除给定对象publicbooleancontains(Objectobj)判断当前集合是否包含给定对象publicbooleanisEmpty()判断当前集合是否为空publicintsize()返回集合中的元素个数。publicObject[]toArray()将集合中的元素存储到一个数组中5.List这是集合的下一个大分支,有序、可重复、有索引,这是这个类的主要特点。有序:存储和检索元素的顺序一致重复:存储的元素可以有相同的索引:通过索引可以直接找到对应的元素5.1创建语法//例如ArrayListarrayList=newArrayList();//jdk1.7后省略ArrayListarrayList=newArrayList<>();5.2List集合遍历集合不仅具有存储功能,还具有增删改查功能。5.2.1for循环遍历通过size方法获取长度,通过for循环遍历所有元素。publicclassListForTest{publicstaticvoidmain(String[]args){//创建一个单列集合ArrayListarrayList=newArrayList<>();//添加新元素arrayList.add(95);arrayList.add(55);arrayList.add(13);arrayList.add(5);//遍历下标for(inti=0;iarrayList=newArrayList<>();//添加新元素arrayList.add(95);arrayList.add(55);arrayList.add(13);arrayList.add(5);//增强for循环for(Integerinteger:arrayList){System.out.println(integer);}}}5.2.3Iterator该类表示集合中的元素,Iterator由集合内部类实现。默认情况下,获得的迭代器是0索引的。publicclassListIterableTest{publicstaticvoidmain(String[]args){//创建一个单列集合ArrayListarrayList=newArrayList<>();//添加新元素arrayList.add(95);arrayList.add(55);arrayList.add(13);arrayList.add(5);Iteratoriterator=arrayList.iterator();while(iterator.hasNext()){System.out.println(iterator.next());Iterator常用方法如下:方法说明Iteratoriterator();Collection集合方法,返回迭代器booleanhasNext()获取当前位置是否有元素,存在则返回true,不存在则返回falsenext()获取当前位置元素并指向下一个元素,如果存在,注意越界5.2.4Lambda表达式循环通过Lambda表达式实现循环collection//示例publicclassListForEachTest{publicstaticvoidmain(String[]args){//创建单列集合ArrayListarrayList=newArrayList<>();//添加新元素arrayList.add(95);arrayList.add(55);arrayList.add(13);arrayList.add(5);//forEach循环arrayList.forEach(newConsumer(){@Overridepublicvoidaccept(Integerinteger){System.out.println(integer);}});//Lambda表达式arrayList.forEach(i->System.out.println(i));}}5.3每个实现类都有List特有的方法:方法说明voidadd(intindex,Eelement)在指定位置E插入一个元素remove(intindex)在指定位置E移除一个元素set(intindex,Eelement)修改指定位置的元素Eget(intindex)获取指定位置的元素ArrayList:底层基于可变数组实现,查询增删快但慢说明publicvoidaddFirst(Ee)在列表头部插入publicvoidaddLast(Ee)在指定位置插入尾部publicEgetFirst()获取第一个元素publicEgetLast()获取尾部元素publicEremoveFirst()删除第一个元素并返回publicEremoveLast()返回尾元素并将其删除。5.4存储自定义类型存储自定义类型publicclassListTest{publicstaticvoidmain(String[]args){//Book类查看文章最终来源地址ArrayListarrayList=newArrayList<>();arrayList.add(newBook("三体","刘慈欣"));arrayList.add(newBook("平凡的世界","璐瑶"));//也可以添加相同的arrayList.add(newBook("Alive","YuHua")twice);arrayList.add(newBook("活着","余华"));//遍历for(Bookbook:arrayList){System.out.println(book);}}}6。集合这是集合中的第二大分支,主要特点是无序、不可重复、无索引。6.1格式同集合ListHashSethashSet=newHashSet<>();6.2集合遍历因为没有索引,所以除了长度循环外,其他循环与List一致。从遍历显示可以看出AA只输出一次,所以Set集合中不能有重复。publicclassSetForTest{publicstaticvoidmain(String[]args){//创建一个HashSetHashSethashSet=newHashSet<>();//添加一个元素(因为不能重复,只能添加一次,重复newhashSet.add("AA");hashSet.add("BB");hashSet.add("CC");hashSet.add("AA");//增强的for循环for(Strings:hashSet){System.out.println(s);}//迭代器Iteratoriterator=hashSet.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}//forEach循环hashSet.forEach(newConsumer(){@Overridepublicvoidaccept(Strings){System.out.println(s);}});//forEachplusLambdahashSet.forEach(s->System.out.println(s));}}6.3各实现类HashSet的特点:无序、不重复、无索引,底层使用哈希表。LinkedHashSet:有序、不重复、无索引,底层是一个哈希表加一个双向链表TreeSet:排序、不重复、无索引,红黑树注:_底层数据结构后面会解释_6.4公共子类示例publicclassDog{privateStringname;私人年龄;publicDog(){}publicDog(Stringname,intage){this.name=name;这个。年龄=年龄;}publicStringgetName(){返回名称;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){返回年龄;}publicvoidsetAge(intage){this.age=age;}@OverridepublicStringtoString(){return"Dog{"+"name='"+name+'\''+",age="+age+'}';}}HashSetpublicclassHashSetTest{publicstaticvoidmain(String[]args){HashSethashSet=newHashSet<>();hashSet.add(newDog("旺财",18));hashSet.add(newDog("旺财",18));//输出可以看到,会有两个相同的元素显示出来,根据不可重复性原则是不正确的。那么问题出在哪里。for(Dogdog:hashSet){System.out.println(dog);}}}如果进行比较,则不必实现equals方法,这样可以避免重复,在自定义类中重写equals和hashCode@Overridepublicbooleanequals(Objecto){if(this==o)return真的;如果(o==null||getClass()!=o.getClass())返回false;Dogdog=(Dog)o;返回年龄==dog.age&&Objects.equals(name,dog.name);}@OverridepublicinthashCode(){returnObjects.hash(name,age);}LinkedHashSetpublicclassLinkedHashSetTest{publicstaticvoidmain(String[]args){LinkedHashSetlinkedHashSet=newLinkedHashSet<>();linkedHashSet.add(newDog("旺财",19));linkedHashSet.add(newDog("小强",20));for(Dogdog:linkedHashSet){System.out.println(dog);}}}TreeSetpublicclassTreeSetTest{publicstaticvoidmain(String[]args){//创建一个TreeSet,需要实现一个排序方法,这里是对TreeSet进行倒序排序treeSet=newTreeSet<>((o1,o2)->o1.getAge()-o2.getAge());treeSet.add(newDog("张三",20));treeSet.add(newDog("李四",18));treeSet.add(newDog("王二",19));for(Dogdog:treeSet){System.out.println(dog);}}}本章结束,供小白个人学习和介绍,大佬勿喷!希望大家喜欢、收藏、支持、支持!源码【GitHub】【码云】