package test;
public class Test {
public static void main(String[] args) throws IOException {
Map<String, String> sourceMap = new HashMap<>();
sourceMap.put( "2019-1", "哈哈哈" );
sourceMap.put( "2019-3", "a哈哈是的" );
sourceMap.put( "2019-20", "a哈哈是的" );
sourceMap.put( "2019-15", "a哈哈是的" );
Map<String, String> sortMap = new TreeMap<String, String>(
new Comparator<String>() {
public int compare(String obj1, String obj2) {
String ke1 = obj1.split( "-" )[1];
String ke2 = obj2.split( "-" )[1];
if (Integer.parseInt( ke1 ) > Integer.parseInt( ke2 )) {
return 1;
} else if ((Integer.parseInt( ke1 ) < Integer.parseInt( ke2 ))) {
return -1;
} else {
return 0;
}
}
});
sourceMap.forEach( new BiConsumer<String, String>() {
@Override
public void accept(String key, String value) {
sortMap.put( key,value );
}
} );
sortMap.forEach( new BiConsumer<String, String>() {
@Override
public void accept(String key, String value) {
System.out.println(key+">>"+value);
}
} );
}