欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > PPT文档下载  

    Java13第十一章-集合框架和泛型.ppt

    • 资源ID:6509216       资源大小:455.50KB        全文页数:63页
    • 资源格式: PPT        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Java13第十一章-集合框架和泛型.ppt

    第十一章,JAVA集合框架和泛型机制,回顾与作业点评,JAVA的异常处理机制try catch finally处理异常throw 和throws引发异常getMessage和 printSackTrace方法自定义异常类,本章任务,掌握JAVA集合框架掌握List Map Set接口掌握容器的泛型操作 掌握Comparable接口掌握equals和 hashCode方法的理解,知识要点,JAVA集合框架List Map Set接口容器的泛型操作 Comparable接口equals和 hashCode方法的理解,11.1 JAVA集合框架,1,接口,Collection,List,Map,2,具体类,ArrayList,LinkedList,HashMap,3,算法,Java集合框架为我们提供了一套性能优良、使用方便的接口和类,它们位于java.util包中我们不必再重新发明轮子,只需学会如何使用它们,就可处理实际应用中问题,Collections,提供了对集合进行排序、遍历等多种算法实现,Set,TreeSet,HashSet,TreeMap,Hashtable,11.2 Collection 接口:表示一组对象,称为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,iterator iteratorIterator iterator()返回在此 collection 的元素上进行迭代的迭代器。关于元素返回的顺序没有任何保证(除非此 collection 是某个能提供保证顺序的类实例)。指定者:接口 Iterable 中的 iterator在此 collection 的元素上进行迭代的 Iterator,11.3 Set接口实现类:没有重复元素,包括HashSet TreeSet LinkedHashSet,11.3.1 实现类HashSet:无序存放数据,根据元素的哈希码存放。,import java.util.HashSet;import java.util.Iterator;public static void main(String args)HashSet hs=new HashSet();hs.add(zxx);hs.add(zahx);hs.add(zyj);hs.add(xmh);hs.add(zah);Iterator it=hs.iterator();while(it.hasNext()System.out.println(it.next();,运行结果:没按顺序显示,zxxzahzahxxmhzyj,public class Student private String name;/姓名private int age;/年龄 public Student(String name,int age)this.name=name;this.age=age;public String toString()return 姓名为:+name+年龄为:+age;public int hashCode()return age*name.hashCode();public boolean equals(Object o)Student s=(Student)o;return age=s.age,public static void main(String args)HashSet hs=new HashSet();hs.add(new Student(28,zah);hs.add(new Student(31,xmh);hs.add(new Student(30,zyj);hs.add(new Student(28,zah);hs.add(new Student(33,zxx);hs.add(null);hs.add(null);Iterator it=hs.iterator();while(it.hasNext()System.out.println(it.next();,运行结果:允许null,但没有重复元素,实现类LinkedHashSet:根据元素的哈希码存放数据,同时用链表记录元素的加入顺序。import java.util.Date;import java.util.LinkedHashSet;import java.util.Set;public class LinkedHashSetTest public static void main(String args)Set linkHashSet=new LinkedHashSet();Student1 stu=new Student1(18,0803预热班,1,张三,85.5,new Date();Student1 stu2=new Student1(23,0803预热班,2,李四,45.0,new Date();Student1 stu3=new Student1(25,0803预热班,3,王五,65.5,new Date();Student1 stu4=new Student1(25,0803预热班,3,老六,65.5,new Date();linkHashSet.add(stu3);linkHashSet.add(stu4);linkHashSet.add(stu);/记录HashCode码顺序,按照顺序查找出来。linkHashSet.add(stu2);for(Student1 temp:linkHashSet)System.out.println(temp);,id为:25 姓名为:0803预热班 年龄为:3 年级为:王五 分数为:65.5日期为:Wed Jun 06 16:39:21 CST 2012hashCode为1786041561hashCode()=458823894 id为:25 姓名为:0803预热班 年龄为:3 年级为:老六 分数为:65.5日期为:Wed Jun 06 16:39:21 CST 2012hashCode为458823894hashCode()=1248097552 id为:18 姓名为:0803预热班 年龄为:1 年级为:张三 分数为:85.5日期为:Wed Jun 06 16:39:21 CST 2012hashCode为1248097552hashCode()=-1045795214 id为:23 姓名为:0803预热班 年龄为:2 年级为:李四 分数为:45.0日期为:Wed Jun 06 16:39:21 CST 2012hashCode为-1045795214,运行结果:,11.3.3 实现类 TreeSet:使用红黑树结构对加入的元素进行排序存放。,public class TreeSetTest public static void main(String args)Set treeSet=new TreeSet();/子类对象给接口引用 Student1 stu=new Student1(18,0803预热班,1,张三,85.5,new Date();Student1 stu2=new Student1(23,0803预热班,2,李四,45.0,new Date();Student1 stu3=new Student1(25,0803预热班,3,王五,65.5,new Date();Student1 stu4=new Student1(25,0803预热班,4,王五,65.5,new Date();treeSet.add(stu);treeSet.add(stu2);treeSet.add(stu3);/添加 treeSet.add(stu4);for(Student1 temp:treeSet)/循环打印,只要是通过迭代器能迭代出来的都可以通过增强for循环遍历出来 System.out.println(temp);/Iterator iter=treeSet.iterator();/while(iter.hasNext()/System.out.println(iter.next()+t);/,运行结果:,id为:18 姓名为:0803预热班 年龄为:1 年级为:张三 分数为:85.5日期为:Wed Jun 06 16:42:49 CST 2012hashCode为1248097552hashCode()=1786041561 id为:25 姓名为:0803预热班 年龄为:3 年级为:王五 分数为:65.5日期为:Wed Jun 06 16:42:49 CST 2012hashCode为1786041561hashCode()=-1045795214 id为:23 姓名为:0803预热班 年龄为:2 年级为:李四 分数为:45.0日期为:Wed Jun 06 16:42:49 CST 2012hashCode为-1045795214,class Student implements Comparable private String name;/姓名private int age;/年龄 public Student(String name,int age)this.name=name;this.age=age;public String toString()return 姓名为:+name+年龄为:+age;public int hashCode()return age*name.hashCode();public boolean equals(Object o)Student s=(Student)o;return age=s.age,public static void main(String args)TreeSet hs=new TreeSet();hs.add(new Student(18,zxx);hs.add(new Student(25,xmh);hs.add(new Student(23,zyj);hs.add(new Student(26,zah);/hs.add(null);Iterator it=hs.iterator();while(it.hasNext()System.out.println(it.next();,运行结果:按年龄大小排序 姓名为:zxx 年龄为:18 姓名为:zyj 年龄为:23 姓名为:xmh 年龄为:25 姓名为:zah 年龄为:26,11.4 List接口实现类:List接口继承了Collection接口,允许存在重复项的有序集合。,List接口的实现类,ArrayList实现了长度可变的数组,在内存中分配连续的空间。遍历元素和随机访问元素的效率比较高LinkedList采用链表存储方式。插入、删除元素时效率比较高,List,ArrayList,LinkedList,11.4.1 实现类ArrayList:支持可随需要而增长的动态数组。,public static void main(String args)Collection c1=new ArrayList();for(int i=0;i5;i+)c1.add(new Integer(i);System.out.println(c1:+c1);Collection c2=new ArrayList();c2.addAll(c1);c2.remove(new Integer(3);c2.add(hehe);System.out.println(c2:+c2);Iterator it=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的特殊方法,public static void main(String args)List list=new LinkedList();/List list1=new Vector();Student2 stu=new Student2(18,0803预热班,1,张三,85.5);Student2 stu2=new Student2(23,0803预热班,2,李四,45.0);Student2 stu3=new Student2(25,0803预热班,3,王五,65.5);Student2 stu4=new Student2(25,0803预热班,3,王五,65.5);list.add(stu);list.add(stu2);list.add(stu3);list.add(stu4);System.out.println(之前的元素为:);for(Student2 temp:list)System.out.println(temp);list.remove(2);/把第二个位置移去System.out.println(移去:);System.out.println(之后的元素为:);list.add(2,new Student2(100,老年班,100,菜10,500.00);int size=list=null?0:list.size();for(int i=0;i size;i+)System.out.println(list.get(i);,运行结果:之前的元素为:name=张三,age=18,grade=0803预热班,score=85.5name=李四,age=23,grade=0803预热班,score=45.0name=王五,age=25,grade=0803预热班,score=65.5name=王五,age=25,grade=0803预热班,score=65.5移去:之后的元素为:name=张三,age=18,grade=0803预热班,score=85.5name=李四,age=23,grade=0803预热班,score=45.0name=菜10,age=100,grade=老年班,score=500.0name=王五,age=25,grade=0803预热班,score=6,public class LinkedListTest public static void main(String args)List list=new LinkedList();/List list1=new Vector();Student2 stu=new Student2(18,0803预热班,1,张三,85.5);Student2 stu2=new Student2(23,0803预热班,2,李四,45.0);Student2 stu3=new Student2(25,0803预热班,3,王五,65.5);Student2 stu4=new Student2(25,0803预热班,3,王五,65.5);list.add(stu);list.add(stu2);list.add(stu3);list.add(stu4);System.out.println(之前的元素为:);for(Student2 temp:list)System.out.println(temp);list.remove(2);/把第二个位置移去System.out.println(移去:);System.out.println(之后的元素为:);list.add(2,new Student2(100,老年班,100,菜10,500.00);int size=list=null?0:list.size();for(int i=0;i size;i+)System.out.println(list.get(i);,之前的元素为:name=张三,age=18,grade=0803预热班,score=85.5name=李四,age=23,grade=0803预热班,score=45.0name=王五,age=25,grade=0803预热班,score=65.5name=王五,age=25,grade=0803预热班,score=65.5移去:之后的元素为:name=张三,age=18,grade=0803预热班,score=85.5name=李四,age=23,grade=0803预热班,score=45.0name=菜10,age=100,grade=老年班,score=500.0name=王五,age=25,grade=0803预热班,score=65.5,实现类Vector:提供了实现可增长数组的功能。,import java.util.Vector;import java.util.Enumeration;public class VectorTestpublic static void main(String args)Vector v=new Vector();v.add(向量);v.add(123);v.add(abc);Enumeration e=v.elements();while(e.hasMoreElements()/类似于迭代System.out.println(-+e.nextElement();,运行结果:,-向量-123-abc,11.5Map接口Map接口专门处理键值映射数据的存储,可以根据键实现对值的操作,Map接口,Map接口常用方法,实现类HahMap:基于哈希表的Map接口的实现,允许null值,public class HashMapTest public static void main(String args)Map map=new HashMap();Student3 stu=new Student3(18,0802预热班,1,张三,85.5);Student3 stu2=new Student3(23,0802预热班,2,李四,45.0);Student3 stu3=new Student3(25,0802预热班,3,王五,65.5);Student3 stu4=new Student3(25,0802预热班,3,王五,65.5);map.put(soft001,stu);/往Map中存放“key-value”对map.put(soft002,stu2);map.put(soft003,stu3);map.put(soft001,null);,实现类HahMap:基于哈希表的Map接口的实现,允许null值,map.put(null,stu4);System.out.println(map.size();Student3 student=map.get(soft003);/根据键取对应的值System.out.println(student);/遍历/获取键的Set集Set keys=map.keySet();Iterator it=keys.iterator();/用Iteratorwhile(it.hasNext()String key=it.next();Student3 temp=map.get(key);System.out.println(key=+key+,value-+temp);for(String key:keys)/用forEachStudent3 temp=map.get(key);System.out.println(key=+key+,value-+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值,public class LinkedHashMapTest public static void main(String args)Map map=new LinkedHashMap();Student3 stu=new Student3(18,0802预热班,1,张三,85.5);Student3 stu2=new Student3(23,0802预热班,2,李四,45.0);Student3 stu3=new Student3(25,0802预热班,3,王五,65.5);Student3 stu4=new Student3(25,0802预热班,3,王五,65.5);map.put(soft001,stu);/往Map中存放“key-value”对map.put(soft002,stu2);map.put(soft003,stu3);,实现LinkedHashMap:是HashMap的子类,可以依照插入的顺序来排列元素,增、删、改效率高,map.put(soft001,null);map.put(null,stu4);System.out.println(map.size();Student3 student=map.get(soft003);/根据键取对应的值/System.out.println(student);Set keys=map.keySet();/遍历/获取键的Set集Iterator it=keys.iterator();/用Iteratorwhile(it.hasNext()String key=it.next();Student3 temp=map.get(key);System.out.println(key=+key+,value-+temp);,实现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,运行结果:,实现类TreeMap,public class TreeMapTestSuppressWarnings(unchecked)public static void main(String args)TreeMap treemap=new TreeMap();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();/返回第一个键 Set set=treemap.keySet();Iterator iterator=set.iterator();while(iterator.hasNext()System.out.print(treemap.get(iterator.next()+;);,运行结果:,TreeMap:0=d,1=b,2=a,3=c0d;b;a;c;,实现类Properties:表示一个持久的属性集,可以保存在流中或从流中加载。,public class PropertiesTest public static void main(String args)/方法链风格 方法返回的必须是一个对象InputStream is=Thread.currentThread().getContextClassLoader().getResourceAsStream(config.properties);/属性文件与PropertiesTest类应该放在同一个目录下Properties prop=new Properties();try/从流中加载数据prop.load(is);catch(IOException e)e.printStackTrace();String name=prop.getProperty(name);System.out.println(name=+name);String pwd=prop.getProperty(password);System.out.println(password=+pwd);,运行结果:name=spiritpassword=abc123,11.6 Collections类:是一个工具类,对集合进行操作,SuppressWarnings(unchecked)public class CollectionsTestpublic static void printView(List arrayList)Iterator it=arrayList.iterator();while(it.hasNext()System.out.print(+it.next();System.out.println();public static void main(String args)List list=new ArrayList();/泛型操作Student stu1=new Student(18,zxx,85);Student stu2=new Student(23,zyj,81);Student stu3=new Student(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);/List newlst=new ArrayList(list.size();List newlst=new ArrayList(Arrays.asList(new Objectlist.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(Object obj)放入List中,认为只是Object类型通过get(int index)取出List中元素时必须进行强制类型转换,繁琐而且容易出现异常使用Map的put(Object key,Object value)和get(Object key)存取对象时存在同样问题使用Iterator的next()方法获取元素时存在同样问题JDK5.0中通过引入泛型有效的解决了这个问题JDK5.0使用泛型改写了集合框架中的所有接口和类,泛型:是在定义(类、方法、形式参数、成员变量)的时候,指定他为通用类型,也就是数据类型可以是任意的类型,具体调用的时候,要将通用类型转换成指定的类型来使用。1.泛型的声明Class TestGenK V代表类型List I=new ArrayList(),2.泛型的使用 1)消除类型转换,import java.util.*;public class TestGenerics1 public static void main(String args)List l=new ArrayList();l.add(ABC);l.add(DEF);String str=l.get(0);/使用泛型后,获得对象时不用进行强制类型转换/*错误,试图将Integer和Double类型的对象放入指定存放String类型对象的 集合中 l.add(1);l.add(1.5);*/for(String s:l)/for-each循环(集合/数组中元素类型 变量:集合/数组名)System.out.println(s);Map map=new HashMap();map.put(1,Huxz);,map.put(2,Liucy);map.put(3,TangLiang);Set keys=map.keySet();for(Integer i:keys)String value=map.get(i);System.out.println(i+-+value);List list=new ArrayList();list.add(10);list.add(1.5);/*List list2;List list3=new ArrayList();*list2=list3;*/,运行结果:ABCDEF1-Huxz2-Liucy3-TangLiang,2)自动解包装与自动包装的功能,import java.util.*;import static java.lang.System.*;public class TestGenerics2 public static void main(String args)List l1=new ArrayList();l1.add(中国);List l2=new ArrayList();l2.add(88);List l3=new ArrayList();l3.add(8.8);List l4=new ArrayList();l4.add(99);List l5=new ArrayList();,2)自动解包装与自动包装的功能,l5.add(100.8);print(l1);/String类型的泛型对象print(l2);/Object类型的泛型对象print(l3);/Number类型的泛型对象print(l4);/Integer类型的泛型对象print(l5);/Double类型的泛型对象/方法参数中使用集合时不指定泛型,默认为 public static void print(List list)for(Object o:list)System.out.println(o);,运行结果:中国888.899100.8,3)限制泛型中类型参数的范围允许所有泛型的引用调用只允许泛型为Number及 Number子类的引用调用只允许泛型为Number及 Number父类的引用调用只允许泛型为实现Comparable接口的实现类的引用调用泛型方法:也称为多态方法,格式为:修饰符 泛型 返回类型 方法名 参数表 抛出的异常泛型类:泛型的通配符与泛型方法的通配符约束一致。,public class TestGenerics3 List list=null;public static void main(String args)List l1=new ArrayList();List l2=new ArrayList();List l3=new ArrayList();List l4=new ArrayList();List l5=new ArrayList();String a1=new String10;Object a2=new Object10;Number a3=new Number10;Integer a4=new Integer10;Double a5=new Double10;,copyFromArray(l1,a1);copyFromArray(l2,a2);copyFromArray(l3,a3);copyFromArray(l4,a4);copyFromArray(l5,a5);/修饰符 泛型 返回类型 方法名 参数表 抛出的异常 public static void cop

    注意事项

    本文(Java13第十一章-集合框架和泛型.ppt)为本站会员(牧羊曲112)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开