banner
NEWS LETTER

Java集合的常用方法

Scroll down

Map

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
//save without order
HashMap<Integer,String> hashMap1 = new HashMap<>();
//initialize with HashMap
HashMap<Integer,String> hashMap = new HashMap<>(hashMap1);
//put a new key-value entry if not existing before;
//replace the old value if existing such a key;
//return the old value or null if not existing
hashMap.put(1,"my value is 1");
//put a new entry only if there was no such key before
hashMap.putIfAbsent(1,"my value is 1");
hashMap.remove(1); //remove the entry whose key is 1
hashMap.remove(1,"my value is 2"); //remove the entry whose key is 1 and value is "my value is 2"
hashMap.get(1); //return the value whose key is 1
hashMap.getOrDefault(1, 0); //get with default value
hashMap.set(1, "new value")
hashMap.containsKey(1);
hashMap.containsValue("whose value is 2");
hashMap.forEach((key1, value1) -> {
//
});
for (Map.Entry(Integer, String) entry : hashMap.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}

//save with order
TreeMap<Integer,String> treeMap = new TreeMap<>((a,b)->{
return a-b;
});

Set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//save without order
HashSet<Integer> integerSet = new HashSet<>();
boolean addReturn = integerSet.add(1); // true if set doesn't contain target
boolean containReturn = integerSet.contains(3); // true if set contains target
boolean removeReturn = integerSet.remove(4); // true if set contains target
boolean isEmptyReturn = integerSet.isEmpty(); // true if set is empty

//save with order, and you can define your specific comparator using lambda expression
//比较器的使用方式为:若返回值>=0,则表示不要交换,否则,需要交换
TreeSet<Integer> integerTreeSet = new TreeSet<>((a,b)->{
return a-b;
});
integerTreeSet.higher(5); // 大于5的最小值
integerTreeSet.lower(5); // 小于5的最大值
integerTreeSet.ceiling(5); // 大于等于5的最小值
integerTreeSet.floor(5); // 小于等于5的最大值

List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
LinkedList<Integer> linkedList = new LinkedList<>();
Integer temp = linkedList.indexOf(2); //return the index of target object if exists else return false
linkedList.remove((Object) 2); //remove the object 2
linkedList.remove(2);//remove the element whose index is 2
//Queue operations: add from tail and get from head, FIFO
linkedList.offer(1); // add target from tail
temp = linkedList.peek(); //simply return the head element of the list
temp = linkedList.poll(); //return the head element of the list and remove it
//Deque operations: add and get from both ends
linkedList.offerFirst(1); //add from first
linkedList.offerLast(2); //add from last
temp = linkedList.peekFirst(); //peek from first
temp = linkedList.peekLast(); //peek from last
temp = linkedList.pollFirst(); //poll from first
temp = linkedList.pollLast(); //poll from last
Collections.sort(linkedList);

Stack

1
2
3
4
5
6
7
8
9
10
//Stack operations:add and get from the same end, LIFO
Stack<Integer> intStack = new Stack<>();
intStack.push(1); //push the target onto the stack
intStack.push(2);
int i = intStack.pop(); //return the element at the top of the stack and remove that element
int j = intStack.peek(); //return the element at the top of the stack
int index = intStack.search(2); //return the index of target object if it's in the stack else return -1
boolean isEmpty = intStack.isEmpty(); //return true if stack is empty else return false

intStack.add(1) //这个是Vector的接口,会加synchronized关键字

Queue

1
2
3
4
5
6
7
//this is a max heap
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((a,b)->{
return a-b;
});
priorityQueue.offer(1); //insert the target
priorityQueue.peek(); //return the max/min element decided by its type
priorityQueue.poll(); //return the max/min element and remove it

求和

1
2
List<Integer> values = new ArrayList<>();
int sum = values.stream().mapToInt(Integer::intValue).sum();

排序

1
2
3
4
5
6
7
8
9
10
11
12
// List排序,默认升序
List<Integer> values = new ArrayList<>();
values.sort();
Collections.sort(values); //实际调用values.list

//数组排序,默认升序
Arrays.sort(new int[]{1,3,2,6,5});

//TreeMap, TreeSet, PriorityQueue
TreeMap<String, Integer> treeMap = new TreeMap<>((x, y) -> {
return x.getValue
})

Map转换为List,并对Value排序

1
2
3
4
5
Map<String, Integer> counter = new HashMap<>();
List<Map.Entry<String, Integer>> list = new ArrayList<>(counter.entrySet());
Collections.sort(list, (x, y) -> {
return x.getValue() - y.getValue()
});