首先,「Collection」和「Collections」是兩個不一樣的概念。你能夠從下面的層次關係圖中看到,」Collection」是集合層次的頂層接口,而」Collections」是提供了操做集合類型的靜態方法的類。java
下圖是集合類的層次關係圖git
下圖是Map的類層次結構關係圖github
下面是一個簡單的集合的例子:spa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
List a1 =
new
ArrayList();
a1.add(
"Program"
);
a1.add(
"Creek"
);
a1.add(
"Java"
);
a1.add(
"Java"
);
System.out.println(
"ArrayList Elements"
);
System.out.print(
"\t"
+ a1 +
"\n"
);
List l1 =
new
LinkedList();
l1.add(
"Program"
);
l1.add(
"Creek"
);
l1.add(
"Java"
);
l1.add(
"Java"
);
System.out.println(
"LinkedList Elements"
);
System.out.print(
"\t"
+ l1 +
"\n"
);
Set s1 =
new
HashSet();
// or new TreeSet() will order the elements;
s1.add(
"Program"
);
s1.add(
"Creek"
);
s1.add(
"Java"
);
s1.add(
"Java"
);
s1.add(
"tutorial"
);
System.out.println(
"Set Elements"
);
System.out.print(
"\t"
+ s1 +
"\n"
);
Map m1 =
new
HashMap();
// or new TreeMap() will order based on keys
m1.put(
"Windows"
,
"2000"
);
m1.put(
"Windows"
,
"XP"
);
m1.put(
"Language"
,
"Java"
);
m1.put(
"Website"
,
"programcreek.com"
);
System.out.println(
"Map Elements"
);
System.out.print(
"\t"
+ m1);
|
輸出:code
1
2
3
4
5
6
7
8
|
ArrayList Elements
[Program, Creek, Java, Java]
LinkedList Elements
[Program, Creek, Java, Java]
Set Elements
[tutorial, Creek, Program, Java]
Map Elements
{Windows=XP, Website=programcreek.com, Language=Java}
|