1 两大接口Collection与Map
在集合框架的类继承体系中,最顶层有两个接口:
- Collection表示一组纯数据
- Map表示一组key-value对
1.1 Collection
如上图所示,Collection接口中主要有三个接口:
- Set表示不允许有重复元素的集合(A collection that contains no duplicate elements)
- List表示允许有重复元素的集合(An ordered collection (also known as a sequence))
- Queue JDK1.5新增,与上面两个集合类主要是的区分在于Queue主要用于存储数据,而不是处理数据。(A collection designed for holding elements prior to processing.)
1.2 Map
Map并不是一个真正意义上的集合(are not true collections),但是这个接口提供了三种“集合视角”(collection views ),使得可以像操作集合一样操作它们,具体如下:
- 把map的内容看作key的集合(map’s contents to be viewed as a set of keys)
- 把map的内容看作value的集合(map’s contents to be viewed as a collection of values)
- 把map的内容看作key-value映射的集合(map’s contents to be viewed as a set of key-value mappings)
2 集合框架的使用
2.1 ArrayList的遍历
2.1.1 迭代器遍历
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
list.add("third");
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
2.1.2 索引值遍历
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
list.add("third");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
2.1.3 forEach循环遍历
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
list.add("third");
for (String str : list) {
System.out.println(str);
}
}
2.2 HashSet的遍历(与List一致)
2.4 HashMap的遍历
2.4.1 通过Map.keySet遍历key和value
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
for (Integer i : map.keySet()) {
System.out.println("key=" + i + ",value=" + map.get(i));
}
}
2.4.2 通过Map.entrySet使用iterator遍历key和value
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = (Entry<Integer, String>) it.next();
System.out.println("key=" + entry.getKey() + ",value=" + entry.getValue());
}
}
2.4.3 通过Map.entrySet遍历key和value
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("key=" + entry.getKey() + ",value=" + entry.getValue());
}
}
2.4.4 通过Map.values()遍历所有的value,但不能遍历key
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
for (String str : map.values()) {
System.out.println(str);
}
}
2.5 注意事项:关于 map.entrySet() 和 keySet():
- 如果遍历 hashMap() 时 entrySet() 方法是将 key 和 value 全部取出来,所以性能开销是可以预计的, 而 keySet() 方法进行遍历的时候是根据取出的 key 值去查询对应的 value 值, 所以如果 key 值是比较简单的结构(如 1,2,3…)的话性能消耗上是比 entrySet() 方法低, 但随着 key 值得复杂度提高 entrySet() 的优势就会显露出来。
- 综合比较在只遍历 key 的时候使用 keySet(), 在只遍历 value 的是使用 values() 方法, 在遍历 key-value 的时候使用 entrySet() 是比较合理的选择。
- 如果遍历 TreeMap 的时候, 不同于 HashMap 在遍历 ThreeMap 的 key-value 时候务必使用 entrySet() 它要远远高于其他两个的性能, 同样只遍历 key 的时候使用 keySet(), 在只遍历 value 的是使用 values() 方法对于 TreeMap 也同样适用。