Java中的GroupBy,涉及3个类和枚举。

huangapple go评论71阅读模式
英文:

Java GroupBy with 3 classes and Enum

问题

以下是您提供的内容的翻译部分:

我是Java的初学者。我有一个问题,似乎无法解决。

我有3个类,

public class Pet {
    public String petname;
    Type type;
}

public class Owner {
    public String name;
    public int age;
}

public class PetStore {
    public Owner owner;
    public ArrayList<Pet> pets;
}

我的输出目前是:

Owner Mike (39) 拥有的动物:[Cat: Sunny, Rat: Bean, Cat: Milk, Dog: Boomer]

但我想要一个不同的输出,像这样:

Owner Mike (39) 拥有的动物:[Cats: Sunny, Milk], [Rat: Bean], [Dog: Boomer]

我如何实现这个?我能使用Stream和groupBy吗?

-------------- 编辑 ---------------------

我有一个Program类,在那里我生成随机数据。我想在Petstore列表中使用groupBy。

public static List<PetStore> getAllAnimal() {
    return petstore = IntStream.range(0, 20)
            .mapToObj(i -> new PetStore(new Person(getOwnerName(), getOwnerAge()), new ArrayList<Pet>(getList())))
            .collect(toList());
}

public static ArrayList<Pet> getList() {
    int randomSize = 1 + rand.nextInt(5);
    return animals = IntStream.range(0, randomSize)
            .limit(randomSize)
            .mapToObj(i -> new Pet(PetNameGenerator.getName(), rand()))
            .collect(toCollection(ArrayList::new));
}

感谢您的帮助。

英文:

I'm a beginner in Java.I have an issue that I can't seem to solve.

I have 3 classes,

public class Pet {
public String petname;
Type type; }

public class Owner {
public String name;
public int age; }

public class PetStore {
public Owner owner;
public ArrayList&lt;Pet&gt; pets;}

my output is right now:

Owner Mike (39) owns the animals: [Cat: Sunny, Rat: Bean, Cat: Milk, Dog: Boomer]

But I want a different output, like this:

Owner Mike (39) owns the animals: [Cats: Sunny, Milk], [Rat: Bean], [Dog: Boomer]

How can I achieve this? Can I use Stream and groupBY?

-------------- Edit ---------------------

I have a Program Class where I generate random data. I want to user the groupBy with the Petstore List.

public static List&lt;PetStore&gt; getAllAnimal(){

    return petstore = IntStream.range(0, 20)
            .mapToObj(i -&gt; new PetStore(new Person(getOwnerName(),getOwnerAge()), new ArrayList&lt;Pet&gt;(getList())))
            .collect(toList());
}

public static ArrayList&lt;Pet&gt; getList() {
    int randomSize = 1 + rand.nextInt(5);
    return animals = IntStream.range(0 , randomSize)
            .limit(randomSize)
            .mapToObj(i -&gt; new Pet(PetNameGenerator.getName(), rand()))
            .collect(toCollection(ArrayList::new));

}

Thanks for the help.

答案1

得分: 2

按宠物类型进行分组,并使用 Collectors.mapping 提取/映射宠物实例到其名称,并将它们收集为列表。

Map<Type, List<String>> petTypeToNames = petStore.getPets()
        .stream()
        .collect(Collectors.groupingBy(Pet::getType,
                Collectors.mapping(Pet::getPetname, Collectors.toList())));
英文:

Group by the pet's type and use Collectors.mapping to extract/map a Pet instance to its name and collect them as a list.

 Map&lt;Type, List&lt;String&gt;&gt; petTypeToNames = petStore.getPets()
        .stream()
        .collect(Collectors.groupingBy(Pet::getType,
                Collectors.mapping(Pet::getPetname, Collectors.toList())));

huangapple
  • 本文由 发表于 2020年9月26日 21:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64078452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定