I have a LinkedHashMap of Item objects. Item has itemId and Color. I want to sort and group my map data, in a way that map is sorted based on insertion sequence, along with colors grouped.
Let me demonstrate with example
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);
After sorting and grouping logic, map should be as below:
{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']
}
Basically, map should contain first all item objects having Red color (which got inserted first), then item objects having Black color, and in the last item objects having Green color.