本文最后更新于:2022-09-03T13:16:42+08:00
集合基本使用
在Java中,集合的接口主要包括Collection和Map,具体继承关系如下所示。使用之前需要导入相应的包java.util.xxx
Collection 常见方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| int size();
boolean add(E e);
boolean addAll(Collection<? extends E> c);
void clear();
boolean remove(Object o);
boolean contains(Object o);
boolean containsAll(Collection<?> c);
boolean isEmpty();
Iterator iterator();
Object[] toArray();
T[] toArray(T[] a);
|
List 常见方法
1 2 3 4 5 6 7
| void add(int index, E element);
E get(int index);
E remove(int index);
E set(int index, E element);
|
ArrayList
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
| ArrayList<String> list = new ArrayList<>();
list.add(obj); list.addAll();
obj = list.get(index);
list.remove(index);
list.remove(obj);
len = list.size()
index = list.indexOf(obj);
list.contains();
list.clear();
list.isEmpty();
|
LinkedList
ArrayList中的方法,LinkedList都能够使用,除此以外还有更多的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| LinkedList<Integer> list = new LinkedList<>();
list.addFirst(E e);
list.addLast(E e);
int a = list.removeFirst();
int b = list.removeLast();
|
Stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Stack<Integer> stack = new Stack<>();
stack.push(int element);
int a = stack.pop();
int b = stack.peek();
stack.isEmpty(); stack.empty();
|
Queue 常见方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
boolean offer(E e); void add(E e);
E poll(); E remove();
E element(); E peek();
|
LinkedList和ArrayDeque
双端队列,可以用来模拟栈。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| LinkedList<Integer> deque = new LinkedList<>(); ArrayDeque<Integer> deque = new ArrayDeque<>();
deque.offer(int element);
int a = deque.poll();
int b = deque.peek();
deque.isEmpty();
deque.size();
|
PriorityQueue
优先队列,可以用来模拟堆。默认为小根堆,可以通过比较函数来指定称为大根堆。(比较函数可以通过定义匿名类或者Lambda表达式实现)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| PriorityQueue<Integer> qmin = new PriorityQueue<>();
PriorityQueue<Integer> qmax = new PriorityQueue<>(new Comparator<Integer>(){ public int compare(Integer a, Integer b){ return b - a; } });
PriorityQueue<Integer> qmax = new PriorityQueue((x, y) -> y - x)
PriorityQueue<Integer> qmax = new PriorityQueue<>(Collections.reverseOrder());
qmax.offer(4);
qmax.poll();
qmax.peek();
|
Set 常见方法
1 2 3 4 5 6 7 8 9 10 11
| floor(E e);
ceiling(E e);
lower(E e);
higher(E e);
|
HashSet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| HashSet<String> set = new HashSet<String>();
set.add("xxx");
set.remove("yyy");
set.size();
set.contains("xxx");
set.clear();
|
Map 常见方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| void put(Object key, Object value);
Object get(Object key);
Object getOrDefault(Object key, Object defalutValue);
boolean containsKey(Object key);
boolean containsValue(Object value);
Set keySet();
Collection values();
Set<Map.Entry<K, V>> entrySet();
|
K-V的遍历:
1 2 3 4 5
| Set<Map.Entry<Integer, Integer>> entries = map.entrySet(); for(Map.Entry<Integer, Integer> entry: entries){ int key = entry.getKey(); int value = entry.getValue(); }
|
HashMap
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 33 34 35
| HashMap<String, Integer> map = new HashMap();
map.put(key, value);
v = map.get(key);
map.keySet();
map.values();
map.remove(key); map.remove(key, value);
map.replace(key, value); map.replace(key, oldValue, newValue);
map.containsKey(key);
map.containsValue(value);
for(int i: map.keySet()) { ... }
|
TreeMap
1 2 3 4 5 6 7 8 9
| TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
map.ceilingKey(Integer key);
map.floorKey(Integer key);
|
工具类
Collection工具类
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
| ArrayList<Integer> array = new ArrayList<>();
Collections.reverse(array);
Collections.shuffle(array);
Collections.swap(array, 1, 3);
Collections.sort(array);
Collections.sort(array, new Comparator<Integer>(){ public int compare(Integer t1, Integer t2){ return t2 - t1; } });
Collections.max(array);
Collections.frequency(array, 1);
Collections.binarySearch(array, 1);
|
Arrays工具类
1 2 3 4 5
| int a[] = {1, 2, 3, 4, 54, 5, 6, 6};
Arrays.sort(a);
Arrays.asList(T ...p);
|
参考文章
- Java刷题常用集合类以及函数总结_叶~子的博客-CSDN博客_java集合函数
- Java常用容器基础操作汇总
- JavicxhloWong - 博客园 (cnblogs.com)