【发布时间】:2014-06-07 17:01:41
【问题描述】:
我有一个 ArrayList>>,其中包含某些键值条目。喜欢:-
ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("NewId", newId);
map.put("Title", title);
map.put("Description", description);
myList.add(map);
“NewId”对于多个条目可以是相似的。
我还有一个颜色数组:-
String[] colors = new String[]{"#1F1A17", "#62934D", "#F9B03F", "#7959BC", "#74B8DE", "#E65641", "#7CC8BB", "#D7CE5D", "#D6BE95", "#B694D1"};
我现在想将具有相同“NewId”的所有条目组合在一起,并为它们分配第一种颜色,下一个类似“NewId”的其他条目使用第二种颜色,依此类推,直到分配了前 10 个相同“NewId”的项目用它们各自的颜色。
eg:- 分组前
NewId Title Description
101 title1 des1
102 title2 des2
103 title3 des3
101 title4 des4
102 title5 des5
103 title6 des6
分组后
NewId Title Description
101 title1 des1 ------> color1
101 title4 des4 ------> color1
102 title2 des2 ------> color2
102 title5 des5 ------> color2
103 title3 des3 ------> color3
103 title6 des6 ------> color3
我怎样才能做到这一点?
【问题讨论】:
标签: java sorting arraylist hashmap grouping