要映射的Java流 [英] Java streams to map

查看:134
本文介绍了要映射的Java流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 Map< Integer,List< String>>的问题personenPerLeeftijd ,编译器说方法Persoon :: getLeeftijd无法解析。我真的不知道我还能做什么,抱歉荷兰语!

I'm having a problem with the Map<Integer, List<String>> personenPerLeeftijd, the compiler says the method, Persoon::getLeeftijd, cannot be resolved. I don't really know what else I can do, sorry for the Dutch words!

我还需要更多信息请问所以

I you need any more info pls ask so

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

    final List<Persoon> personen = Personen.getPersonen();


    Map<String, Persoon> map =
            personen.stream().collect(Collectors.toMap(p -> p.getNaam() + "-" + p.getLeeftijd(), p -> p));
    for (String s : map.keySet()) {
        System.out.printf("%-15s -> %s\n", s, map.get(s));
    }
    Map<Integer, List<String>> personenPerLeeftijd =
            personen.stream().collect(Collectors.groupingBy(Persoon::getLeeftijd)); //THIS METHOD


    personenPerLeeftijd.forEach((k, v) -> System.out.printf("%2d: %s\n", k,
            v.stream()


            System.out.println();

    TreeMap<Integer, Long> perLeeftijd =
            personen.stream().collect(Collectors.groupingBy(Persoon::getLeeftijd, Collectors.counting()));


    perLeeftijd.forEach((k, v) -> System.out.printf("%2d -> %d\n", k, v));
}

Persoon class

Persoon class

public class Persoon {
private final String naam;
private final Geslacht geslacht;
private final int leeftijd;

public Persoon(String naam, Geslacht geslacht, int leeftijd) {
    this.naam = naam;
    this.geslacht = geslacht;
    this.leeftijd = leeftijd;
}

public String getNaam() {
    return naam;
}

public Geslacht getGeslacht() {
    return geslacht;
}

public int getLeeftijd() {
    return leeftijd;
}

public int leeftijdsverschil(final Persoon andere) {
    return leeftijd - andere.leeftijd;
}

@Override
public String toString() {
    return String.format("%-9s - %s - %2d", naam, geslacht, leeftijd);
}


推荐答案

正如@ luk2302所述,最简单的解决方案是使用 Map< Integer,List< Person>>

As mentioned by @luk2302, the easiest solution is to use Map<Integer, List<Person>>.

    Map<Integer, List<Person>> peopleByAge = personen
            .stream()
            .collect(Collectors.groupingBy(Person::getAge)); //THIS METHOD

如果你真的需要 Map< Integer,List< String> ;> 然后还有一些事情要做。

If you really need Map<Integer, List<String>> then there's a little more to do.

    Map<Integer, List<String>> peoplesNamesByAge = personen
            .stream()
            .collect(
                    Collectors.groupingBy(Person::getAge,
                            Collectors.mapping(Person::getName,
                                    Collectors.toList())));
    peoplesNamesByAge.forEach((k, v) -> System.out.printf("%2d: %s\n", k, v));

这就是整个事情 - 我为了自己的理智而对它进行了一些改进。 :)

Here's the whole thing - I've anglicised it a little for my own sanity. :)

enum Gender {

    Male, Female;
}

public class Person {

    private final String name;
    private final Gender gender;
    private final int age;

    public Person(String naam, Gender gender, int age) {
        this.name = naam;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Gender getGender() {
        return gender;
    }

    public int getAge() {
        return age;
    }

    public int getAgeDifference(final Person other) {
        return age - other.age;
    }

    @Override
    public String toString() {
        return String.format("%-9s - %s - %2d", name, gender, age);
    }
}

public void test() {
    final List<Person> personen = Personen.getPersonen();

    Map<String, Person> map
            = personen.stream().collect(Collectors.toMap(p -> p.getName() + "-" + p.getAge(), p -> p));
    for (String s : map.keySet()) {
        System.out.printf("%-15s -> %s\n", s, map.get(s));
    }
    Map<Integer, List<Person>> peopleByAge = personen
            .stream()
            .collect(Collectors.groupingBy(Person::getAge)); //THIS METHOD

    peopleByAge.forEach((k, v) -> System.out.printf("%2d: %s\n", k, v));

    Map<Integer, List<String>> peoplesNamesByAge = personen
            .stream()
            .collect(
                    Collectors.groupingBy(Person::getAge,
                            Collectors.mapping(Person::getName,
                                    Collectors.toList())));
    peoplesNamesByAge.forEach((k, v) -> System.out.printf("%2d: %s\n", k, v));

    System.out.println();

    // TreeMap<Integer, Long> perLeeftijd
    Map<Integer, Long> perLeeftijd = personen
            .stream()
            .collect(Collectors.groupingBy(Person::getAge, Collectors.counting()));

    perLeeftijd.forEach((k, v) -> System.out.printf("%2d -> %d\n", k, v));
}

这篇关于要映射的Java流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆