import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class AccordingToKeyOrValueComparison {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
("1", "4");
("3", "3");
("2", "2");
("4", "1");
// Map<String, String> resultMap = sortMapByKey(map);
// //按Key进行排序:的性质
Map<String, String> resultMap = sortMapByKey2(map); // 按key进行排序:,自定义比较器(排序后需要LinkedHashMap保存)
// Map<String, String> resultMap = sortMapByValue(map);
// //按Value进行排序:list,自定义比较器
for (<String, String> entry : ()) {
(() + " " + ());
}
}
/**
* 使用 TreeMap的性质按key进行排序
*
* @param map
* @return
*/
public static Map<String, String> sortMapByKey(Map<String, String> map) {
if (map == null || ()) {
return null;
}
// TreeMap默认用key的自然排序,所以不用声明比较器也可以实现key排序,比较器可以自定义排序规则,比如倒序
// Map<String, String> sortMap = new TreeMap<String, String>();
// TreeMap构造方法可以有比较器参数~但是比较器只能是对key进行比较
Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());
(map);
return sortMap;
}
/**
* 使用 list按key进行排序
*
* @param map
* @return
*/
public static Map<String, String> sortMapByKey2(Map<String, String> map) {
if (map == null || ()) {
return null;
}
List<<String, String>> list = new ArrayList<>(());
(list, new MapKeyComparator2());
Map<String, String> sortMap = new LinkedHashMap<>();
Iterator<<String, String>> iterable = ();
while (()) {
<String, String> tmpEntry = ();
((), ());
}
return sortMap;
}
/**
* 使用 List对Map按value进行排序
*
* @param oriMap
* @return
*/
public static Map<String, String> sortMapByValue(Map<String, String> oriMap) {
if (oriMap == null || ()) {
return null;
}
// 一定是LinkedHashMap,因为LinkedHashMap保证put顺序和输出顺序一致!
Map<String, String> sortedMap = new LinkedHashMap<>();
// 把map的<key,value>当节点装进list,对list排序
List<<String, String>> entryList = new ArrayList<>(());
(entryList, new MapValueComparator());
Iterator<<String, String>> iter = ();
<String, String> tmpEntry = null;
while (()) {
tmpEntry = ();
((), ());
}
return sortedMap;
}
}
class MapKeyComparator implements Comparator<String> {
@Override
public int compare(String str1, String str2) {
return (str1);
}
}
class MapValueComparator implements Comparator<<String, String>> {
@Override
public int compare(<String, String> me1, <String, String> me2) {
return ().compareTo(());
}
}
class MapKeyComparator2 implements Comparator<<String, String>> {
@Override
public int compare(<String, String> me1, <String, String> me2) {
return ().compareTo(());
}
}
转载:http://blog./13580976/2147994
public class SortMapByValueUtils {
/**
* needResult为true时Map按value从大到小排序,返回排序后的Map的Key组成的List
* needResult为false时Map按value从大到小排序,返回排序后的Map的Key组成的List的反顺序
* 根据距离、长度等进行排序,获取排序后需要的对应类
*/
public static <K> List<K> sortMapByValue(final Map<K, Double> oriMap,
final boolean needResult) {
if (oriMap == null || ()) {
return null;
}
final List<K> sortedList = new ArrayList<>(());
// 把map的<key,value>当节点装进list,对list排序
final List<<K, Double>> entryList = new ArrayList<>(());
(new MapValueComparator<>());
final Iterator<<K, Double>> iter = ();
<K, Double> tmpEntry = null;
while (()) {
tmpEntry = ();
(());
}
if (!needResult) {
return sortedList;
} else {
return (sortedList);
}
}
private static class MapValueComparator<K> implements Comparator<<K, Double>> {
@Override
public int compare(final <K, Double> me1,
final <K, Double> me2) {
return ().compareTo(());
}
}
}
((o1, o2) -> Double
.compare(MathToMath2.toPoint2d(o2).distanceTo(doorMidPoint2d),
MathToMath2.toPoint2d(o1).distanceTo(doorMidPoint2d)));
((o1, o2) -> ((), ()));
((o -> (
())));
((o1, o2) -> {
Double distanceMin1 = Double.MAX_VALUE;
double distanceMin2 = Double.MAX_VALUE;
for (final RoomProxy balcony : adjacentBalconyList) {
final double distanceToTvCabinet1 =
().distanceTo(
MathToMath2.toPoint2d(()));
final double distanceToTvCabinet2 =
().distanceTo(
MathToMath2.toPoint2d(()));
distanceMin1 = (distanceMin1, distanceToTvCabinet1);
distanceMin2 = (distanceMin2, distanceToTvCabinet2);
}
return (distanceMin2);
});
((LineSeg2d::length));