简体   繁体   English

将 map 值转换为另一个 map object

[英]Convert map value to another map object

I have a map like this.我有一个像这样的 map。 Map<long,List<Student>> studentMap

Key is a number 1,2,3,4... Student object is:键是数字 1,2,3,4... 学生 object 是:

public class Student {
 private long addressNo;
 private String code;
 private BigDecimal tax;
 private String name;
 private String city;

 // getter and setters` 
}

What i want to do is to convert it Map<long,List<StudentInfo>> studentMap object and group id, addressNo and code fields.我想要做的是将它转换Map<long,List<StudentInfo>> studentMap object 和组 id、addressNo 和代码字段。

I can group the map by using these codes but summingDouble is not working for BigDecimal.Also I cannot convert my studentMap to studentInfoMap.:(我可以使用这些代码对 map 进行分组,但 summingDouble 不适用于 BigDecimal。我也无法将 studentMap 转换为 studentInfoMap。:(

 studentInfoMap.values().stream()
            .collect(
                     Collectors.groupingBy(StudentInfo::getCode, 
                     Collectors.groupingBy(StudentInfo::getAddressNo, 
                     Collectors.summingDouble(StudentInfo::getTax))));

My studentInfo object is:我的 studentInfo object 是:

public class StudentInfo {
  private long addressNo;
  private String code;
  private BigDecimal tax;

  // getter and setters` 
}

If you want to convert a Map<Long,List<Student>> to a Map<Long,List<StudentInfo>> with the keys being the same, try the following:如果要将Map<Long,List<Student>>转换为具有相同键的Map<Long,List<StudentInfo>> ,请尝试以下操作:

Map<Long,List<StudentInfo>> convertedMap = 
  studentInfoMap.entrySet() //get the entry set to access keys and values
    .stream() 
    .collect(Collectors.toMap( //collect the entries into another map
        entry -> entry.getKey(), //use the keys as they are
        entry -> entry.getValue() //get the value list
                      .stream() /
                      .map(student -> new StudentInfo(/*pass in the student parameters you need*/) //create a StudentInfo out of a Student - and entry.getKey() if needed
                      .toList() //collect the StudentInfo elements into a list
     ));

转换列表<object>映射<div id="text_translate"><p>我有一个包含四个字段的Employee类:id、name、department 和 email。 我创建了五个对象,设置了值并将它们全部添加到列表中。</p><p> 现在,我想将该列表转换为地图,但我做不到。 供您参考,这是我尝试过的代码</p><pre>Employee emp1 = new Employee("1", "Mayank", "HR", "mayank@gmail.com"); Employee emp2 = new Employee("2", "Mahesh", "Trainer", "Mahesh@gmail.com"); Employee emp3 = new Employee("3", "Vipul", "SEO", "Vipul@gmail.com"); Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "Ajay@gmail.com"); Employee emp5 = new Employee("5", "Rakesh", "Marketing", "Rakesh@gmail.com"); //add class object to list List &lt; Employee &gt; listWmp = new ArrayList &lt; &gt; (); listWmp.add(0, emp1); listWmp.add(1, emp2); listWmp.add(2, emp3); listWmp.add(3, emp4); listWmp.add(4, emp5); System.out.println("list elements are: " + listWmp); //convert the list into map Map &lt; String, Employee &gt; listMap = new HashMap &lt; &gt; (); for (Employee employee: listWmp) { listMap.put(employee.getEmpId(), employee.getEmpBame()); }</pre><p> 如何将列表转换为地图?</p></div></object> - convert List<object> to map

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于映射的“键”,将对象类型的“值”转换为另一个映射<String,String> - For a “key” of a map convert the “value” which is of type Object to another Map<String,String> Map&lt;&gt; 中的 Map&lt;&gt; 作为值 -&gt; 如何从 Map&lt;&gt; 创建 object,其中 value 是另一个 Map&lt;&gt;? - Map<> in Map<> as value -> How to create object from the Map<> where value is another Map<>? 地图中的值包含另一个地图 - Value in a map contains another map 将JsonNode对象转换为Map - Convert JsonNode object to Map 将 Class 的对象转换为 Map - Convert object of Class into Map 如何将地图转换为对象 - How to Convert Map to Object 在Kotlin中将地图转换为对象 - Convert a Map to an Object in Kotlin 在java中将对象转换为映射 - Convert object to map in java 转换列表<object>映射<div id="text_translate"><p>我有一个包含四个字段的Employee类:id、name、department 和 email。 我创建了五个对象,设置了值并将它们全部添加到列表中。</p><p> 现在,我想将该列表转换为地图,但我做不到。 供您参考,这是我尝试过的代码</p><pre>Employee emp1 = new Employee("1", "Mayank", "HR", "mayank@gmail.com"); Employee emp2 = new Employee("2", "Mahesh", "Trainer", "Mahesh@gmail.com"); Employee emp3 = new Employee("3", "Vipul", "SEO", "Vipul@gmail.com"); Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "Ajay@gmail.com"); Employee emp5 = new Employee("5", "Rakesh", "Marketing", "Rakesh@gmail.com"); //add class object to list List &lt; Employee &gt; listWmp = new ArrayList &lt; &gt; (); listWmp.add(0, emp1); listWmp.add(1, emp2); listWmp.add(2, emp3); listWmp.add(3, emp4); listWmp.add(4, emp5); System.out.println("list elements are: " + listWmp); //convert the list into map Map &lt; String, Employee &gt; listMap = new HashMap &lt; &gt; (); for (Employee employee: listWmp) { listMap.put(employee.getEmpId(), employee.getEmpBame()); }</pre><p> 如何将列表转换为地图?</p></div></object> - convert List<object> to map 将 Map 值转换为字符串 - Convert a Map value to String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM