《JavaJDK6学习笔记第13章.ppt》由会员分享,可在线阅读,更多相关《JavaJDK6学习笔记第13章.ppt(25页珍藏版)》请在三一办公上搜索。
1、第13章,对象容器 Collection类Map类,简介List界面,List界面是接口的子接口Collection界面是子界面在Java SE的API中找不到任何实作Iterator的类别Iterator会根据实际的容器数据结构来迭代元素而容器的数据结构实作方式对外界是隐藏的,package java.lang;import java.util.Iterator;public interface Iterable Iterator iterator();,简介List界面,Collection界面继承了Iterator界面,package java.util;public interface
2、 Collection extends Iterable int size();boolean isEmpty();boolean contains(Object o);Iterator iterator();T toArray(T a);boolean add(E o);boolean remove(Object o);boolean containsAll(Collection c);boolean addAll(Collection c);boolean removeAll(Collection c);boolean retainAll(Collection c);void clear(
3、);boolean equals(Object o);int hashCode();,简介List界面,每个加入List中的元素是循序加入的,并可指定索引来存取元素,package java.util;public interface List extends Collection.boolean addAll(int index,Collection c);E get(int index);E set(int index,E element);void add(int index,E element);E remove(int index);int indexOf(Object o);int
4、 lastIndexOf(Object o);List subList(int fromIndex,int toIndex);.,简介List界面,List可以使用数组(Array)或是链结串行(LinkedList)来实作这个特性对于循序加入与存取,使用ArrayList的效率比较好对于经常变动元素排列顺序的需求,使用LinkedList会比较好,ArrayList,使用数组结构实作List数据结构可以使用索引来快速指定对象的位置于快速的随机取得对象来说,使用ArrayList可以得到较好的效能若要从中间作移除或插入对象的动作,会需要搬动后段的数组元素以重新调整索引顺序,所以速度上就会慢的多
5、,ArrayList,Scanner scanner=new Scanner(System.in);List list=new ArrayList();System.out.println(输入名称(使用quit结束);while(true)System.out.print(#);String input=scanner.next();if(input.equals(quit)break;list.add(input);System.out.print(显示输入:);for(int i=0;i list.size();i+)System.out.print(list.get(i)+);Syst
6、em.out.println();,ArrayList,如果您的目的是要循序取出容器中所有的对象,则您可以使用IteratorIterator的实例是在ArrayList中根据数组的结构而实作的,但您不用理会实作细节,Iterator iterator=list.iterator();while(iterator.hasNext()/还有下一个元素吗?/使用next()取得下一个元素 System.out.print(iterator.next()+);,ArrayList,使用增强的for循环(Enhanced forloop)来直接遍访List的所有元素,/使用foreach来遍访List
7、中的元素 for(String s:list)System.out.print(s+);,LinkedList,如果经常从容器中作移除或插入对象的动作,使用LinkedList会获得较好的效能LinkedList使用链结串行(Linkedlist)实作了List界面addFirst()、addLast()、getFirst()、getLast()、removeFirst()、removeLast()等,LinkedList,private LinkedList linkedList;public StringStack()linkedList=new LinkedList();public v
8、oid push(String name)/将元素加入串行前端 linkedList.addFirst(name);public String top()/取得串行第一个元素 return linkedList.getFirst();public String pop()/移出第一个元素 return linkedList.removeFirst();public boolean isEmpty()/串行是否为空 return linkedList.isEmpty();,LinkedList,private LinkedList linkedList;public StringQueue()l
9、inkedList=new LinkedList();public void put(String name)linkedList.addFirst(name);public String get()return linkedList.removeLast();public boolean isEmpty()return linkedList.isEmpty();,HashSet,实作了界面,Set界面继承了Collection界面List容器中的对象允许重复,但Set容器中的对象都是唯一的Set容器有自己的一套排序规则HashSet容器中的对象是否相同时,会先比较hashCode()方法传回
10、的值是否相同,如果相同,则再使用equals()方法比较,如果两者都相同,则视为相同的对象,HashSet,Set set=new HashSet();set.add(caterpillar);set.add(momor);set.add(bush);/故意加入重复的对象 set.add(caterpillar);/使用Iterator显示对象 Iterator iterator=set.iterator();while(iterator.hasNext()System.out.print(iterator.next()+);System.out.println();,HashSet,Set
11、set=new LinkedHashSet();set.add(caterpillar);set.add(momor);set.add(bush);/使用enhanced for loop显示对象 for(String name:set)System.out.print(name+);System.out.println();,TreeSet,TreeSet实作Set界面与界面TreeSet是JavaSE中唯一实作SortedSet接口的类别自动依字典顺序进行排列的动作,TreeSet,Set set=new TreeSet();set.add(justin);set.add(caterpil
12、lar);set.add(momor);/使用enhanced for loop显示对象 for(String name:set)System.out.print(name+);System.out.println();,TreeSet,自定义一个实作Comparator接口的类别,public class CustomComparator implements Comparator public int compare(T o1,T o2)if(T)o1).equals(o2)return 0;return(Comparable)o1).compareTo(T)o2)*-1;,Compara
13、tor comparator=new CustomComparator();Set set=new TreeSet(comparator);,HashMap,Map的特性即键-值(Key-Value)匹配实作了Map界面,HashMap在内部实作使用哈希(Hash),很快的时间内可以寻得键-值匹配,HashMap,Map map=new HashMap();String key1=caterpillar;String key2=justin;map.put(key1,caterpillar的讯息);map.put(key2,justin的讯息);System.out.println(map.g
14、et(key1);System.out.println(map.get(key2);,HashMap,可以使用values()方法返回一个实作Collection的对象,当中包括所有的值对象,Map map=new HashMap();map.put(justin,justin的讯息);map.put(momor,momor的讯息);map.put(caterpillar,caterpillar的讯息);Collection collection=map.values();Iterator iterator=collection.iterator();while(iterator.hasNex
15、t()System.out.println(iterator.next();System.out.println();,HashMap,Map map=new LinkedHashMap();map.put(justin,justin的讯息);map.put(momor,momor的讯息);map.put(caterpillar,caterpillar的讯息);for(String value:map.values()System.out.println(value);,TreeMap,实作Map界面与界面SortedMap提供相关的方法让您有序的取出对应位置的对象,像是firstKey()、
16、lastKey()等方法TreeMap是JavaSE中唯一实作SortedMap接口的类别,TreeMap,Map map=new TreeMap();map.put(justin,justin的讯息);map.put(momor,momor的讯息);map.put(caterpillar,caterpillar的讯息);for(String value:map.values()System.out.println(value);,TreeMap,如果对对象有一套排列顺序,要定义一个实作接口的对象,CustomComparator comparator=new CustomComparator();Map map=new TreeMap(comparator);,