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

上传人:xin****828 文档编号:15472840 上传时间:2020-08-12 格式:PPT 页数:63 大小:946KB
返回 下载 相关 举报
Java13第十一章-集合框架和泛型.ppt_第1页
第1页 / 共63页
Java13第十一章-集合框架和泛型.ppt_第2页
第2页 / 共63页
Java13第十一章-集合框架和泛型.ppt_第3页
第3页 / 共63页
点击查看更多>>
资源描述
第十一章,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 iterator Iterator 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(); ,运行结果:没按顺序显示,zxx zah zahx xmh zyj,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,但没有重复元素,11.3.2实现类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为1786041561 hashCode()=458823894 id为: 25 姓名为:0803预热班 年龄为:3 年级为: 老六 分数为:65.5 日期为:Wed Jun 06 16:39:21 CST 2012hashCode为458823894 hashCode()=1248097552 id为: 18 姓名为:0803预热班 年龄为:1 年级为: 张三 分数为:85.5 日期为:Wed Jun 06 16:39:21 CST 2012hashCode为1248097552 hashCode()=-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为1248097552 hashCode()=1786041561 id为: 25 姓名为:0803预热班 年龄为:3 年级为: 王五 分数为:65.5日期为:Wed Jun 06 16:42:49 CST 2012hashCode为1786041561 hashCode()=-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, 4 c2:0, 1, 2, 4, hehe 0 1 2 4 hehe,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.5 name=李四,age=23,grade=0803预热班,score=45.0 name=王五,age=25,grade=0803预热班,score=65.5 name=王五,age=25,grade=0803预热班,score=65.5 移去: 之后的元素为: name=张三,age=18,grade=0803预热班,score=85.5 name=李四,age=23,grade=0803预热班,score=45.0 name=菜10,age=100,grade=老年班,score=500.0 name=王五,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.5 name=李四,age=23,grade=0803预热班,score=45.0 name=王五,age=25,grade=0803预热班,score=65.5 name=王五,age=25,grade=0803预热班,score=65.5 移去: 之后的元素为: name=张三,age=18,grade=0803预热班,score=85.5 name=李四,age=23,grade=0803预热班,score=45.0 name=菜10,age=100,grade=老年班,score=500.0 name=王五,age=25,grade=0803预热班,score=65.5,11.4.3实现类Vector:提供了实现可增长数组的功能。,import java.util.Vector; import java.util.Enumeration; public class VectorTest public 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接口常用方法,11.5.1实现类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);,11.5.1实现类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();/用Iterator while (it.hasNext() String key = it.next(); Student3 temp = map.get(key); System.out.println(key= + key + , value- + temp); for (String key : keys) /用forEach Student3 temp = map.get(key); System.out.println(key= + key + , value- + temp); ,运行结果:,4 name=王五,age=25,grade=0802预热班,score=65.5 key=soft003, value-name=王五,age=25,grade=0802预热班,score=65.5 key=soft002, value-name=李四,age=23,grade=0802预热班,score=45.0 key=null, value-name=王五,age=25,grade=0802预热班,score=65.5 key=soft001, value-null key=soft003, value-name=王五,age=25,grade=0802预热班,score=65.5 key=soft002, value-name=李四,age=23,grade=0802预热班,score=45.0 key=null, value-name=王五,age=25,grade=0802预热班,score=65.5 key=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);,11.5.2实现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();/ 用Iterator while (it.hasNext() String key = it.next(); Student3 temp = map.get(key); System.out.println(key= + key + , value- + temp); ,11.5.2实现LinkedHashMap:是HashMap的子类,可以依照插入的顺序来排列元素,增、删、改效率高,4 key=soft001, value-null key=soft002, value-name=李四,age=23,grade=0802预热班,score=45.0 key=soft003, value-name=王五,age=25,grade=0802预热班,score=65.5 key=null, value-name=王五,age=25,grade=0802预热班,score=65.5,运行结果:,11.5.3实现类TreeMap,public class TreeMapTest SuppressWarnings(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=c 0 d;b;a;c;,11.5.4实现类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=spirit password=abc123,11.6 Collections类:是一个工具类,对集合进行操作,SuppressWarnings(unchecked) public class CollectionsTest public 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 TestGen K 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; */ ,运行结果: ABC DEF 1-Huxz 2-Liucy 3-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); ,运行结果: 中国 88 8.8 99 100.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 copyFromArray(List l, T a) for (T o : a) l.add(o); ,public class TestGenerics4 public static void main(String args) MyClass2 test = new MyClass2(); MyClass2 s = new MyClass2(); s.method1(231); MyClass2 s2 = new MyClass2(); s2.method1(231); class MyClass2 /定义了一个泛型类 public void method1(T t) System.out.println(t); public T method2() return null; ,泛型类的注意点: 1.静态方法中不能使用类的泛型 2.不要创建泛型类的对象,如果不是接口或抽象类则可以 3.不能在catch子句中使用泛型。,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!