【问题标题】:Group HashMap items according to a specific key根据特定键对 HashMap 项进行分组
【发布时间】: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


    【解决方案1】:

    您可以添加自定义列表类:

    public class MyList {
    
        private ArrayList<HashMap<String, String>> list = new ArrayList<>();
    
        public boolean add(HashMap<String, String> map) {
            return list.add(map);
        }
    
        public void setColor(String newId, String color) {
            for (HashMap<String, String> m : list)
                if (m.containsKey(newId))
                    m.put("color", color);
        }
    
        public String getGroupKey(String key, int i) {      
            ArrayList<String> uniqeList = getUniqKeyList(key);
            Collections.sort(uniqeList);
            return uniqeList.get(i);
        }
    
        public ArrayList<String> getUniqKeyList(String key){
            ArrayList<String> l = new ArrayList<>();
            for (HashMap<String, String> m : list)
                if(!l.contains(m.get(key)))
                    l.add(m.get(key));
            return l;
        }
    }
    

    总的来说,每件事都清晰而简单:

    public static void main(String[] args) throws Exception {
            MyList myList = new MyList();
            HashMap<String,String> map = new HashMap<String,String>();
            map.put("NewId", newId);
            map.put("Title", title);
            map.put("Description", description);
            myList.add(map);
    
            String[] colors = new String[]{"#1F1A17", "#62934D","#B694D1"};
    
            int i=0;
            while (true) {
                        if(i == colors.length) 
                                break;
                String s =  myList.getGroupKey("NewId", i);
                if(s == null)
                    break;
                else 
                    myList.setColor(s, colors[i++]);
            }       
        }
    

    【讨论】:

    • 我的 newId 来自 JSON 响应。我如何首先对相似的 id 进行分组?您的解决方案不会将所有 id 放入数组中
    • 如何将此列表传递给 ListAdapter?
    • 一个简单的列表怎么能这样?
    • 我正在尝试添加列表以显示 ListView
    • 仍然出现同样的错误
    【解决方案2】:

    我建议不要使用Arraylist&lt;HashMap&lt;string, string&gt;&gt;,而是使用HashMap&lt;string, List&lt;MyCustomObject&gt;&gt;CustomObject 类将包含属性NewIdTitleDescription。此哈希图的键是 NewId 属性。这很重要,因为您正在为 NewId 的每个值分配颜色。使用这种方法,您可以为地图中的每个条目分配一种颜色。

    【讨论】:

      【解决方案3】:

      您将需要一个令人印象深刻的循环。高级 for 循环确实会有所帮助。如果我没听错的话,你想要这样的东西

      int i = 0; //Or whatever the starting id number is
      int j = 0; //For managing the color assigning
      //If you want a new hash map Map<String, String> colorsMap = new HashMap<>();    
      
      
      for (HashMap<String, String> loopMap : myList) {
           while (i < colors.length) {
               if (loopMap.containsKey("" + i) {
                   loopMap.put("" + i + "color", colors[j]);
                   //Or if you want to make a new HashMap: colorsMap.put("" + i, colors[j]);
               }
               i++; //Now start with next id entry
               j++; //And next color
           }
           i = 0; //Or starting id num
           j = 0; //Starting color index
      } 
      //If you are making a new hash map: myList.add(colorsMap);
      

      我个人会使用一个类来将数据、标题、描述和颜色数据放在一起,这会让事情变得简单得多。它会让你添加更好的方法

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 2014-04-27
        • 2022-11-19
        • 1970-01-01
        • 1970-01-01
        • 2022-08-16
        • 1970-01-01
        • 2019-03-23
        相关资源
        最近更新 更多