使用 Collectors.toMap in Java 与 LIst 映射 8

Map with LIst using Collectors.toMap in Java 8

我喜欢将下面的代码转换为 java 流,

HashMap<String, List<Data>> heMap = new HashMap<String, List<Data>>();
for (Data heData : obj) {
    String id = heData.getData().getId() + heData.getPlanData().getCode()
            + heData.getPlanData().getId();
    if (!heMap.containsKey(id)) {
        CitizenHElist = new ArrayList<Data>();

        CitizenHElist.add(heData);
        heMap.put(id, CitizenHElist);

    } else {
        heMap.get(id).add(heData);
    }
}

我使用流尝试了以下代码,但我没有成功。

heMap=obj.stream().collect(Collectors.toMap(t->getKey(t), obj.stream().collect(Collectors.toList())));

private String getKey(Data heData){
    String id = heData.getData().getId() + heData.getPlanData().getCode()
                    + heData.getPlanData().getId();
    return id;
}

这是 groupingBy 收集器的工作:

import static java.util.stream.Collectors.groupingBy;

Map<String, List<Data>> heMap = obj.stream().collect(groupingBy(d -> getKey(d)));

请注意,这将使用 一些 未指定的 MapList 实现。目前,它恰好是 HashMapArrayList,但将来可能会改变。

不确定您的数据结构,但您想执行如下所示的操作。

import java.util.*;
import java.util.stream.Collectors;
class Data {

    String stud_id;
    String stud_name;
    String stud_location;
    public Data(String string, String string2, String string3) {
    this.stud_id=string;
    this.stud_location=string2;
    this.stud_name=string3;
    }
    public Object getData() {
        return this.stud_id;
    }


}

class Temp3
{
    public static void main(String args[])
    {

        Map<String, List<Data>> heMap=new HashMap<String, List<Data>>();
        Data data1=new Data("1","11","111");
        Data data2=new Data("2","22","222");
        List<Data> obj=new ArrayList<Data>();
        obj.add(data1);
        obj.add(data2);
        for (Data heData : obj) 
        {
                    String id = "2";
                    if (!heMap.containsKey(id)) 
                    {
                        ArrayList<Data> CitizenHElist = new ArrayList<Data>();
                        CitizenHElist.add(heData);
                        heMap.put(id, CitizenHElist);
                    } 
                    else 
                    {
                        heMap.get(id).add(heData);

                    }

        }
        heMap=obj.stream().collect(Collectors.groupingBy(w -> w.stud_location));
        System.out.println(heMap);

    }
}

基于字段分组 -

import java.util.*;
import java.util.stream.*;
public class Main
{
  public static void main (String[]args)
  {
    System.out.println ("Hello World");

    List < Data > dataList = getDataList();

    System.out.println (dataList);

    Map < String, List < Data >> dataMap =
      dataList.stream ().collect (Collectors.groupingBy (d->d.code));

    System.out.println (dataMap);
  }

 static List < Data > getDataList(){
    List < Data > dataList = new ArrayList <> ();
    dataList.add (new Data (1, "Prince", "102"));
    dataList.add (new Data (2, "Rahul", "102"));
    dataList.add (new Data (3, "Sunny", "103"));
    dataList.add (new Data (4, "Mitul", "104"));
    dataList.add (new Data (5, "Amit", "105"));
    dataList.add (new Data (6, "Ashish", "105"));
    return dataList;
  }
}

class Data
{
   int id;
   String name;
   String code;
  public Data (int id, String name, String code)
  {
    this.id = id;
    this.name = name;
    this.code = code;
  }
  public String toString ()
  {
    return String.format ("id:%s,name:%s,code:%s", id, name, code);
  }
}