集合类.docx
- 文档编号:12092028
- 上传时间:2023-04-17
- 格式:DOCX
- 页数:21
- 大小:259.37KB
集合类.docx
《集合类.docx》由会员分享,可在线阅读,更多相关《集合类.docx(21页珍藏版)》请在冰豆网上搜索。
集合类
接口:
Collection:
集合层次中的根接口,JDK不提供此接口的任何直接实现:
它提供更具体的子接口(如Set和List)实现,Collection表示一组对象,这些对象也称为collection的元素
子接口:
Set和List
⏹Collection:
集合层次中的根接口,JDK没有提供这个接口直接的实现类。
⏹Set:
不能包含重复的元素。
SortedSet是一个按照升序排列元素的Set。
⏹List:
是一个有序的集合,可以包含重复的元素。
提供了按索引访问的方式。
⏹Map:
包含了key-value对。
Map不能包含重复的key。
SortedMap是一个按照升序排列key的Map。
⏹Queue:
模拟了队列(FIFO),队列的头部保存存放时间最长的元素,尾部保存存放时间最短的元素,新元素插入到尾部,队列不允许随机访问队列中的元素。
子接口List(有类list;):
List:
是一个有序的集合,可以包含重复的元素
List的两个实现类ArrayList和LinkedList
如果要使用这些类,接口需要引用:
importjava.util.*;(*要用需要的类代替)
引入后,因为ArrayList(LinkedList类定义一样)就是类所以定义方法可以是:
ArrayLista1=newArrayList();
LinkedLista2=newLinkedList();
都有方法add();(add(E element))调用为a1.add();将指定的元素添加到此列表的尾部。
括号中就是说的指定的元素,可以是String,也可以是引用变量,如:
a2.add(newPoint(1,1));a1.add("ddd");
还有一种形式add(int index,E element)将指定的元素插入此列表中的指定位置
size()方法,获得a1(a2)的长度,使用a1.size();
get()方法:
返回此列表中指定位置上的元素。
使用a1.get(intindex);
toArray()方法:
按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。
。
。
返回类型Objcet[];Object[]objs=a2.toArray();
Arrays类:
方法很多看JDK,现在用一方法:
asList();如:
Lista3=Arrays.asList(objs);返回一个受指定数组支持的固定大小的列表,所以a3不可以再用add方法,编译可以,但是运行出错
看一Test类
packagecom.niit.collection;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.List;
publicclassTestArrayList{
publicstaticvoidmain(String[]args){
ArrayLista1=newArrayList();
a1.add("k");//add方法
a1.add("kk");
a1.add("ddd");
//intlength=a1.size();size()方法,获得a1的长度
for(inti=0;i System.out.println((i+1)+""+a1.get(i)); //get方法,返回此列表中指定位置上的元素。 } ArrayLista2=newArrayList(); a2.add(newPoint(1,1)); a2.add(newPoint(2,2)); a2.add(newPoint(3,3)); for(inti=0;i System.out.println((i+1)+""+a2.get(i)); } //如果下方的Point类中没有定义toString方法,则上边输出是地址 //下方方式为增强for循环 for(Objectobj: a1){ System.out.println(obj); } Object[]objs=a2.toArray(); for(Objectobj1: objs){ System.out.println(obj1); } Lista3=Arrays.asList(objs); for(Objectob: a3){ System.out.println(ob); } } } classPoint{ intx,y; Point(intx,inty){ this.x=x; this.y=y; } publicStringtoString(){ returnx+""+y; } } ⏹ArrayList: 我们可以将其看作是能够自动增长容量的数组。 ⏹利用ArrayList的toArray()返回一个数组。 ⏹Arrays.asList()返回一个列表。 ⏹迭代器(Iterator)给我们提供了一种通用的方式来访问集合中的元素。 迭代器: Iterator: 接口(要使用也要先引入,importjava.util.Iterator),在Collection接口中有iterator方法,一般如下使用: (Collectionc) Iteratoriter=c.iterator(); 在Iterator中有三种方法: hasNext()[返回值Boolean],如果仍有元素可以迭代,则返回true。 next()[返回迭代的下一个元素。 ] Remove();void 看下边一方法: publicstaticvoidprintElements(Collectionc){ if(c==null) return; Iteratoriter=c.iterator(); while(iter.hasNext()) System.out.println(iter.next()); } 接口ComparablecompareTo 接口Comparatorcompare 接口Comparable: (有方法compareTo) compareTo方法: 返回值为int10-1,三种; 看一个实现该接口的类Point: classPointimplementsComparable { intx,y; Point(intx,inty) { this.x=x; this.y=y; } publicStringtoString() { return"点("+x+","+y+")"; } publicintcompareTo(Objecto){ Pointp=(Point)o; return(this.x+this.y)>(p.x+p.y)? 1: ((this.x+this.y)==(p.x+p.y)? 0: -1); } } 在上边的的compareTo方法中,传入的对象是Objecto,所以要把它转为Point; Collections类: Importjava.util.Collections;引入类: LinkedListlist=newLinkedList(); 方法sort(list): 调用Collections.sort();根据元素自然顺序,升序排序 Sort(list,Comparator superT> c);根据指定比较器产生的顺序对指定列表进行排序。 {{接口Comparator定义: publicstaticComparatorgetComparatorNO() 调用的时候Collections.sort(list,Student.getComparatorNO)[假设定义Student这个学生类]; 接口里边的方法是compare,所以要在实现的类里进行重写 publicintcompare(Objecto1,Objecto2){ Students1=(Student)o1; Students2=(Student)o2; return(s1.name)==(s2.name)? 0: (((s1.name).compareTo(s2.name)==1)? 1: -1); } 返回值为10-1 整个接口定义: (getComparatorName是要实现的接口的名) publicstaticComparatorgetComparatorName(){ returnnewComparator(){ publicintcompare(Objecto1,Objecto2){ Students1=(Student)o1; Students2=(Student)o2; return(s1.name)==(s2.name)? 0: (((s1.name).compareTo(s2.name)==1)? 1: -1); } };此处是匿名内部类的实现 } }} Collections类的另一个方法: reverse().对列表进行反向排序 Collections.sort(al); Collections.reverse(al); 一个整体例子TestStudent packagecom.niit.lesson; importjava.util.Comparator; importjava.util.LinkedList; importjava.util.List; importjava.util.Collection; importjava.util.Collections; importjava.util.Iterator; publicclassTestStudent{ publicstaticvoidprintElements(Collectionc){ if(c==null) return; Iteratoriter=c.iterator(); while(iter.hasNext()) System.out.println(iter.next()); } publicstaticvoidmain(String[]args){ LinkedListaa=newLinkedList(); aa.add(newStudent("aa",20)); aa.add(newStudent("cc",30)); aa.add(newStudent("bb",25)); printElements(aa); Collections.sort(aa,Student.getComparatorName()); printElements(aa); Collections.sort(aa,Student.getComparatorNO()); printElements(aa); } } classStudent{ Stringname; intNO; Student(Stringname,intNO){ this.name=name; this.NO=NO; } publicstaticComparatorgetComparatorName(){ returnnewComparator(){ publicintcompare(Objecto1,Objecto2){ Students1=(Student)o1; Students2=(Student)o2; return(s1.name)==(s2.name)? 0: (((s1.name).compareTo(s2.name)==1)? 1: -1); } }; } publicstaticComparatorgetComparatorNO(){ returnnewComparator(){ publicintcompare(Objecto1,Objecto2){ Students1=(Student)o1; Students2=(Student)o2; returns1.NO>s2.NO? 1: (s1.NO==s2.NO? 0: -1); } };//此处是匿名内部类(实现接口Comparator,) } publicStringtoString(){ returnname+""+NO; } } 接口: Comparable 方法compareTo();返回int类型: 以下是类Point实现接口Comparable classPointimplementsComparable { intx,y; Point(intx,inty) { this.x=x; this.y=y; } publicintcompareTo(Objecto) { Pointp=(Point)o; return(this.x+this.y)>(p.x+p.y)? 1: ((this.x+this.y)==(p.x+p.y)? 0: -1); } publicStringtoString() { return"点("+x+","+y+")"; } } TestArray类: packagecom.niit.lesson8; importjava.util.LinkedList; importjava.util.Arrays; importjava.util.List; importjava.util.Iterator; importjava.util.Collection; importjava.util.Collections; publicclassTestArrayList { publicstaticvoidprintElements(Collectionc) { if(c==null) return; Iteratoriter=c.iterator(); while(iter.hasNext()) System.out.println(iter.next()); } publicstaticvoidmain(String[]args) { LinkedListal=newLinkedList(); al.add(newPoint(2,2)); al.add(newPoint(1,1)); al.add(newPoint(3,3)); Collections.sort(al); printElements(al); Collections.reverse(al); printElements(al); } } classPointimplementsComparable { intx,y; Point(intx,inty) { this.x=x; this.y=y; } publicintcompareTo(Objecto) { Pointp=(Point)o; return(this.x+this.y)>(p.x+p.y)? 1: ((this.x+this.y)==(p.x+p.y)? 0: -1); } publicStringtoString() { return"点("+x+","+y+")"; } } LinkedList类: LinkedList是采用双向循环链表实现的 方法: add(),addFirst(Objectobj),addLast(Objectobj),peek()[获取但不清空列表的第一个元素]remove()[获取并移除此列表的头]remove(intindex)[移除此列表中指定位置处的元素。 ]size()[列表的元素数]isEmpty()返回boolean类型,若列表元素空,则TRUE; 堆栈: 先进后出 队列: 先进先出 Linkedlist类和arraylist的区别: (用的时候区别不大。 。 ) 接口Set: 一个不包含重复元素的collection。 实现类: HashSet,TreeSet HashSet: (无序。 。 。 ) ⏹我们应该为要存放到散列表的各个对象定义hashCode()和equals()。 ⏹hashCode不相等的2个对象肯定不等;(不管equals相不相等); ⏹hashCode相等的2个对象,如果equals相等,则相等,否则则不等. 此类实现Set接口,由哈希表(实际上是一个HashMap实例)支持。 它不保证set的迭代顺序;特别是它不保证该顺序恒久不变。 (无序) 散列表中,HashSet里边,不能有重复的元素(可以有,但是保存的时候按相同保存),要像上边第一条写的,要定义hashCode()和equals(); TestHashSet: (省略引入部分,) publicclassTestHashSet{ (省略prt方法的(下边用到的,就是用来遍历输出的)) publicstaticvoidmain(String[]args){ HashSeths=newHashSet(); hs.add (1); hs.add (2); hs.add(3); hs.add (2); prt(hs);//输出为213因为里边没有重复的元素 HashSethh=newHashSet(); hh.add(newStudent(15,"king"));在下边要定义hashCode和equals hh.add(newStudent(15,"king")); hh.add(newStudent(20,"king")); hh.add(newStudent(20,"jing")); hh.add(newStudent(25,"jing")); prt(hh); 此处输出: 学号: 15,姓名: king 学号: 20,姓名: jing 学号: 20,姓名: king 学号: 25,姓名: jing } } classStudent{ privateintnum; privateStringname; publicStudent(intnum,Stringname) { this.num=num; this.name=name; } 下边在这里定义的equals和hashCode publicbooleanequals(Objectobj){ Studentst=(Student)obj; if(this.num==st.num&&this.name.equals(st.name)) returntrue; returnfalse; } publicinthashCode(){ returnthis.num; } publicStringtoString() { return"学号: "+num+",姓名: "+name; } } TreeSet类: 使用元素的自然顺序对元素进行排序,或者根据创建set时提供的Comparator进行排序 ⏹TreeSet是一个有序集合,TreeSet中元素将按照升序排列,缺省是按照自然顺序进行排列,意味着TreeSet中元素要实现Comparable接口。 publicclassTestTreeSet { publicstaticvoidmain(String[]args) { TreeSetts=newTreeSet(); ts.add(newStudent(3,"Jerry")); ts.add(newStudent(1,"Mary")); ts.add(newStudent(2,"Billy")); ts.add(newStudent(3,"Jerry")); Iteratoriter=ts.iterator(); while(iter.hasNext()) { System.out.println(iter.next()); } } } //此中就不用在写equals方法和hashCode了,但是下方的student类要实现Comparable接口,里边的compareTo方法要重写,看下边 classStudentimplementsComparable { privateintnum; privateStringname; publicStudent(intnum,Stringname) { this.num=num; this.name=name; } publicStringtoString() { return"学号: "+num+",姓名: "+name; } publicintcompareTo(Objectobj) { Studentstu=(Student)obj;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 集合