对地图进行分组/排序<;关键、价值>;按值(Java)
我有一个项目对象的LinkedHashMap。项目具有itemId和颜色。我想对我的地图数据进行排序和分组,以一种基于插入序列和分组颜色对地图进行排序的方式 让我举例说明对地图进行分组/排序<;关键、价值>;按值(Java),java,sorting,grouping,Java,Sorting,Grouping,我有一个项目对象的LinkedHashMap。项目具有itemId和颜色。我想对我的地图数据进行排序和分组,以一种基于插入序列和分组颜色对地图进行排序的方式 让我举例说明 Map<String, ItemVO> itemChildMap = new LinkedHashMap<String, ItemVO>(); ItemVO item1 = new ItemVO("98091", "Red"); ItemVO item2 = new ItemVO("3
Map<String, ItemVO> itemChildMap = new LinkedHashMap<String, ItemVO>();
ItemVO item1 = new ItemVO("98091", "Red");
ItemVO item2 = new ItemVO("32456", "Black");
ItemVO item3 = new ItemVO("12323", "Green");
ItemVO item4 = new ItemVO("78956", "Red");
ItemVO item5 = new ItemVO("11231", "Green");
ItemVO item6 = new ItemVO("10098", "Black");
ItemVO item7 = new ItemVO("23410", "Red");
itemChildMap.put("98091", item1);
itemChildMap.put("32456", item2);
itemChildMap.put("12323", item3);
itemChildMap.put("78956", item4);
itemChildMap.put("11231", item5);
itemChildMap.put("10098", item6);
itemChildMap.put("23410", item7);
基本上,映射应该首先包含所有红色的项目对象(首先插入),然后是黑色的项目对象,最后是绿色的项目对象。publicstaticvoidmain(String[]args){
public static void main(String[] args){
Map<String, ItemVO> itemChildMap = new LinkedHashMap<String, ItemVO>();
ItemVO item1 = new ItemVO("100", "Black");
ItemVO item2 = new ItemVO("101", "Red");
ItemVO item3 = new ItemVO("102", "Black");
ItemVO item4 = new ItemVO("103", "Green");
ItemVO item5 = new ItemVO("104", "Red");
ItemVO item6 = new ItemVO("105", "Green");
ItemVO item7 = new ItemVO("106", "Black");
itemChildMap.put("100", item1);
itemChildMap.put("101", item2);
itemChildMap.put("102", item3);
itemChildMap.put("103", item4);
itemChildMap.put("104", item5);
itemChildMap.put("105", item6);
itemChildMap.put("106", item7);
List<Map.Entry<String, ItemVO>> entries = new ArrayList<>(itemChildMap.entrySet());
Comparator<Map.Entry<String, ItemVO>> comparatorByColor = new Comparator<Map.Entry<String, ItemVO>>() {
@Override
public int compare(Map.Entry<String, ItemVO> o1, Map.Entry<String, ItemVO> o2) {
return o1.getValue().getColor().compareTo(o2.getValue().getColor());
}
};
Comparator<Map.Entry<String, ItemVO>> comparatorById = new Comparator<Map.Entry<String, ItemVO>>() {
@Override
public int compare(Map.Entry<String, ItemVO> o1, Map.Entry<String, ItemVO> o2) {
return o1.getValue().getId().compareTo(o2.getValue().getId());
}
};
System.out.println(itemChildMap);
Collections.sort(entries, comparatorByColor.thenComparing(comparatorById));
itemChildMap.clear();
for (Map.Entry<String, ItemVO> entry : entries) {
itemChildMap.put(entry.getKey(), entry.getValue());
}
System.out.println(itemChildMap);
}
static class ItemVO {
String id;
String color;
public ItemVO(String id, String color) {
this.id = id;
this.color = color;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "ItemVO{" +
"id='" + id + '\'' +
", color='" + color + '\'' +
'}';
}
}
Map itemChildMap=newlinkedhashmap();
ItemVO item1=新的ItemVO(“100”,“黑色”);
ItemVO item2=新的ItemVO(“101”,“红色”);
ItemVO item3=新的ItemVO(“102”,“黑色”);
ItemVO item4=新的ItemVO(“103”,“绿色”);
ItemVO item5=新的ItemVO(“104”,“红色”);
ItemVO item6=新的ItemVO(“105”,“绿色”);
ItemVO item7=新的ItemVO(“106”,“黑色”);
itemChildMap.put(“100”,item1);
itemChildMap.put(“101”,item2);
itemChildMap.put(“102”,item3);
itemChildMap.put(“103”,item4);
itemChildMap.put(“104”,item5);
itemChildMap.put(“105”,item6);
itemChildMap.put(“106”,item7);
列表条目=新的ArrayList(itemChildMap.entrySet());
比较器比较器颜色=新比较器(){
@凌驾
公共整数比较(Map.Entry o1,Map.Entry o2){
返回o1.getValue().getColor().compareTo(o2.getValue().getColor());
}
};
比较器comparatorById=新比较器(){
@凌驾
公共整数比较(Map.Entry o1,Map.Entry o2){
返回o1.getValue().getId().compareTo(o2.getValue().getId());
}
};
System.out.println(itemChildMap);
Collections.sort(条目,comparatorByColor.ThenComparating(comparatorById));
itemChildMap.clear();
对于(Map.Entry:entries){
itemChildMap.put(entry.getKey(),entry.getValue());
}
System.out.println(itemChildMap);
}
静态类ItemVO{
字符串id;
字符串颜色;
公共项VO(字符串id、字符串颜色){
this.id=id;
这个颜色=颜色;
}
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getColor(){
返回颜色;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
@凌驾
公共字符串toString(){
返回“ItemVO{”+
“id=”+id+“\”+
,color=''+color+'\''+
'}';
}
}
使用键:颜色和值:列表创建一个LinkedHashMap
迭代itemChildMap
并将ItemVO添加到LinkedHashMap(colorItemListMap)
然后迭代colorItemListMap
并将所有ItemVO添加到LinkedHashMap(sortedItemMap)
publicmap-sortItemMap(Map-itemChildMap){
Map sortedItemMap=新建LinkedHashMap();
Map colorItemListMap=新建LinkedHashMap();
对于(Map.Entry:itemChildMap.entrySet()){
字符串颜色=itemEntry.getValue().getColor();
如果(!colorItemListMap.containsKey(颜色)){
列表=新的ArrayList();
添加(itemEntry.getValue());
colorItemListMap.put(颜色,列表);
}否则{
colorItemListMap.get(color.add(itemEntry.getValue());
}
}
for(条目:colorItemListMap.entrySet()){
对于(ItemVO itemObj:entry.getValue())
sortedItemMap.put(itemObj.getItemId(),itemObj);
}
系统输出打印项次(sortedItemMap);
返回MMAP;
}
sortedItemMap
包含排序和分组的ItemVO对象。我曾经需要同样的东西。这有助于:分组后是否还要保持组中的插入顺序?嘿@mumpitz/@OH good SPIDERS,在该解决方案中,只涉及排序,我已经检查过了。嘿@AshishLohia,如果我们也能保持组中的插入顺序就好了(不是强制性的)
public static void main(String[] args){
Map<String, ItemVO> itemChildMap = new LinkedHashMap<String, ItemVO>();
ItemVO item1 = new ItemVO("100", "Black");
ItemVO item2 = new ItemVO("101", "Red");
ItemVO item3 = new ItemVO("102", "Black");
ItemVO item4 = new ItemVO("103", "Green");
ItemVO item5 = new ItemVO("104", "Red");
ItemVO item6 = new ItemVO("105", "Green");
ItemVO item7 = new ItemVO("106", "Black");
itemChildMap.put("100", item1);
itemChildMap.put("101", item2);
itemChildMap.put("102", item3);
itemChildMap.put("103", item4);
itemChildMap.put("104", item5);
itemChildMap.put("105", item6);
itemChildMap.put("106", item7);
List<Map.Entry<String, ItemVO>> entries = new ArrayList<>(itemChildMap.entrySet());
Comparator<Map.Entry<String, ItemVO>> comparatorByColor = new Comparator<Map.Entry<String, ItemVO>>() {
@Override
public int compare(Map.Entry<String, ItemVO> o1, Map.Entry<String, ItemVO> o2) {
return o1.getValue().getColor().compareTo(o2.getValue().getColor());
}
};
Comparator<Map.Entry<String, ItemVO>> comparatorById = new Comparator<Map.Entry<String, ItemVO>>() {
@Override
public int compare(Map.Entry<String, ItemVO> o1, Map.Entry<String, ItemVO> o2) {
return o1.getValue().getId().compareTo(o2.getValue().getId());
}
};
System.out.println(itemChildMap);
Collections.sort(entries, comparatorByColor.thenComparing(comparatorById));
itemChildMap.clear();
for (Map.Entry<String, ItemVO> entry : entries) {
itemChildMap.put(entry.getKey(), entry.getValue());
}
System.out.println(itemChildMap);
}
static class ItemVO {
String id;
String color;
public ItemVO(String id, String color) {
this.id = id;
this.color = color;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "ItemVO{" +
"id='" + id + '\'' +
", color='" + color + '\'' +
'}';
}
}
public Map<String, ItemVO> sortItemMap(Map<String, ItemVO> itemChildMap) {
Map<String, ItemVO> sortedItemMap = new LinkedHashMap<>();
Map<String, List<ItemVO>> colorItemListMap = new LinkedHashMap<String, List<ItemVO>>();
for (Map.Entry<String, ItemVO> itemEntry : itemChildMap.entrySet()) {
String color = itemEntry.getValue().getColor();
if (!colorItemListMap.containsKey(color)) {
List<ItemVO> list = new ArrayList<ItemVO>();
list.add(itemEntry.getValue());
colorItemListMap.put(color, list);
} else {
colorItemListMap.get(color).add(itemEntry.getValue());
}
}
for (Entry<String, List<ItemVO>> entry : colorItemListMap.entrySet()) {
for (ItemVO itemObj : entry.getValue())
sortedItemMap.put(itemObj.getItemId(), itemObj);
}
System.out.println(sortedItemMap);
return sortedItemMap;
}