Collection, TreeMap, LinkedHashMap, TreeSet, LinkedHashSet, Comparable

1.	Collections.sort():
List<Integer> list = new ArrayList<Integer>();
       list.add(1);
       list.add(3);
       list.add(2);

       for (int i : list) {
           System.out.println(i);
       }

       Collections.sort(list);

       for (int i : list) {
           System.out.println(i);
       }

       Collections.sort(list, new Comparator() {
           @Override
           public int compare(Object arg0, Object arg1) {
              // TODO Auto-generated method stub
              //return new Integer(arg1.toString()) - new Integer(arg0.toString());
              return arg1.toString().compareTo(arg0.toString());
           }
       });

2.	TreeMap usage:
TreeMap<String, Object> treeMap1 = new TreeMap<String, Object>();
       treeMap1.put("2", new Object());
       treeMap1.put("3", new Object());
       treeMap1.put("1", new Object());
       treeMap1.put("2", new Object());

       for (String i : treeMap1.keySet()) {
           System.out.println(i);
       }

       TreeMap<Integer, Object> treeMap2 = new TreeMap<Integer, Object>(new Comparator() {
           @Override
           public int compare(Object arg0, Object arg1) {
              // TODO Auto-generated method stub
              return new Integer(arg1.toString()) - new Integer(arg0.toString());
           }
       });

       treeMap2.put(2, new Object());
       treeMap2.put(3, new Object());
       treeMap2.put(1, new Object());

       for (int i : treeMap2.keySet()) {
           System.out.println(i);
}

3.	ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动 等内存操作,所以索引数据快插入数据慢,Vector由于使用了synchronized方法(线程安全)所以性能上比ArrayList要 差,LinkedList使用双向链表实现存储,按序号索引数据需要进行向前或向后遍历,但是插入数据时只需要记录本项的前后项即可,所以插入数度较快!


线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构。
Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set

Map
├Hashtable
├HashMap
│       └LinkedHashMap: 遍历按照put进去的顺序进行
└WeakHashMap

Set接口有一个子接口SortedSet,提供了集合元素的顺序存储,其中元素保持升序排列.为了在加入一个元素到SortedSet实现时能对这些 元素排序,元素类型必须实现Comarable接口,或者建立SortedSet时使用Comparator.否则程序在运行时将抛出 ClassCastException.

4.LinkedHashMap:
                   Map<String, Object> treeMap1 = new LinkedHashMap<String, Object>();
       treeMap1.put("2", new Object());
       treeMap1.put("3", new Object());
       treeMap1.put("1", new Object());
       treeMap1.put("2", new Object());

       for (String i : treeMap1.keySet()) {
           System.out.println(i);
       }
                   输出结果为:2 3 1

4.	TreeSet:
Set<String> treeSet = new TreeSet<String>(new Comparator() {
           @Override
           public int compare(Object arg0, Object arg1) {
              // TODO Auto-generated method stub
              return arg1.toString().compareTo(arg0.toString());
           }
       });
       
       treeSet.add("2");
       treeSet.add("3");
       treeSet.add("1");
       treeSet.add("2");

       for (String i : treeSet) {
           System.out.println(i);
}


class ABC {
    private int value;

    public ABC(int value) {
       this.value = value;
    }

    public int getValue() {
       return this.value;
    }
}
//********** ABC必须实现Comparable接口或者TreeSet必须接收一个Comparator的实例
public class Test {

    public static void main(String[] args) {
       // TODO Auto-generated method stub
       Set<ABC> treeSet = new TreeSet<ABC>(new Comparator() {
           @Override
           public int compare(Object arg0, Object arg1) {
              // TODO Auto-generated method stub
              return ((ABC) arg0).getValue() - ((ABC) arg1).getValue();
           }
       });

       treeSet.add(new ABC(2));
       treeSet.add(new ABC(3));
       treeSet.add(new ABC(1));
       treeSet.add(new ABC(2));

       for (ABC i : treeSet) {
           System.out.println(i.getValue());
       }
    }
}

5.	LinkedHashSet
Set<String> treeSet = new LinkedHashSet<String>();
       
       treeSet.add("2");
       treeSet.add("3");
       treeSet.add("1");
       treeSet.add("2");

       for (String i : treeSet) {
           System.out.println(i);
}
输出结果为:2 3 1

你可能感兴趣的:(LinkedHashMap)