按值(Java)对地图<key,value =“”>进行分组/排序

时间:2017-07-17 11:16:02

标签: java sorting grouping

我有一个Item对象的LinkedHashMap。项目有itemId和Color。我想对地图数据进行排序和分组,方式是根据插入顺序对地图进行排序,并将颜色分组。

让我举例说明

{98091=ItemVO [itemId='98091', color='Red']
, 78956=ItemVO [itemId='78956', color='Red']
, 23410=ItemVO [itemId='23410', color='Red']
, 32456=ItemVO [itemId='32456', color='Black']
, 10098=ItemVO [itemId='10098', color='Black']
, 12323=ItemVO [itemId='12323', color='Green']
, 11231=ItemVO [itemId='11231', color='Green']
}

对逻辑进行排序和分组后,map应如下所示:

.imgBot{
    position: absolute;
    height: auto;
    width: 100%;
    top: 500px;
    display: flex;
    flex-direction: row;
    text-align: center;
    flex-wrap: wrap;
    left: 70px;
}

基本上,地图应首先包含所有具有红色(首先插入)的项目对象,然后包含具有黑色的项目对象,以及最后一个具有绿色的项目对象。

2 个答案:

答案 0 :(得分:0)

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 + '\'' +
                '}';
    }
}

答案 1 :(得分:0)

使用LinkedHashMapkey:color创建value:List<ItemVO>

迭代itemChildMap并将ItemVO添加到LinkedHashMap(colorItemListMap)

然后迭代colorItemListMap并将所有ItemVO添加到LinkedHashMap(sortedItemMap)

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;
}

sortedItemMap包含已排序和分组的ItemVO对象。