如何选择合适的容器以及其实现品

各种Lists间的选择:
lists包括:ArrayList and LinkedList
ArrayList : 以array作为底层实现;
Linkedlist:以一般的双向链表完成,其中的每个对象除了数据本身以外,还有两个reference分别指向前一个
元素和后一个元素。
+----------Example-------------------+
import java.util.*;
import com.bruceeckel.util.*;
public class ListPerformance {
  private abstract static class Tester {
    String name;
    int size; // Test quantity
    Tester(String name, int size) {
      this.name = name;
      this.size = size;
    }
    abstract void test(List a, int reps);
  }
  private static Tester[] tests = {
    new Tester("get", 300) {
      void test(List a, int reps) {
        for(int i = 0; i < reps; i++) {
          for(int j = 0; j < a.size(); j++)
            a.get(j);
        }
      }
    },
    new Tester("iteration", 300) {
      void test(List a, int reps) {
        for(int i = 0; i < reps; i++) {
          Iterator it = a.iterator();
          while(it.hasNext())
            it.next();
        }
      }
    },
    new Tester("insert", 5000) {
      void test(List a, int reps) {
        int half = a.size()/2;
        String s = "test";
        ListIterator it = a.listIterator(half);
        for(int i = 0; i < size * 10; i++)
          it.add(s);
      }
    },
    new Tester("remove", 5000) {
      void test(List a, int reps) {
        ListIterator it = a.listIterator(3);
        while(it.hasNext()) {
          it.next();
          it.remove();
        }
      }
    },
  };
  public static void test(List a, int reps) {
    // A trick to print out the class name:
    System.out.println("Testing " +
      a.getClass().getName());
    for(int i = 0; i < tests.length; i++) {
      Collections2.fill(a,
        Collections2.countries.reset(),
        tests[i].size);
      System.out.print(tests[i].name);
      long t1 = System.currentTimeMillis();
      tests[i].test(a, reps);
      long t2 = System.currentTimeMillis();
      System.out.println(": " + (t2 - t1));
    }
  }
  public static void testArray(int reps) {
    System.out.println("Testing array as List");
    // Can only do first two tests on an array:
    for(int i = 0; i < 2; i++) {
      String[] sa = new String[tests[i].size];
      Arrays2.fill(sa,
        Collections2.countries.reset());
      List a = Arrays.asList(sa);
      System.out.print(tests[i].name);
      long t1 = System.currentTimeMillis();
      tests[i].test(a, reps);
      long t2 = System.currentTimeMillis();
      System.out.println(": " + (t2 - t1));
    }
  }
  public static void main(String[] args) {
    int reps = 50000;
    // Or, choose the number of repetitions
    // via the command line:
    if(args.length > 0)
      reps = Integer.parseInt(args[0]);
    System.out.println(reps + " repetitions");
    testArray(reps);
    test(new ArrayList(), reps);
    test(new LinkedList(), reps);
    test(new Vector(), reps);
  }
} ///:~
+-----------------------------------------+
运行结果如下:
容器型别      取得      迭代     安插     移除
array               1430      3850       na        na
ArrayList       3070      12200     500     46850
LinkedList     16320     9110      110     60
Vector            4890      16250     550     46850
+-------{转自《think in java》}--------------+
从上面的结果可以看出,linkedlist的优点在于安插和移除,在迭代上也比arraylist要快,
因此如过你要在list发中心处进行安插和移除操作,请选用linkedlist,否则则选用arraylist。
因为在数据的取得上arraylist还是很有优势的如果你所处理的是固定数量的一组元素,请使用array。
Vector通常是兼容老旧的程序,出此之外没什么用途。你应该尽量避免使用他。
+=------------------------------------------=+
各种Sets之间的选择
Sets包括:TreeSet and HashSet
将上面的代码改稍微一下,测试后的结果表明:对所有的动作而言,hashset都要优越与treeset。所以一般情况下
hashset应该是你的默认选择。只有当需要维护元素的排序状况的时候,才应该使用treeset;
+=------------------------------------------=+
各种Maps之间的选择
Maps包括:TreeMap;HashMap;Hashtable;
HashMap与Hashtable的效率差不多,一般来讲,HashMap还要快一点。因为HashMap是Hashtable的替代品,但是并不是说Hashtable就没有用了,你可以不把Hashtable当作Map,而是把他当成一种产生有序链表(ordered list)的手法。tree特性是他总保持某种次序,但是不一定有什么特定的排序。一般说来,你应该把HashMap当成你的默认选择。只有当你的 Map必须要保持排序状态,才需要动用TreeMap
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值