Java常用
package kemuyi;
import com.sun.jmx.remote.internal.ArrayQueue;
import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class KS20220107_1 {
@Test
public void testStack() {
// Stack
Deque<Integer> stack = new LinkedList<>(); // ArrayDeque
stack.push(1);
stack.push(2);
stack.peek();
stack.pop();
stack.isEmpty();
}
@Test
public void testQueue() {
// Queue
Queue<Integer> queue = new LinkedList<>(); // ArrayDeque
queue.offer(1);
queue.add(2);
queue.poll();
queue.remove();
queue.isEmpty();
//Deque
Deque<Integer> deque = new LinkedList<>(); // ArrayDeque
deque.offerFirst(1);
deque.offerFirst(2);
System.out.println("Deque:" + deque.poll()); // 2
}
@Test
public void testList() {
List<Student> students = Arrays.asList(
new Student(1, 24, 180),
new Student(2, 21, 170),
new Student(3, 24, 165));
Collections.sort(students, (o1, o2) -> {
if (o1.age == o2.age) {
return o1.height - o2.height;
}
return o1.age - o2.age;
});
System.out.println(students);
}
@Test
public void testSet() {
Set<Student> hashSet = new HashSet<>();
hashSet.add(new Student(1, 24, 180));
hashSet.add(new Student(2, 21, 170));
hashSet.add(new Student(3, 24, 165));
// 对HashSet排序的话,必须返回LinkedHashSet
Set<Student> collect = hashSet.stream().sorted((o1, o2) -> {
if (o1.age == o2.age) {
return o1.height - o2.height;
}
return o1.age - o2.age;
}).collect(Collectors.toCollection(LinkedHashSet::new));
System.out.println("HashSet排序:" + collect);
// LinkedHashSet容器中元素的顺序和插入顺序保持一致
Set<Student> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(new Student(1, 24, 180));
linkedHashSet.add(new Student(2, 21, 170));
linkedHashSet.add(new Student(3, 24, 165));
linkedHashSet = linkedHashSet.stream().sorted((o1, o2) -> {
if (o1.age == o2.age) {
return o1.height - o2.height;
}
return o1.age - o2.age;
}).collect(Collectors.toCollection(LinkedHashSet::new));
System.out.println("LinkedHashSet排序:" + linkedHashSet);
// TreeSet可以在构造的时候传入比较器,也可以和上面方式一样stream遍历sorted()
Set<Student> treeSet = new TreeSet<>((o1, o2) -> {
if (o1.age == o2.age) {
return o1.height - o2.height;
}
return o1.age - o2.age;
});
treeSet.add(new Student(1, 24, 180));
treeSet.add(new Student(2, 21, 170));
treeSet.add(new Student(3, 24, 165));
System.out.println("TreeSet排序:" + treeSet);
treeSet.removeIf(student -> student.equals(new Student(1, 24, 180)));
System.out.println("TreeSet遍历删除:" + treeSet);
}
@Test
public void testMap() {
Map<String, Integer> strMap = new HashMap<>();
strMap.put("A", 3);
strMap.put("B", 5);
strMap.put("C", 1);
strMap.put("E", 9);
strMap.put("D", 1);
System.out.println("HashMap key默认升序排列:" + strMap);
strMap = strMap.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (k1, k2) -> k2, LinkedHashMap::new));
System.out.println("HashMap 按Value降序排列:" + strMap);
strMap = strMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (k1, k2) -> k2, LinkedHashMap::new));
System.out.println("HashMap 按Value升序排列:" + strMap);
System.out.println();
Map<Integer, Student> map = new HashMap<>();
map.put(1, new Student(1, 24, 180));
map.put(3, new Student(3, 24, 165));
map.put(2, new Student(2, 21, 170));
System.out.println("原始HashMap数据:" + map);
map = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue((o1, o2) -> {
if (o1.age == o2.age) {
return o1.height - o2.height;
}
return o1.age - o2.age;
}))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> x, LinkedHashMap::new));
System.out.println("HashMap 按value排序:" + map);
// key:student value:count
Map<Student, Integer> map2 = new HashMap<>();
map2.put(new Student(1, 24, 180), 12);
map2.put(new Student(3, 24, 165), 8);
map2.put(new Student(2, 21, 170), 9);
map2 = map2.entrySet().stream()
.sorted(Map.Entry.comparingByKey((o1, o2) -> {
if (o1.age == o2.age) {
return o1.height - o2.height;
}
return o1.age - o2.age;
}))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> x, LinkedHashMap::new));
System.out.println("HashMap 按key排序:" + map2);
map2.entrySet().removeIf(entry -> entry.getKey().equals(new Student(3, 24, 165)));
System.out.println("HashMap 遍历删除:" + map2);
}
@Test
public void testTreeMap() {
TreeMap<Integer, List<Student>> treeMap = new TreeMap<>();
treeMap.computeIfAbsent(4, k -> new LinkedList<>()).add(new Student(1, 24, 180));
treeMap.computeIfAbsent(8, k -> new LinkedList<>()).add(new Student(3, 24, 165));
treeMap.computeIfAbsent(12, k -> new LinkedList<>()).add(new Student(2, 21, 170));
System.out.println(treeMap.ceilingKey(5));
System.out.println(treeMap.floorKey(5));
System.out.println(treeMap.firstKey());
treeMap.ceilingEntry(5);
treeMap.floorEntry(5);
treeMap.computeIfPresent(8, (key, students) -> {
students.add(new Student(1, 1, 1));
return students;
});
// 常用法
if (treeMap.containsKey(8)) {
treeMap.get(8).add(new Student(1, 1, 1));
} else {
treeMap.computeIfAbsent(8, k -> new LinkedList<>()).add(new Student(3, 3, 3));
}
// 遍历
treeMap.forEach((k, v) -> System.out.println(k + ":" + v));
Collection<List<Student>> values = treeMap.values();
}
@Test
public void testPQ() {
Map<Integer, Student> map = new HashMap<>();
map.put(1, new Student(1, 24, 180));
map.put(3, new Student(3, 24, 165));
map.put(2, new Student(2, 21, 170));
PriorityQueue<Student> priorityQueue = new PriorityQueue<>((o1, o2) -> {
if (o1.age == o2.age) {
return o1.height - o2.height;
}
return o1.age - o2.age;
});
priorityQueue.addAll(map.values());
}
public static void main(String[] args) {
}
}
class Student {
public int id;
public int age;
public int height;
public Student(int id, int age, int height) {
this.id = id;
this.age = age;
this.height = height;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id && age == student.age && height == student.height;
}
@Override
public int hashCode() {
return Objects.hash(id, age, height);
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", age=" + age +
", height=" + height +
'}';
}
}