英文:
Put all hashmap together based on same name condition in java 8
问题
public class PersonAgg {
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
Map<String, Integer> age = new HashMap<>();
Map<String, Integer> age2 = new HashMap<>();
Map<String, Integer> age3 = new HashMap<>();
Map<String, Integer> age4 = new HashMap<>();
age.put("One", 1);
age2.put("Two", 2);
age3.put("Three", 3);
age4.put("Four", 4);
persons.add(new Person("Ajay", age));
persons.add(new Person("Vijay", age2));
persons.add(new Person("Ajay", age3));
persons.add(new Person("Vijay", age4));
List<Map<String, Object>> outputList = new ArrayList<>();
Map<String, List<Map<String, Object>>> nameMap = new HashMap<>();
for (Person person : persons) {
Map<String, Object> personMap = new HashMap<>();
personMap.put("name", person.getName());
Map<String, Object> hashMapValue = new HashMap<>();
hashMapValue.put("age", person.getAge());
personMap.put("HashMapValue", hashMapValue);
List<Map<String, Object>> ageList = nameMap.getOrDefault(person.getName(), new ArrayList<>());
ageList.add(personMap);
nameMap.put(person.getName(), ageList);
}
for (String name : nameMap.keySet()) {
List<Map<String, Object>> ageList = nameMap.get(name);
Map<String, Object> aggregatedMap = new HashMap<>();
for (Map<String, Object> ageMap : ageList) {
aggregatedMap.putAll(ageMap);
}
outputList.add(aggregatedMap);
}
System.out.println(outputList);
}
}
class Person {
public Person(String name, Map<String, Integer> age) {
super();
this.name = name;
this.age = age;
}
private String name;
private Map<String, Integer> age;
public String getName() {
return name;
}
public Map<String, Integer> getAge() {
return age;
}
}
英文:
public class PersonAgg {
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
Map<String, Integer> age = new HashMap<>();
Map<String, Integer> age2 = new HashMap<>();
Map<String, Integer> age3 = new HashMap<>();
Map<String, Integer> age4 = new HashMap<>();
age.put("One", 1);
age2.put("Two", 2);
age3.put("Three", 3);
age4.put("Four", 4);
persons.add(new Person("Ajay", age));
persons.add(new Person("Vijay", age2));
persons.add(new Person("Ajay", age3));
persons.add(new Person("Vijay", age4));
}
}
class Person {
public Person(String name, Map<String, Integer> age) {
super();
this.name = name;
this.age = age;
}
private String name;
private Map<String, Integer> age;
Now I want output like : {name : Ajay, HashMapValue : {age :["One",1],} age3 :["Three",3]}, {name : Vijay , HashMapValue : {age2 :["Two",2],} age4 :["Four",4]}
答案1
得分: 0
可以尝试类似这样的代码:
Map<String, Map<String, Integer>> grouped = persons.stream()
.collect(toMap(Person::getName, Person::getAge, (p1, p2) -> {
p1.putAll(p2);
return p1;
}));
英文:
You can try something like this:
Map<String, Map<String, Integer>> grouped = persons.stream()
.collect(toMap(Person::getName, Person::getAge, (p1, p2) -> {
p1.putAll(p2);
return p1;
}));
答案2
得分: 0
不是完美的,所以请随意改进这个答案
public class Test
{
public static void main(String args[])
{
List<Person> persons = List.of(new Person("Ajay", Map.of("One", 1)),
new Person("Vijay", Map.of("Two", 2)),
new Person("Ajay", Map.of("Three", 3)),
new Person("Vijay", Map.of("Four", 4)));
Map<String, List<String>> values =
persons.stream()
.collect(Collectors.groupingBy(p -> "name : " + p.name + ", HashMapValue: ",
Collectors.mapping(p2 -> p2.age.entrySet().stream(),
Collectors.flatMapping(ages -> ages.map(entry -> get(entry)),
Collectors.toList())))
);
System.out.println("{");
values.entrySet().forEach((value) ->
{
System.out.print(value.getKey());
value.getValue().forEach((age) -> System.out.print(age + ","));
System.out.println();
});
System.out.println("}");
}
private static String get(Entry<String, Integer> entry)
{
String map;
switch(entry.getKey())
{
case "One": map = "age";
break;
case "Two": map = "age2";
break;
case "Three": map = "age3";
break;
default: map = "age4";
}
return map + " :[" + entry.getKey() + "," + entry.getValue() + "]";
}
private static class Person
{
private final String name;
private final Map<String, Integer> age;
private Person(String name, Map<String, Integer> age)
{
this.name = name;
this.age = age;
}
}
}
Output:
{
name : Ajay, HashMapValue: age :[One,1],age3 :[Three,3],
name : Vijay, HashMapValue: age2 :[Two,2],age4 :[Four,4],
}
英文:
Not perfect So please feel free to improve upon this answer
public class Test
{
public static void main(String args[])
{
List<Person> persons=List.of(new Person("Ajay",Map.of("One",1))
,new Person("Vijay",Map.of("Two",2))
,new Person("Ajay",Map.of("Three",3))
,new Person("Vijay",Map.of("Four",4)));
Map<String,List<String>> values=
persons.stream()
.collect(Collectors.groupingBy(p->" name : "+p.name+", HashMapValue: ",
Collectors.mapping(p2->p2.age.entrySet().stream(),
Collectors.flatMapping(ages->ages.map(entry->get(entry))
,Collectors.toList())))
);
System.out.println("{");
values.entrySet().forEach((value)->
{
System.out.print(value.getKey());
value.getValue().forEach((age)->System.out.print(age+","));
System.out.println();
});
System.out.println("}");
}
private static String get(Entry<String,Integer> entry)
{
String map;
switch(entry.getKey())
{
case "One":map="age";
break;
case "Two":map="age2";
break;
case "Three":map="age3";
break;
default:map="age4";
}
return map+" :["+entry.getKey()+","+entry.getValue()+"]";
}
private static class Person
{
private final String name;
private final Map<String,Integer> age;
private Person(String name,Map<String,Integer> age)
{
this.name=name;
this.age=age;
}
}
Output:
{
name : Ajay, HashMapValue: age :[One,1],age3 :[Three,3],
name : Vijay, HashMapValue: age2 :[Two,2],age4 :[Four,4],
}
专注分享java语言的经验与见解,让所有开发者获益!
评论