Debug
System.out.println("String: " + s);
Math
Math.abs(x);
Math.sqrt(x);
Math.min(a, b);
Math.max(a, b);
Imax = Integer.MAX_VALUE;
Imin = Integer.MIN_VALUE;
String
//长度
s.length();
//string转数字
int a = Integer.parseInt(string);
long la = Long.parseLong(string);
//转char
String s = "asdefgasdefg";
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
}
char[] cs = s.toCharArray();
// 把char[] 变成字符串
char[] ch = {'a', 'b', 'c'};
String.valueOf(ch);
//按逗号分割返回数组
string[] ss = s.split(",");
String s = s.substring((int)start,(int)end)//[start,end)
//StringBuilder
StringBuilder sb = new StringBuilder("String");
sb.append("");
sb.reverse();
sb.delete((int)start,(int)end); //[start,end)
sb.deleteCharAt(int index);
sb.insert((int)offset,"String");
sb.toString();
//join
String m1 = String.join("-", "Java", "is", "cool");// "Java-is-cool"
List<String> strings = List.of("Java", "is", "cool");
String m2 = String.join(" ", strings); // "Java is cool"
Arrays
Arrays.sort(int[] arr, int fromIndex, int toIndex, 比较器); //一定是需要泛型
Arrays.sort(arr, (o1, o2) -> o2 - o1); //数组全部 从大到小排序 跟Collections.sort()一样
Arrays.sort(arr, 0, 3, (o1, o2) -> o2 - o1); //从大到小排序,只排序[0, 3)
Collection
Collection接口是Set,List,Queue接口的父接口
//Collection
add(Object o);
addAll(Collection c);
clear();
contains(Object o);
remove(Object o);
size();
toArray();
Collections.sort(list); 从小到大排序
Collections.sort(list, (o1, o2) -> o2 - o1); 从大到小排序, 第二个参数为一个比较器
Collections.reverse(Collection c);
Collections.min(list);
Collections.max(list);
List
//List 有序集合,具有和索引有关的操作
List<Object> list = new ArrayList<>();
list.add((int)index,Object o);
list.get((int)index);
list.remove((int)index);
list.indexOf(Object o);
list.subList(int start,int end); [start,end);
Stack
//Stack(LIFO)
Stack<Object> s = new Stack<>();
s.pop();
s.peek();
s.push(Object o);
//or deque
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1); // [1]
stack.push(2); // [2, 1]
stack.peek(); // 2
stack.isEmpty(); // false
stack.pop(); // 2
Queue
Queue<Integer> q = new LinkedList<>();
offer(E e); // 队尾加入元素e。 若成功入队返回值true,否则返回false --- O(1)
poll(); // 出队头,返回出队元素e --- O(1)
peek(); // 查看队头元素, 返回值队首元素e --- O(1)
isEmpty() // 若队空返回true, 否则返回false --- O(1)
size() // 返回队中元素个数 --- O(1)
Deque
Dueue<Integer> q = new LinkedList<>();
offFirst(Object e) // 将指定元素添加到双端队列的头部 --- O(1)
offLast(Object e) //将指定元素添加到双端队列的尾部 --- O(1)
pollFirst() //获取并删除双端队列的第一个元素 --- O(1)
pollLast() //获取并删除双端队列的最后一个元素 --- O(1)
peekFirst() //获取但不删除双端队列的第一个元素 --- O(1)
peekLast() //获取但不删除双端队列的最后一个元素 --- O(1)
PriorityQueue
Queue<Integer> minH = new PriorityQueue<>(); // 小根堆,默认大小为11 相当于 new PriorityQueue<>(11)
Queue<Integer> minH = new PriorityQueue<>(100); // 定义一个默认容量有100的小根堆。在当中增加元素会扩容,只是开始指定大小。不是size,是capacity
Queue<Integer> maxH = new PriorityQueue<>((i1, i2) -> i2 - i1); // 大根堆,默认大小为11 相当于 new PriorityQueue<>(11, (i1, i2) -> i2 - i1)
Queue<Integer> maxH = new PriorityQueue<>(100, (i1, i2) -> i2 - i1); // 定义一个默认容量有100的大根堆。在当中增加元素会扩容,只是开始指定大小
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(3);// 在堆中加入元素e,并调整堆。若成功入堆返回值true,否则返回false --- O(logN)
pq.add(1);
pq.add(5);
pq.poll();// 弹出堆顶元素,并重新调整堆,返回出队元素e --- O(logN)
pq.peek();// 查看堆顶元素, 返回值堆顶元素e --- O(1)
pq.clear();
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b.compareTo(a));
PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<Node>(){
public int compare(Node n1, Node n2) {
return n1.value - n2.value;
}
})
Set
//Set 不允许重复
HashSet<Object> set = new HashSet<>();
set.add(Object o);
set.contains(Object o);
set.remove(Object o);
isEmpty() // 若集合为空返回true, 否则返回false --- O(1)
first() // 返回集合里的最小值(若给了比较器从大到小则是返回最大值)
HashMap
Map<Characters, Integer> map = new HashMap<>();
put(K key, V value); // 在Map中加入键值对<key, value>。返回value值。如果Map中有key,则replace旧的value --- O(1)
get(K key); // 返回Map中key对应的value。若Map中没有该key,则返回null --- O(1)
getOrDefault(K key, V defaultValue); // 返回Map中key对应的value。若Map中没有该key,则返回defaultValue --- O(1)
// For example: map.put('k', map.getOrDefault('k', 0) + 1);
containsKey(Key key); // 在Map中若存在key,则返回true,否则返回false --- O(1)
keySet(); // 返回一个Set,这个Set中包含Map中所有的Key --- O(1)
// Map<Character, Integer> map = new HashMap<>();
for (Character key : map.keySet()) { // Operate with each key }
for (Integer value : map.values()) { // Operate with each values }
for(Map.Entry<String,String> entry : map.entrySet()){
entry.getKey();
entry.getValue();
}
isEmpty() // 若Map为空返回true, 否则返回false --- O(1)
.size() // 返回Map中中键值对<K, V>的个数 --- O(1)