




已阅读5页,还剩58页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第十一章,JAVA集合框架和泛型机制,回顾与作业点评,JAVA的异常处理机制trycatchfinally处理异常throw和throws引发异常getMessage和printSackTrace方法自定义异常类,本章任务,掌握JAVA集合框架掌握ListMapSet接口掌握容器的泛型操作掌握Comparable接口掌握equals和hashCode方法的理解,知识要点,JAVA集合框架ListMapSet接口容器的泛型操作Comparable接口equals和hashCode方法的理解,11.1JAVA集合框架,1,接口,Collection,List,Map,2,具体类,ArrayList,LinkedList,HashMap,3,算法,Java集合框架为我们提供了一套性能优良、使用方便的接口和类,它们位于java.util包中我们不必再重新发明轮子,只需学会如何使用它们,就可处理实际应用中问题,Collections,提供了对集合进行排序、遍历等多种算法实现,Set,TreeSet,HashSet,TreeMap,Hashtable,11.2Collection接口:表示一组对象,称为Collection接口元素,Collection接口存储一组不唯一,无序的对象List接口存储一组不唯一,有序(插入顺序)的对象Set接口存储一组唯一,无序的对象Map接口存储一组键值对象,提供key到value的映射,Collection,List,Map,Set,Collection接口,java.util接口Collection所有超级接口:Iterable所有已知子接口:BeanContext,BeanContextServices,BlockingDeque,BlockingQueue,Deque,List,NavigableSet,Queue,Set,SortedSet所有已知实现类:AbstractCollection,AbstractList,AbstractQueue,AbstractSequentialList,AbstractSet,ArrayBlockingQueue,ArrayDeque,ArrayList,AttributeList,BeanContextServicesSupport,BeanContextSupport,ConcurrentLinkedQueue,ConcurrentSkipListSet,CopyOnWriteArrayList,CopyOnWriteArraySet,DelayQueue,EnumSet,HashSet,JobStateReasons,LinkedBlockingDeque,LinkedBlockingQueue,LinkedHashSet,LinkedList,PriorityBlockingQueue,PriorityQueue,RoleList,RoleUnresolvedList,Stack,SynchronousQueue,TreeSet,Vector,iteratoriteratorIteratoriterator()返回在此collection的元素上进行迭代的迭代器。关于元素返回的顺序没有任何保证(除非此collection是某个能提供保证顺序的类实例)。指定者:接口Iterable中的iterator在此collection的元素上进行迭代的Iterator,11.3Set接口实现类:没有重复元素,包括HashSetTreeSetLinkedHashSet,11.3.1实现类HashSet:无序存放数据,根据元素的哈希码存放。,importjava.util.HashSet;importjava.util.Iterator;publicstaticvoidmain(Stringargs)HashSeths=newHashSet();hs.add(zxx);hs.add(zahx);hs.add(zyj);hs.add(xmh);hs.add(zah);Iteratorit=hs.iterator();while(it.hasNext()System.out.println(it.next();,运行结果:没按顺序显示,zxxzahzahxxmhzyj,publicclassStudentprivateStringname;/姓名privateintage;/年龄publicStudent(Stringname,intage)=name;this.age=age;publicStringtoString()return姓名为:+name+年龄为:+age;publicinthashCode()returnage*name.hashCode();publicbooleanequals(Objecto)Students=(Student)o;returnage=s.age,publicstaticvoidmain(Stringargs)HashSeths=newHashSet();hs.add(newStudent(28,zah);hs.add(newStudent(31,xmh);hs.add(newStudent(30,zyj);hs.add(newStudent(28,zah);hs.add(newStudent(33,zxx);hs.add(null);hs.add(null);Iteratorit=hs.iterator();while(it.hasNext()System.out.println(it.next();,运行结果:允许null,但没有重复元素,11.3.2实现类LinkedHashSet:根据元素的哈希码存放数据,同时用链表记录元素的加入顺序。importjava.util.Date;importjava.util.LinkedHashSet;importjava.util.Set;publicclassLinkedHashSetTestpublicstaticvoidmain(Stringargs)SetlinkHashSet=newLinkedHashSet();Student1stu=newStudent1(18,0803预热班,1,张三,85.5,newDate();Student1stu2=newStudent1(23,0803预热班,2,李四,45.0,newDate();Student1stu3=newStudent1(25,0803预热班,3,王五,65.5,newDate();Student1stu4=newStudent1(25,0803预热班,3,老六,65.5,newDate();linkHashSet.add(stu3);linkHashSet.add(stu4);linkHashSet.add(stu);/记录HashCode码顺序,按照顺序查找出来。linkHashSet.add(stu2);for(Student1temp:linkHashSet)System.out.println(temp);,id为:25姓名为:0803预热班年龄为:3年级为:王五分数为:65.5日期为:WedJun0616:39:21CST2012hashCode为1786041561hashCode()=458823894id为:25姓名为:0803预热班年龄为:3年级为:老六分数为:65.5日期为:WedJun0616:39:21CST2012hashCode为458823894hashCode()=1248097552id为:18姓名为:0803预热班年龄为:1年级为:张三分数为:85.5日期为:WedJun0616:39:21CST2012hashCode为1248097552hashCode()=-1045795214id为:23姓名为:0803预热班年龄为:2年级为:李四分数为:45.0日期为:WedJun0616:39:21CST2012hashCode为-1045795214,运行结果:,11.3.3实现类TreeSet:使用红黑树结构对加入的元素进行排序存放。,publicclassTreeSetTestpublicstaticvoidmain(Stringargs)SettreeSet=newTreeSet();/子类对象给接口引用Student1stu=newStudent1(18,0803预热班,1,张三,85.5,newDate();Student1stu2=newStudent1(23,0803预热班,2,李四,45.0,newDate();Student1stu3=newStudent1(25,0803预热班,3,王五,65.5,newDate();Student1stu4=newStudent1(25,0803预热班,4,王五,65.5,newDate();treeSet.add(stu);treeSet.add(stu2);treeSet.add(stu3);/添加treeSet.add(stu4);for(Student1temp:treeSet)/循环打印,只要是通过迭代器能迭代出来的都可以通过增强for循环遍历出来System.out.println(temp);/Iteratoriter=treeSet.iterator();/while(iter.hasNext()/System.out.println(iter.next()+t);/,运行结果:,id为:18姓名为:0803预热班年龄为:1年级为:张三分数为:85.5日期为:WedJun0616:42:49CST2012hashCode为1248097552hashCode()=1786041561id为:25姓名为:0803预热班年龄为:3年级为:王五分数为:65.5日期为:WedJun0616:42:49CST2012hashCode为1786041561hashCode()=-1045795214id为:23姓名为:0803预热班年龄为:2年级为:李四分数为:45.0日期为:WedJun0616:42:49CST2012hashCode为-1045795214,classStudentimplementsComparableprivateStringname;/姓名privateintage;/年龄publicStudent(Stringname,intage)=name;this.age=age;publicStringtoString()return姓名为:+name+年龄为:+age;publicinthashCode()returnage*name.hashCode();publicbooleanequals(Objecto)Students=(Student)o;returnage=s.age,publicstaticvoidmain(Stringargs)TreeSeths=newTreeSet();hs.add(newStudent(18,zxx);hs.add(newStudent(25,xmh);hs.add(newStudent(23,zyj);hs.add(newStudent(26,zah);/hs.add(null);Iteratorit=hs.iterator();while(it.hasNext()System.out.println(it.next();,运行结果:按年龄大小排序姓名为:zxx年龄为:18姓名为:zyj年龄为:23姓名为:xmh年龄为:25姓名为:zah年龄为:26,11.4List接口实现类:List接口继承了Collection接口,允许存在重复项的有序集合。,List接口的实现类,ArrayList实现了长度可变的数组,在内存中分配连续的空间。遍历元素和随机访问元素的效率比较高LinkedList采用链表存储方式。插入、删除元素时效率比较高,List,ArrayList,LinkedList,11.4.1实现类ArrayList:支持可随需要而增长的动态数组。,publicstaticvoidmain(Stringargs)Collectionc1=newArrayList();for(inti=0;i5;i+)c1.add(newInteger(i);System.out.println(c1:+c1);Collectionc2=newArrayList();c2.addAll(c1);c2.remove(newInteger(3);c2.add(hehe);System.out.println(c2:+c2);Iteratorit=c2.iterator();while(it.hasNext()System.out.println(it.next();,运行结果:,c1:0,1,2,3,4c2:0,1,2,4,hehe0124hehe,11.4.2实现类LinkedList:提供一个链接列表数据结构,便于插入、删除。,LinkedList的特殊方法,publicstaticvoidmain(Stringargs)Listlist=newLinkedList();/Listlist1=newVector();Student2stu=newStudent2(18,0803预热班,1,张三,85.5);Student2stu2=newStudent2(23,0803预热班,2,李四,45.0);Student2stu3=newStudent2(25,0803预热班,3,王五,65.5);Student2stu4=newStudent2(25,0803预热班,3,王五,65.5);list.add(stu);list.add(stu2);list.add(stu3);list.add(stu4);System.out.println(之前的元素为:);for(Student2temp:list)System.out.println(temp);list.remove(2);/把第二个位置移去System.out.println(移去:);System.out.println(之后的元素为:);list.add(2,newStudent2(100,老年班,100,菜10,500.00);intsize=list=null?0:list.size();for(inti=0;i+temp);,运行结果:,4name=王五,age=25,grade=0802预热班,score=65.5key=soft003,value-name=王五,age=25,grade=0802预热班,score=65.5key=soft002,value-name=李四,age=23,grade=0802预热班,score=45.0key=null,value-name=王五,age=25,grade=0802预热班,score=65.5key=soft001,value-nullkey=soft003,value-name=王五,age=25,grade=0802预热班,score=65.5key=soft002,value-name=李四,age=23,grade=0802预热班,score=45.0key=null,value-name=王五,age=25,grade=0802预热班,score=65.5key=soft001,value-null,Vector和ArrayList的异同实现原理相同,功能相同,很多情况下可以互用两者的主要区别如下Vector线程安全,ArrayList重速度轻安全,线程非安全长度需增长时,Vector默认增长一倍,ArrayList增长50%Hashtable和HashMap的异同实现原理相同,功能相同,在很多情况下可以互用两者的主要区别如下Hashtable继承Dictionary类,HashMap实现Map接口Hashtable线程安全,HashMap线程非安全Hashtable不允许null值,HashMap允许null值,publicclassLinkedHashMapTestpublicstaticvoidmain(Stringargs)Mapmap=newLinkedHashMap();Student3stu=newStudent3(18,0802预热班,1,张三,85.5);Student3stu2=newStudent3(23,0802预热班,2,李四,45.0);Student3stu3=newStudent3(25,0802预热班,3,王五,65.5);Student3stu4=newStudent3(25,0802预热班,3,王五,65.5);map.put(soft001,stu);/往Map中存放“key-value”对map.put(soft002,stu2);map.put(soft003,stu3);,11.5.2实现LinkedHashMap:是HashMap的子类,可以依照插入的顺序来排列元素,增、删、改效率高,map.put(soft001,null);map.put(null,stu4);System.out.println(map.size();Student3student=map.get(soft003);/根据键取对应的值/System.out.println(student);Setkeys=map.keySet();/遍历/获取键的Set集Iteratorit=keys.iterator();/用Iteratorwhile(it.hasNext()Stringkey=it.next();Student3temp=map.get(key);System.out.println(key=+key+,value-+temp);,11.5.2实现LinkedHashMap:是HashMap的子类,可以依照插入的顺序来排列元素,增、删、改效率高,4key=soft001,value-nullkey=soft002,value-name=李四,age=23,grade=0802预热班,score=45.0key=soft003,value-name=王五,age=25,grade=0802预热班,score=65.5key=null,value-name=王五,age=25,grade=0802预热班,score=65.5,运行结果:,11.5.3实现类TreeMap,publicclassTreeMapTestSuppressWarnings(unchecked)publicstaticvoidmain(Stringargs)TreeMaptreemap=newTreeMap();treemap.put(0,d);/指定键值,如果映射以前包含一个此键的映射关系,那么将替换原值treemap.put(2,a);treemap.put(1,b);treemap.put(3,c);System.out.println(nTreeMap:);/可以对键排序System.out.println(treemap);System.out.println(treemap.firstKey();/返回第一个键Setset=treemap.keySet();Iteratoriterator=set.iterator();while(iterator.hasNext()System.out.print(treemap.get(iterator.next()+;);,运行结果:,TreeMap:0=d,1=b,2=a,3=c0d;b;a;c;,11.5.4实现类Properties:表示一个持久的属性集,可以保存在流中或从流中加载。,publicclassPropertiesTestpublicstaticvoidmain(Stringargs)/方法链风格方法返回的必须是一个对象InputStreamis=Thread.currentThread().getContextClassLoader().getResourceAsStream(perties);/属性文件与PropertiesTest类应该放在同一个目录下Propertiesprop=newProperties();try/从流中加载数据prop.load(is);catch(IOExceptione)e.printStackTrace();Stringname=prop.getProperty(name);System.out.println(name=+name);Stringpwd=prop.getProperty(password);System.out.println(password=+pwd);,运行结果:name=spiritpassword=abc123,11.6Collections类:是一个工具类,对集合进行操作,SuppressWarnings(unchecked)publicclassCollectionsTestpublicstaticvoidprintView(ListarrayList)Iteratorit=arrayList.iterator();while(it.hasNext()System.out.print(+it.next();System.out.println();publicstaticvoidmain(Stringargs)Listlist=newArrayList();/泛型操作Studentstu1=newStudent(18,zxx,85);Studentstu2=newStudent(23,zyj,81);Studentstu3=newStudent(26,xmh,92);list.add(stu1);list.add(stu2);list.add(stu3);System.out.println(初始list内容);printView(list);,Collections.shuffle(list);System.out.println(混淆后list内容);printView(list);Collections.sort(list);System.out.println(排序后list内容);printView(list);Collections.reverse(list);System.out.println(逆序后list内容);printView(list);/Listnewlst=newArrayList(list.size();Listnewlst=newArrayList(Arrays.asList(newObjectlist.size();Collections.copy(newlst,list);System.out.println(复制list内容);printView(newlst);,运行结果:初始list内容姓名为:zxx年龄为:18姓名为:zyj年龄为:23姓名为:xmh年龄为:26混淆后list内容姓名为:zxx年龄为:18姓名为:xmh年龄为:26姓名为:zyj年龄为:23排序后list内容姓名为:zxx年龄为:18姓名为:zyj年龄为:23姓名为:xmh年龄为:26逆序后list内容姓名为:xmh年龄为:26姓名为:zyj年龄为:23姓名为:zxx年龄为:18复制list内容姓名为:xmh年龄为:26姓名为:zyj年龄为:23姓名为:zxx年龄为:18,11.7泛型概述,把任何类型对象通过add(Objectobj)放入List中,认为只是Object类型通过get(intindex)取出List中元素时必须进行强制类型转换,繁琐而且容易出现异常使用Map的put(Objectkey,Objectvalue)和get(Objectkey)存取对象时存在同样问题使用Iterator的next()方法获取元素时存在同样问题JDK5.0中通过引入泛型有效的解决了这个问题JDK5.0使用泛型改写了集合框架中的所有接口和类,泛型:是在定义(类、方法、形式参数、成员变量)的时候,指定他为通用类型,也就是数据类型可以是任意的类型,具体调用的时候,要将通用类型转换成指定的类型来使用。1.泛型的声明ClassTestGenKV代表类型ListI=newArrayList(),2.泛型的使用1)消除类型转换,importjava.util.*;publicclassTestGenerics1publicstaticvoidmain(Stringargs)Listl=newArrayList();l.add(ABC);l.add(DEF);Stringstr=l.get(0);/使用泛型后,获得对象时不用进行强制类型转换/*错误,试图将Integer和Double类型的对象放入指定存放String类型对象的集合中l.add(1);l.add(1.5);*/for(Strings:l)/for-each循环(集合/数组中元素类型变量:集合/数组名)System.out.println(s);Mapmap=newHashMap();map.put(1,Huxz);,map.put(2,Liucy);map.put(3,TangLiang);Setkeys=map.keySet();for(Integeri:keys)Stringvalue=map.get(i);System.out.println(i+-+value);Listlist=newArrayList();list.add(10);list.add(1.5);/*Listlist2;Listlist3=newArrayList();*list2=list3;*/,运行结果:ABCDEF1-Huxz2-Liucy3-TangLiang,2)自动解包装与自动包装的功能,importjava.util.*;importstaticjava.lang.System.*;publicclassTestGenerics2publicstaticvoidmain(Stringargs)Listl1=newArrayList();l1.add(中国);Listl2=newArrayList();l2.add(88);Listl3=newArrayList();l3.add(8.8);Listl4=newArrayList();l4.add(99);Listl5=newArray
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年湖南益阳市交通投资运营集团有限公司下属子公司公开招聘(第一批)考前自测高频考点模拟试题附答案详解(考试直接用)
- 2025年4月广东深圳市光明区教育局招聘公办幼儿园人员模拟试卷及参考答案详解一套
- 2025湖南湘潭市韶山学校招聘教师15人模拟试卷附答案详解
- 2025湖南新宁县招聘教师30人模拟试卷(含答案详解)
- 2025春季中国太平校园招聘考前自测高频考点模拟试题及一套参考答案详解
- 2025辽宁省机场管理集团有限公司拟聘人员模拟试卷及答案详解1套
- 2025年临沂郯城县技工学校公开招聘教师(26人)考前自测高频考点模拟试题及答案详解(典优)
- 意向金协议书5篇
- 2025中电建宁夏工程有限公司设计管理部笔试题库历年考点版附带答案详解
- 父母外出互助协议8篇
- 2《归园田居》任务式公开课一等奖创新教案(表格式)统编版高中语文必修上册
- 银行文明礼仪课件
- 虚拟电厂运行关键课件
- 敏捷企业组织结构与设计的案例研究
- 光伏储能技术介绍
- 项目合同交付管理办法
- 国企贸易业务管理办法
- 3done入门基础知识课件
- 职业健康卫生培训课件
- 2025年广西专业技术人员继续教育公需科目(三)答案
- 麻醉科设备管理制度
评论
0/150
提交评论