import java.util.*;
public class TestCollectionSequence
{
ArrayList arrayList = null;
LinkedList linkedList = null;
HashSet hashSet = null;
TreeSet treeSet = null;
HashMap hashMap = null;
TreeMap treeMap = null;
public TestCollectionSequence()
{
// arrayList
arrayList = new ArrayList();
System.out.println("Add elements into arrayList");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
arrayList.add(r);
System.out.print(r + " ");
}
System.out.println("
The elements in arrayList is : ");
System.out.println(arrayList);
System.out.println("
");
//linkedList
linkedList = new LinkedList();
System.out.println("Add elements into linkedList");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
linkedList.add(r);
System.out.print(r + " ");
}
System.out.println("
The elements in linkedList is : ");
System.out.println(linkedList);
System.out.println("
");
// hashSet
hashSet = new HashSet();
System.out.println("Add elements into hashSet");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
if(hashSet.add(r))
System.out.print(r + " ");
}
System.out.println("
The elements in hashSet is : ");
System.out.println(hashSet);
System.out.println("
");
//
treeSet = new TreeSet();
System.out.println("Add elements into treeSet");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
if(treeSet.add(r))
System.out.print(r + " ");
}
System.out.println("
The elements in treeSet is : ");
System.out.println(treeSet);
System.out.println("
");
//hashMap
hashMap = new HashMap();
System.out.println("Add elements into hashMap");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
System.out.print(r + " ");
hashMap.put(new Integer(i), r);
}
System.out.println("
The elements in hashMap is : ");
System.out.println(hashMap);
System.out.println("
");
//treeMap
treeMap = new TreeMap();
System.out.println("Add elements into treeMap");
for(int i = 0; i < 15; i++)
{
Integer r = new Integer((int)(Math.random()*15));
System.out.print(r + " ");
treeMap.put(new Integer(i), r);
}
System.out.println("
The elements in treeMap is : ");
System.out.println(treeMap);
}
public static void main(String[] args)
{
TestCollectionSequence test = new TestCollectionSequence();
}
}
输出如下:
Add elements into arrayList
11 9 7 3 1 8 9 6 5 9 6 2 11 14 3
The elements in arrayList is :
[11, 9, 7, 3, 1, 8, 9, 6, 5, 9, 6, 2, 11, 14, 3]
Add elements into linkedList
3 10 8 5 1 7 3 10 10 11 14 10 14 2 0
The elements in linkedList is :
[3, 10, 8, 5, 1, 7, 3, 10, 10, 11, 14, 10, 14, 2, 0]
Add elements into hashSet
6 10 14 13 11 9 12 8 5 2 7 3
The elements in hashSet is :
[2, 13, 8, 9, 11, 6, 3, 14, 7, 10, 5, 12]
Add elements into treeSet
6 1 14 9 8 11 4 5 2 0 13 3
The elements in treeSet is :
[0, 1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14]
Add elements into hashMap
14 7 2 13 0 9 2 8 5 5 12 8 11 9 6
The elements in hashMap is :
{4=0, 8=5, 11=8, 3=13, 7=8, 12=11, 2=2, 13=9, 9=5, 6=2, 1=7, 14=6, 10=12, 5=9, 0=14}
Add elements into treeMap
0 4 12 0 2 8 3 6 3 7 3 13 2 3 3
The elements in treeMap is :
{0=0, 1=4, 2=12, 3=0, 4=2, 5=8, 6=3, 7=6, 8=3, 9=7, 10=3, 11=13, 12=2, 13=3, 14=3}
Finished executing
List, Map, Set都只是接口而已,里面数据具体的存放方式可能会依具体的类不同而显得完全不一样的。比如我写的一个List其存储方式可以与ArrayList完全相反。
但是通常而言,我们说List,其实是在说ArrayList/LinkedList,Map就是HashMap/TreeMap,下面我所说的,也是基于这几个类的
(理论上)List是有序的,这个从其接口get(int), indexOf(Object)这些方法上就可以看出来
List的顺序就是你放他们的顺序,Set基本上就是一个List,唯一的区别就是同一个东西,List里面你可以多次重复放进去,而Set里面只能放一次,也就是第一次。
Map 是一个Key/Value对的集合,他们之间的关系就好像y=f(x); x是key, y是value, 一个key只能对应一个value, 因此通常是根据Key来决定存放Value(或者Key/Value对)的位置的。正是基于这个原因,Map的存放顺序与你放他们的顺序无关。而和他们的散列码hashCode有关