Java教程

Java常用

本文主要是介绍Java常用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import com.sun.jmx.remote.internal.ArrayQueue;
import org.junit.Test;

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class KS20220107_1 {
public static void main(String[] args) {

    // Stack
    Deque<Integer> stack = new LinkedList<>(); // ArrayDeque
    stack.push(1);
    stack.peek();
    stack.pop();
    stack.isEmpty();


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

    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    Collections.sort(list, (x, y) -> y - x); // 比较器
    System.out.println("ArrayList:" + list);

    // Map
    HashMap<Integer, Integer> map = new HashMap<>();
    map.put(2, 1);
    map.put(1, 2);
    System.out.println("HashMap:" + map); // hashmap key默认升序 (integer)

    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(Map.Entry.comparingByValue())
.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);

    LinkedHashMap<Integer, Integer> map2 = new LinkedHashMap<>();
    map2.put(2, 1);
    map2.put(1, 2);
    System.out.println("LinkedHashMap:" + map2);

    LinkedHashMap<String, Student> linkedHashMap = new LinkedHashMap<>();
    linkedHashMap.put("ab", new Student(1, 3));
    linkedHashMap.put("c", new Student(2, 3));
    linkedHashMap.put("e", new Student(1, 2));

    linkedHashMap = linkedHashMap.entrySet().stream()
            .sorted((o1, o2) -> {
                Student s1 = o1.getValue();
                Student s2 = o2.getValue();
                if (s1.score == s2.score) {
                    return s1.id - s2.id;
                }
                return s1.score - s2.score;
            })
            .collect(Collectors.toMap(k -> k.getKey(), k -> k.getValue(), (k1, k2) -> k2, LinkedHashMap::new));
    System.out.println("LinkedHashMap:" + linkedHashMap);
}

}

class Student implements Comparable {
public int id;
public int score;

public Student(int id, int score) {
    this.id = id;
    this.score = score;
}

@Override
public int compareTo(Student o) {
    if (this.id == o.id) {
        return this.score - o.score;
    }
    return this.id - o.id;
}

@Override
public String toString() {
    return "Student{" +
            "id=" + id +
            ", score=" + score +
            '}';
}

}

这篇关于Java常用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!