Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 如何关联具有相同键属性的对象列表_Java_Arrays_Spring Mvc_Arraylist - Fatal编程技术网

Java 如何关联具有相同键属性的对象列表

Java 如何关联具有相同键属性的对象列表,java,arrays,spring-mvc,arraylist,Java,Arrays,Spring Mvc,Arraylist,如果对象的特定键属性匹配,我将尝试解析自定义对象 我得到的数据格式如下 清单是这样的 我试图基于一个特定的关键属性(即rawmateriad)对它们进行分类 例如,如果索引[0]的rawMaterialId=6743与索引[1]匹配,则两者都需要连接 期望列表 我尝试使用For循环,但没有得到预期的输出 我试过的代码 for(inti=0;i为什么不使用map,key作为rawmateriad,value作为数据列表 Map<Integer,List<Data>> map

如果对象的特定键属性匹配,我将尝试解析自定义对象

我得到的数据格式如下

清单是这样的

我试图基于一个特定的关键属性(即rawmateriad)对它们进行分类

例如,如果索引[0]的rawMaterialId=6743与索引[1]匹配,则两者都需要连接

期望列表

我尝试使用For循环,但没有得到预期的输出

我试过的代码


for(inti=0;i为什么不使用map,key作为rawmateriad,value作为数据列表

Map<Integer,List<Data>> map= new HashMap<Integer,List<Data>>();
for(int i=0; i<data.length;i++)
{
if(map.contains(data[i].rawMaterialId))    {
   List<Data> list=map.get(data[i].rawMaterialId);
list.add(data[i]);
}
else    {
 List<Data> list=new ArrayList<Data>();
list.add(data[i]);
map.put(data[i]);
}
}
Map Map=newhashmap();
for(int i=0;i
for(int i=0;i
若你们有数组数据,你们可以使用流api来转换它。在最后一行,将数据替换为你们项目的真实类型

data = Arrays.stream(data).       // Create stream from your array
    collect(Collectors.toMap(     // Transform it to Map
        d -> d.rawMaterialId,     // It is keys of creating map
        d -> d,                   // This is values of creating map 
        (d1, d2) -> {             // Function for merging values with same keys. 
                                  // There happens all neded concatenations.
            d1.location = d1.location + "," + d2.location;
            d1.batchNo = d1.batchNo + "," + d2.batchNo;
            d1.quantity = d1.quantity + "," + d2.quantity;
            return d1;
        }).
    values().toArray(new Data[0]); // Transform map to new array. 
                                   // Replace Data to real type of your items.

int j=i+1;不要从头开始。但是在这之后也无法获得预期的输出。谢谢,man@yudongshenI我是流API新手,我会试试。谢谢。
       for(int i=0; i<data.length;i++)
        {
         for(int j=0; i<data.length;i++)
          {
            if(data[i].rawMaterialId == data[j].rawMaterialId )
            {
                data[i].location=data[i].location +","+ data[j].location;
                data[i].batchNo=data[i].batchNo +"," + data[j].batchNo;
                data[i].quantity=data[i].quantity +"," + data[j].quantity;
            }
        } 
}
Map<Integer,List<Data>> map= new HashMap<Integer,List<Data>>();
for(int i=0; i<data.length;i++)
{
if(map.contains(data[i].rawMaterialId))    {
   List<Data> list=map.get(data[i].rawMaterialId);
list.add(data[i]);
}
else    {
 List<Data> list=new ArrayList<Data>();
list.add(data[i]);
map.put(data[i]);
}
}
    for (int i = 0; i < data.length ; i++) {
        for (int j = i + 1; i < data.length; i++) {
            if (data[i].rawMaterialId == data[j].rawMaterialId ) {
                    // copy to new Data Array
            }
        }
    } 
data = Arrays.stream(data).       // Create stream from your array
    collect(Collectors.toMap(     // Transform it to Map
        d -> d.rawMaterialId,     // It is keys of creating map
        d -> d,                   // This is values of creating map 
        (d1, d2) -> {             // Function for merging values with same keys. 
                                  // There happens all neded concatenations.
            d1.location = d1.location + "," + d2.location;
            d1.batchNo = d1.batchNo + "," + d2.batchNo;
            d1.quantity = d1.quantity + "," + d2.quantity;
            return d1;
        }).
    values().toArray(new Data[0]); // Transform map to new array. 
                                   // Replace Data to real type of your items.