Java第十一章集合框架和泛型.ppt
《Java第十一章集合框架和泛型.ppt》由会员分享,可在线阅读,更多相关《Java第十一章集合框架和泛型.ppt(63页珍藏版)》请在三一办公上搜索。
1、第十一章,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,
2、具体类,ArrayList,LinkedList,HashMap,3,算法,Java集合框架为我们提供了一套性能优良、使用方便的接口和类,它们位于java.util包中我们不必再重新发明轮子,只需学会如何使用它们,就可处理实际应用中问题,Collections,提供了对集合进行排序、遍历等多种算法实现,Set,TreeSet,HashSet,TreeMap,Hashtable,11.2 Collection 接口:表示一组对象,称为Collection接口元素,Collection 接口存储一组不唯一,无序的对象List 接口存储一组不唯一,有序(插入顺序)的对象Set 接口存储一组唯一,无序
3、的对象 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,Ab
4、stractSet,ArrayBlockingQueue,ArrayDeque,ArrayList,AttributeList,BeanContextServicesSupport,BeanContextSupport,ConcurrentLinkedQueue,ConcurrentSkipListSet,CopyOnWriteArrayList,CopyOnWriteArraySet,DelayQueue,EnumSet,HashSet,JobStateReasons,LinkedBlockingDeque,LinkedBlockingQueue,LinkedHashSet,LinkedLi
5、st,PriorityBlockingQueue,PriorityQueue,RoleList,RoleUnresolvedList,Stack,SynchronousQueue,TreeSet,Vector,iterator iteratorIterator iterator()返回在此 collection 的元素上进行迭代的迭代器。关于元素返回的顺序没有任何保证(除非此 collection 是某个能提供保证顺序的类实例)。指定者:接口 Iterable 中的 iterator在此 collection 的元素上进行迭代的 Iterator,11.3 Set接口实现类:没有重复元素,包括
6、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.print
7、ln(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)S
8、tudent 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.hasNex
9、t()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
10、(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码顺序,按照顺序查找出来。l
11、inkHashSet.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
12、 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 ma
13、in(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(s
14、tu);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
15、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 Compara
16、ble 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
17、(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
18、年龄为: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.
19、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:提供一个链接列表数
20、据结构,便于插入、删除。,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预热班
21、,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.siz
22、e();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
23、预热班,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
24、);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.printl
25、n(之后的元素为:);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预热班,s
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 第十一 集合 框架

链接地址:https://www.31ppt.com/p-6510083.html