Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有3个类和枚举的Java GroupBy_Java_Group By_Stream - Fatal编程技术网

具有3个类和枚举的Java GroupBy

具有3个类和枚举的Java GroupBy,java,group-by,stream,Java,Group By,Stream,我是Java的初学者。我有一个我似乎无法解决的问题 我有三节课 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;} 但我想要一个不同的输出,如下所示: Owne

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

我有三节课

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) owns the animals: [Cats: Sunny, Milk], [Rat: Bean], [Dog: Boomer]
我怎样才能做到这一点?我可以使用Stream和groupBY吗

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

我有一个生成随机数据的程序类。我想用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));

}
公共静态列表getAllAnimal(){
return petstore=IntStream.range(0,20)
.mapToObj(i->newpetstore(newperson(getOwnerName(),getowneerage()),newarraylist(getList()))
.collect(toList());
}
公共静态ArrayList getList(){
int randomSize=1+rand.nextInt(5);
返回动物=IntStream.range(0,随机大小)
.限制(随机大小)
.mapToObj(i->新宠物(PetNameGenerator.getName(),rand())
.collect(toCollection(ArrayList::new));
}

感谢您的帮助。

按宠物的类型分组,并使用
收集器。mapping
将宠物实例提取/映射到其名称,并将其收集为列表

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

按宠物的类型分组,并使用
收集器.mapping
将宠物实例提取/映射到其名称,并将其收集为列表

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

你实际上在编码什么?什么是类
类型
类型是一个枚举:枚举类型{Cat,Dog,Rat,仓鼠,Fish;}在我的程序类中,我生成了一个列表:list petstore,我有两个方法spublic static list getAllAnimal(){return petstore=IntStream.range(0,20)。mapToObj(I->new petstore(新所有者(getOwnerName(),getOwnerAge()),新ArrayList(getList())).collect(toList());}公共静态ArrayList getList(){int randomSize=1+rand.nextInt(5);return anies=IntStream.range(0,randomSize).limit(randomSize).mapToObj(i->new Pet)(PetNameGenerator.getName(),rand()).collect(toCollection(ArrayList::new));}所有内容都已创建randonly@AlexRudenko对不起,我这样做了,你实际上在编码什么?什么是类
Type
Type是枚举:枚举类型{Cat,Dog,Rat,仓鼠,Fish;}在我的程序类中,我生成了一个list:list-petstore,我有两个方法,一个是静态list-getAllAnimal(){return-petstore=IntStream.range(0,20).mapToObj(I->new-petstore(new-Owner-name(),getowneraylist()),另一个是getList())。collect(toList();}公共静态ArrayList getList(){int randomSize=1+rand.nextInt(5);return animals=IntStream.range(0,randomSize)。limit(randomSize)。mapToObj(i->new Pet(PetNameGenerator.getName(),rand())。collection(toCollection(ArrayList::new));}一切都是创造出来的randonly@AlexRudenko对不起,是我干的