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
//save without order
HashMap<Integer,String> hashMap = new HashMap<>();
//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.set(1, "new value")
hashMap.containsKey(1);
hashMap.containsValue("whose value is 2");
hashMap.forEach((key1, value1) -> {
//
});

//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
//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
TreeSet<Integer> integerTreeSet = new TreeSet<>((a,b)->{
return a-b;
});

List

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
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 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

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
3
4
5
6
List<Integer> values = new ArrayList<>();

int sum = values.stream().mapToInt(Integer::intValue).sum();

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