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("\nThe elements in arrayList is : ");
System.out.println(arrayList);
System.out.println("\n"); //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("\nThe elements in linkedList is : ");
System.out.println(linkedList);
System.out.println("\n"); // 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("\nThe elements in hashSet is : ");
System.out.println(hashSet);
System.out.println("\n"); //
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("\nThe elements in treeSet is : ");
System.out.println(treeSet);
System.out.println("\n");
//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("\nThe elements in hashMap is : ");
System.out.println(hashMap);
System.out.println("\n"); //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("\nThe elements in treeMap is : ");
System.out.println(treeMap);
} public static void main(String[] args)
{
TestCollectionSequence test = new 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("\nThe elements in arrayList is : ");
System.out.println(arrayList);
System.out.println("\n"); //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("\nThe elements in linkedList is : ");
System.out.println(linkedList);
System.out.println("\n"); // 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("\nThe elements in hashSet is : ");
System.out.println(hashSet);
System.out.println("\n"); //
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("\nThe elements in treeSet is : ");
System.out.println(treeSet);
System.out.println("\n");
//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("\nThe elements in hashMap is : ");
System.out.println(hashMap);
System.out.println("\n"); //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("\nThe 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存放数据是顺序的
而
set存放数据则是无序的
所以你取出来时和你
放进去时不同
一个是hash,肯定是随机的
而tree也是一种有序的
但是通常而言,我们说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有关
至于具体的shine333(enihs) 已经说的差不多了。
遍历顺序是经过排序的容器是实现了SortedSet和SortedMap接口的容器(SortedMap是按键的值排序的)
HashSet和HashMap的遍历顺序看起来是随机的,其实它是按照hashCode顺序排列的
我上面曾说过Map是按照key去找value的,因此其存放位置也是以key为标准的。一个TreeMap,要么放到其中的key是Comparable(一个key可以自己和另外一个key进行比较)的,要么你向它提供Comparator来比较两个key的大小。你put的时候,TreeMap从根节点(也就是你放进去的第一个key/value对)开始进行递归比较:如果当前节点是空的话,则要放的key/value对的位置就是当前节点。
如果新进的key与当前节点的key相同,则用新近的value冲掉原有的。
如果新进的key比当前节点的key小,则查找当前节点的左节点,重新进行比较。
如果新进的key比当前节点的key大,则查找当前节点的右节点,重新进行比较。为了描述方便,我讲的和程序里面稍微有些出入,程序是如果左右子节点为空,就新建一个节点,并把要放的key/value对放进去。get的时候与之类似,只不过如果节点为空说明找不到相应的key
做法就是递归提升某些子节点的辈分(遵照左先右后的顺序吧)