Java 如何对包含值映射的映射进行排序
我的数据结构如下:Java 如何对包含值映射的映射进行排序,java,sorting,hashmap,Java,Sorting,Hashmap,我的数据结构如下: class SKUPriceVO { String skuId; Map<String, PriceVo> priceMap; } class PriceVo { String type; String skuId; double price; } 类SKUPriceVO{ 线状臭鼬; 地图价格地图; } 类PriceVo{ 字符串类型; 线状臭鼬; 双倍价格; } 我需要排序的地图是: MapmyMap,按照Pric
class SKUPriceVO {
String skuId;
Map<String, PriceVo> priceMap;
}
class PriceVo {
String type;
String skuId;
double price;
}
类SKUPriceVO{
线状臭鼬;
地图价格地图;
}
类PriceVo{
字符串类型;
线状臭鼬;
双倍价格;
}
我需要排序的地图是:
Map
myMap,按照PriceVo对象ie>myMap中的价格进行排序的方式,应根据PriceVo中的价格按升序或降序排列SkupPriceVO。您不能直接对hashmap进行排序。您需要做的是将地图数据移动到列表中,并根据价格对该列表进行排序
List<Map.Entry<Integer, PriceVo >> list = new ArrayList<Map.Entry<Integer, PriceVo >>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, PriceVo >>() {
@Override
public int compare(Map.Entry<Integer, PriceVo > price1,
Map.Entry<Integer, PriceVo > price2) {
return price1.getValue().price.compareTo(price2.getValue().price);
}
}
);
List List=newarraylist(map.entrySet());
Collections.sort(list,newcomparator(){
@凌驾
公共整数比较(地图输入价格1,
地图输入价格(2){
返回price1.getValue().price.compareTo(price2.getValue().price);
}
}
);
您甚至可以使用树映射并直接将比较器传递给树映射并进行排序,因此无需复制其他列表中的数据。对映射进行排序是什么意思?但是,这只会对SkupPriceVO对象中的priceMap进行排序。那么我们如何根据PriceVo价格对外部地图进行排序,即“myMap”?与您对priceMap进行排序的方式相同。