1:对象实现Comparable, 那么对象就具有了比较功能



package comparableAndComparator;

import java.util.Collections;
import java.util.LinkedList;

/**
* @program: GradleTestUseSubModule
* @author: Yafei Li
* @create: 2018-09-10 16:18
**/
public class Main {
public static void main(String[] args){
LinkedList<Person> linkedList = new LinkedList<>();
linkedList.add(new Person("wangwu"));
linkedList.add(new Person("lisi"));
linkedList.add(new Person("zhangsan"));
linkedList.stream().forEach(person -> {
System.out.println(person.name);
});
Collections.sort(linkedList);
System.out.println("--------------------------");
linkedList.stream().forEach(person -> {
System.out.println(person.name);
});
}
}

class Person implements Comparable<Person>{

public String name;

public Person(String name) {
this.name = name;
}

public int compareTo(Person person) {
return person.name.compareTo(name);
}
}


结果:



wangwu
lisi
zhangsan
--------------------------
zhangsan
wangwu
lisi


 2:单独定义一个比较器,用于比较Person对象



package comparableAndComparator;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

/**
* @program: GradleTestUseSubModule
* @author: Yafei Li
* @create: 2018-09-10 16:18
**/
public class Main {
public static void main(String[] args){
LinkedList<Person> linkedList = new LinkedList<>();
linkedList.add(new Person("wangwu"));
linkedList.add(new Person("lisi"));
linkedList.add(new Person("zhangsan"));
linkedList.stream().forEach(person -> {
System.out.println(person.name);
});
Collections.sort(linkedList,new PersonComparator());
System.out.println("--------------------------");
linkedList.stream().forEach(person -> {
System.out.println(person.name);
});
}
}

class Person{

public String name;

public Person(String name) {
this.name = name;
}
}

class PersonComparator implements Comparator<Person>{
public int compare(Person p1, Person p2) {
return p1.name.compareTo(p2.name);
}
}


结果:



wangwu
lisi
zhangsan
--------------------------
lisi
wangwu
zhangsan


3:使用比较器对 map进行排序



package com.li.jingdong;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
* @program: GradleTestUseSubModule
* @author: Yafei Li
* @create: 2018-09-10 10:37
**/
public class Main5 {

public static void salaryfrequeny(int num,int[] salaries) throws NoSuchFieldException, IllegalAccessException {
Map<Integer, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < num; i++) {
map.computeIfPresent(salaries[i],(k,v)->{
return v+1;
});
map.putIfAbsent(salaries[i], 1);
}
Map<Integer, Integer> sortMap = sortByKey(map);
System.out.println(sortMap);
}

//根据值对map进行排序
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
Map<K, V> result = new LinkedHashMap<>();
Stream<Map.Entry<K, V>> st = map.entrySet().stream();
st.sorted(Comparator.comparing(e -> e.getValue())).forEach(e -> result.put(e.getKey(), e.getValue()));
return result;
}
//根据键对map排序
public static <K extends Comparable,V> Map<K,V> sortByKey(Map<K,V> map) {
Map<K, V> resultMap = new LinkedHashMap<>();
Stream<Map.Entry<K, V>> stream = map.entrySet().stream();
//(a,b)->b.getKey().compareTo(a.getKey()) 是一个比较器
stream.sorted((a,b)->b.getKey().compareTo(a.getKey())).forEach(e->{ //e就是挨个取出map中的Entry,
System.out.println("key:"+e.getKey()+" value:"+e.getValue());
resultMap.put(e.getKey(), e.getValue());
});
return resultMap;
}


public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

int n=5;
int[] salary = {1000, 2000, 1000, 3000, 2000};
salaryfrequeny(n,salary);
}
}


 4:通过反射O(1)的时间获取LinkedHashMap的尾节点



Map<Integer, Integer> map = new LinkedHashMap<>();            
Field tail = map.getClass().getDeclaredField("tail");
tail.setAccessible(true);
Map.Entry<Integer,Integer> entry=(Map.Entry<Integer, Integer>) tail.get(map);
Integer key = entry.getKey();
Integer value = entry.getValue();


 


本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究