Java collections
Java collections framework is a group of classes and interfaces to provide common data structures.
算法中经常用到的collection和方法。
Collection 接口常用方法
(1).方法
add() remove() isEmpty() size()
//example
Collection list = new Arraylist();
ArrayList<Integer> result = new ArrayList<Integer>();
result.add(1);
result.size();
result.remove(0);
Iterater it = list.iterator();
while list.hasNext() {
String str = (String)it.next();
}
List
(1). 方法:
get(index) set(index, obj)
(2). 实现类:
ArrayList: 可变长度动态数组,insert,remove操作比较慢
LinkedList: 链表
List list1 = new ArrayList();
List list2 = new LinkedList();
Set (1). HashSet 不能有重复对象
Map 没有继承collection interface, 不能用collection 方法, 而且只能有一个key,不能重复 (1). 方法:
put(key, value) containsKey() containsValue() get(key)
(2). example:
Map map = new HashMap();
map.put("1", "leon");
map.get("1")
遍历hashmap
Map<String, String> map = ...
for (Map.Entry<String, String> entry : map.entrySet()){
System.out.println(entry.getKey() + "/" + entry.getValue());
}