1. 栈Stack、双端队列Deque
Stack<Character> stack=new Stack<Character>();
stack.push('c');
char c=stack.peek();
stack.pop();
class Stack<E> extends Vector<E> {}
- 推荐使用Deque
- ArrayDeque实现:循环数组,也就是说数组的任何一点都可能被看作起点或者终点
- LinkedList实现:双向链表结构
Deque<Integer> Q=new ArrayDeque<Integer>();
Deque<Integer> Q=new LinkedList<Integer>();
Q.addLast(x);
Q.addFirst(x);
Q.getFirst();
Q.getLast();
Q.removeFirst();
Q.removeLast();
2. HashSet
Set<Integer> set=new HashSet<>();
boolean flag=set.contains(n);
set.add(n);
int size=set.size();
set.remove(Object o);
3. HashMap
Map<Integer,String> map=new HashMap<>();
map.put(key, value);
boolean flag=map.containsKey(key);
String value=map.get(key);
String value=map.getOrDefault(key, defaultValue);
int size=map.size();
Map<String,Integer> map=new HashMap<String,Integer>(){{
put("One", 1);
put("Two", 2);
put("Three", 3);
}};
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
String[] strs = {"a", "b", "a", "c"};
Map<String, List<Integer>> map = new HashMap<>();
for (int i = 0; i < strs.length; i ++) {
String str = strs[i];
if (!map.containsKey(str)) {
map.put(str, new ArrayList<>());
}
map.get(str).add(i);
}
for (int i = 0; i < strs.length; i ++) {
String str = strs[i];
map.putIfAbsent(str, new ArrayList<>());
map.get(str).add(i);
}
for (int i = 0; i < strs.length; i ++) {
String str = strs[i];
map.compute(str, (k, v) -> v == null ? new ArrayList<>(1) : v).add(i);
}
for (int i = 0; i < strs.length; i ++) {
String str = strs[i];
map.computeIfAbsent(str, k -> new ArrayList<>(1)).add(i);
}
4. List
List<Integer> list=new ArrayList<>();
list.add(element);
list.get(index);
list.size();
list.add(index,element);
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1>o2) return -1;
else if(o1<o2) return 1;
else return 0;
};
});
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1>o2) return -1;
else if(o1<o2) return 1;
else return 0;
};
});
5. Queue
Queue<Integer> queue=new LinkedList<>();
queue.offer(e);
Integer e=queue.poll();
Integer e=queue.peek();
6. PriorityQueue
pqueue.offer();
pqueue.peek();
pqueue.poll();
pqueue.remove(Object o);
PriorityQueue<Integer> pqueue=new PriorityQueue<Integer>(
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1>o2) return -1;
else if(o1<o2) return 1;
else return 0;
}
}
);
PriorityQueue<Integer> priorityQueue=new PriorityQueue<>((o1, o2) -> o2 - o1);
PriorityQueue<Integer> pqueue=new PriorityQueue<Integer>(
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1>o2) return 1;
else if(o1<o2) return -1;
else return 0;
}
}
);
PriorityQueue<Integer> priorityQueue=new PriorityQueue<>((o1, o2) -> o1 - o2);
PriorityQueue<int[]> pqueue=new PriorityQueue<>(
new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[1]>o2[1]) return -1;
else if(o1[1]<o2[1]) return 1;
else return 0;
}
}
);
PriorityQueue<int[]> pqueue=new PriorityQueue<>((o1, o2) -> o2[1] - o1[1]);
PriorityQueue<int[]> pqueue=new PriorityQueue<>((o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);
PriorityQueue<My> pqueue=new PriorityQueue<My>(
new Comparator<My>() {
@Override
public int compare(My o1, My o2) {
if(o1.getNum()>o2.getNum()) return -1;
else if(o1.getNum()<o2.getNum()) return 1;
else return 0;
}
}
);
PriorityQueue<My> pqueue=new PriorityQueue<My>((o1, o2) -> o2.getNum() - o1.getNum());
PriorityQueue<Map.Entry<Integer, Integer>> priorityQueue = new PriorityQueue<>(
new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) {
return e2.getValue() - e1.getValue();
}
}
);
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<5;i++) {
map.put(i,i+2);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
priorityQueue.add(entry);
}
7. 数组Arrays
int[] nums=new int[5];
int[] nums=new int[] {1,2,3,4,5};
Arrays.fill(nums,Integer.MAX_VALUE);
Arrays.asList(nums);
Arrays.sort(nums);
Arrays.copyOfRange(nums, fromIndex, toIndex);
int[][] nums=new int[][]{{1,3},{2,4},{3,2}};
Arrays.sort(nums, new Comparator<int[]>(){
public int compare(int[] a, int[] b){
return b[1]-a[1];
}
});
8. 常用类型转换
String str = String.valueOf('c');
char c=str.charAt(index)
char[] chars=str.toCharArray( )
String[] strs1= new String[]{"Tom", "Bob", "Jane"};
List<String> strList= Arrays.asList(strs1);
String[] strs2=new String[strList.size()];
strList.toArray(strs2);
int[] data = {4, 5, 3, 6, 2, 5, 1};
List<Integer> list1 = Arrays.stream(data).boxed().collect(Collectors.toList());
int[] arr1 = list1.stream().mapToInt(Integer::valueOf).toArray();
String[] strs1= new String[]{"Tom", "Bob", "Jane"};
Set<String> set = new HashSet<String>(Arrays.asList(strs1));
String[] strs2=new String[set.size()];
set.toArray(strs2);
List<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("a");
Set<String> set=new HashSet<String>(list);
List<String> newlist=new ArrayList<String>(set);
List<int[]> <---> int[][]
List<int[]> list= new ArrayList<>();
list.add(new int[] {1,2});
list.add(new int[] {3,4});
int[][] arrs= list.toArray(new int[0][]);
详细查看:https://www.cnblogs.com/zhihaospace/p/12547294.html
java中List, Integer[], int[]的相互转换