Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据特定键对HashMap项进行分组_Java_Sorting_Arraylist_Hashmap_Grouping - Fatal编程技术网

Java 根据特定键对HashMap项进行分组

Java 根据特定键对HashMap项进行分组,java,sorting,arraylist,hashmap,grouping,Java,Sorting,Arraylist,Hashmap,Grouping,我有一个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

我有一个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”的条目分组在一起,并为它们分配第一种颜色,其他具有下一个类似“NewId”的条目分配第二种颜色,依此类推,直到具有前10个相同“NewId”的条目分配各自的颜色

分组前

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

如何实现这一点?

您需要一个非常令人印象深刻的循环。高级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);
inti=0//或者不管起始id号是什么
int j=0//用于管理颜色分配
//如果需要新的哈希映射,则colorsMap=newhashmap();
for(HashMap loopMap:myList){
而(i

我个人会使用一个类来将数据、标题、描述和颜色数据放在一起,这会使事情变得更简单。我建议,它可以让您添加更好的方法,而不是使用
Arraylist
,而是使用
HashMap
CustomObject
类将保存属性
NewId
Title
Description
。此hashmap的关键是NewId属性。这很重要,因为您为NewId的每个值分配颜色。使用此方法,您可以为地图中的每个条目分配一种颜色。

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

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;
    }
}
公共类MyList{
private ArrayList list=new ArrayList();
公共布尔添加(哈希映射){
返回列表。添加(地图);
}
公共void setColor(字符串newId,字符串颜色){
for(HashMap m:list)
如果(m.containsKey(newId))
m、 放置(“颜色”,颜色);
}
公共字符串getGroupKey(字符串键,int i){
ArrayList uniqeList=getUniqKeyList(键);
Collections.sort(uniqeList);
返回uniqeList.get(i);
}
公共ArrayList getUniqKeyList(字符串键){
ArrayList l=新的ArrayList();
for(HashMap m:list)
如果(!l.contains(m.get(key)))
l、 添加(m.get(key));
返回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++]);
        }       
    }
publicstaticvoidmain(字符串[]args)引发异常{
MyList MyList=新建MyList();
HashMap=newHashMap();
map.put(“NewId”,NewId);
地图放置(“标题”,标题);
地图放置(“说明”,说明);
添加(地图);
字符串[]颜色=新字符串[]{“#1F1A17”、“#62934D”、“#B694D1”};
int i=0;
while(true){
if(i==colors.length)
打破
字符串s=myList.getGroupKey(“NewId”,i);
如果(s==null)
打破
其他的
setColor(s,colors[i++]);
}       
}

My newId来自JSON响应。如何首先对类似ID进行分组?您的解决方案是否会将所有ID放入阵列中?如何将此列表传递给ListAdapter?如何将其传递给简单列表?我正在尝试添加列表以显示ListView,但仍然收到相同的错误
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++]);
        }       
    }