java编程思想阅读笔记(五)持有对象

来源:互联网 发布:874是什么意思网络语言 编辑:程序博客网 时间:2024/05/16 17:07
    先说说概念,什么持有不持有的,就是“保存对象”,说白了就是对象的灵活(按需)存取,这个需就是Java容器类类库各种类的用武之地。

    放一张书上的简单的容器分类图,其中点线框表示接口,实线框表示普通的(具体的)类,带有空心箭头的点线表示一个特定的类实现了一个接口,空心箭头表示某个类可以生成箭头所指向类的对象。


    从上图看,实现对象按需存取的基本接口是List、Set、Queue和Map,抽象共性Collection是其根接口;从接口再到具体的类方式有点多,那么我只管遍历保存的对象,却不想管你是哪种接口(类)的,就用个迭代器,接口为Iterator,帮助你遍历并选择保存的对象。
    看基本接口,
List可以精确的控制插入的元素位置(add方法),根据索引(类似于数组)随机地访问元素(get方法);
Set则是保存唯一的对象,这一唯一性怎么确定,可以通过前面图中看到的Comparator接口类实现。
Queue由其名就可知通常以先进先出的方式排列各个元素,但也不是绝对,PriorityQueue则赋予了存入元素的优先级,这样在使用poll(出队列)时,保证还有优先级的要求。
Map可以理解为保存的是键值对的序列,建立了键和值的映射(对应),可以通过键来找值。
    看了基本接口,就是具体的类了,List下的ArrayList和LinkedList,书中介绍ArrayList长于随机访问元素,但是在List的中间插入和移除元素时较慢;LinkedList在随机访问方面相对比较慢,但是优化了顺序访问,而且降低了在List中间进行的插入和删除操作代价;书中也提到了性能优化,提示说是不必一开始就注重向上述所述的优化,而当确实发现性能出现问题(或者瓶颈在此)时,再进行有针对性的处理,个人很同意,但是没有大型项目支撑,不知道这样做会在以后的优化埋下怎样的地雷。
    Set下的HashSet通过利用哈希表来支持基本的add、remove等操作(是没有get方法的,可以通过iterator方法来遍历所有元素),不保证存储的顺序;TreeSet将元素存储在红-黑树结构中,而且具有lower、higher方法返回与查询对象最近匹配(实现了NavigableSet接口,对保存对象已经进行了排序,实现了SortedSet接口,这些没有在图中显示,可以查看帮助文档得到详细信息);LinkedHashSet加入了链表来维护元素的插入顺序,使得迭代时与插入顺序一致。
    Map中的HashMap不保证映射顺序,保证基本操作(get 和 put)提供稳定的性能,那么LinkedHashMap就通过一个双重链表保证了顺序(迭代顺序)。而Queue下的PriorityQueue以优先级顺序进行排列的队列,如果想要纯粹的那么可以使用AbstractQueue,其实上面基本接口都有以Abstract开头的基本实现可以使用。

最后以一个遍历各个常用容器类的代码结尾,说明遍历顺序:

package com.test.myjava;import java.util.*;class Content implements Comparable<Content> {public int id;public String content;public Content(int i, String string) {id = i;content = string;}public static void asCollection(int count, Content[] con,Collection<Content> coll) {for (int j = 0; j < count; j++)coll.add(con[j]);}public int compareTo(Content arg0) {// TODO Auto-generated method stubreturn (-this.id - arg0.id);//this.id - arg0.id 影响TreeSet顺序}}public class JavaExample {public static void display(Collection<Content> it) {for (Content t : it)System.out.print(t.content + " ");System.out.println();}public static void main(String args[]) {final int contentCount = 5;Content[] contentArray = new Content[contentCount];ArrayList<Content> coll = new ArrayList<Content>();ArrayList<Integer> collInt = new ArrayList<Integer>();// 内容赋值for (int i = 0; i < contentCount; i++) {contentArray[i] = new Content(i, "Content" + i);collInt.add(i);}// 转为CollectionContent.asCollection(contentCount, contentArray, coll);Collection<Content> arrayListContent = new ArrayList<Content>(coll);System.out.println("ArrayList:");JavaExample.display(arrayListContent);Collection<Content> linkedListContent = new LinkedList<Content>(coll);System.out.println("LinkedList:");JavaExample.display(linkedListContent);Collection<Content> hashSetContent = new HashSet<Content>(coll);System.out.println("HashSet:");JavaExample.display(hashSetContent);Collection<Content> treeSetContent = new TreeSet<Content>(coll);System.out.println("TreeSet:");JavaExample.display(treeSetContent);Collection<Content> priorityQueueContent = new PriorityQueue<Content>(coll);System.out.println("PriorityQueue:");JavaExample.display(priorityQueueContent);Map<Integer, Content> hashMapContent = new HashMap<Integer, Content>();for (int i = 0; i < contentCount; i++)hashMapContent.put(collInt.get(i), coll.get(i));System.out.println("HashMap:");JavaExample.display(hashMapContent.values());//利用hashMapContent填充Map<Integer, Content> linkedHashMapContent0 = new LinkedHashMap<Integer, Content>(hashMapContent);System.out.println("LinkedHashMap0:");JavaExample.display(linkedHashMapContent0.values());//顺序填充Map<Integer, Content> linkedHashMapContent = new LinkedHashMap<Integer, Content>();for (int i = 0; i < contentCount; i++)linkedHashMapContent.put(collInt.get(i), coll.get(i));System.out.println("LinkedHashMap:");JavaExample.display(linkedHashMapContent.values());}}/* * 输出:  * ArrayList: * Content0 Content1 Content2 Content3 Content4  * LinkedList: * Content0 Content1 Content2 Content3 Content4  * HashSet: * Content2 Content1 Content4 Content0 Content3  * TreeSet: * Content4 Content3 Content2 Content1 Content0  * PriorityQueue: * Content0 Content1 Content2 Content3 Content4  * HashMap: * Content2 Content4 Content1 Content3 Content0  * LinkedHashMap0: * Content2 Content4 Content1 Content3 Content0  * LinkedHashMap: * Content0 Content1 Content2 Content3 Content4  */



原创粉丝点击