我具有以下类结构(给出的例子是没有意义的,我只是出于提问的目的而做了):
public class ManufacturerDto {
private String name;
private Boolean withConfirmation;
//getters and setters omitted for brevity
}
public class CarDto {
private ManufacturerDto manufacturer;
//getters and setters omitted for brevity
}
public abstract class VehicleDto {
private list<CarDto> cars = new ArrayList<>();
//getters and setters omitted for brevity
}
public class EuropeanMarketCarDto extends VehicleDto {
public Map<String, List<CarDto>> getCarManufacturerToCarsMap() {
return this.getCarDtos()
.stream()
.collect(Collectors.groupingBy(e -> e.manufacturer().getName()));
//getters and setters omitted for brevity
}
}
在给定的情况下,groupingBy
返回了我想要的几乎所有内容,但是密钥有问题。在getCarManufacturerToCarsMap()
方法中,我想将manufacturer
对象作为键,但是我什至不确定使用groupingBy
是否可以实现。我该怎么做才能返回Map<Manufacturer, List<CarDto>>
的地图?
答案 0 :(得分:3)
如果您实际上想避免将地图密钥设为String
即Map<String, List<CarDto>>
,而改为拥有Map<Manufacturer, List<CarDto>>
,则可以按以下方式进行分组:
this.getCarDtos()
.stream()
.collect(Collectors.groupingBy(e -> e.getManufacturer()));
请注意,这将要求您重写equals
类中的hashcode
和Manufacturer
方法,以定义两个给定对象具有相同名称时是相等的。
答案 1 :(得分:2)
您可以尝试这样的事情:
Map<Manufacturer, List<Car>> listing = cars.stream().collect(Collectors.groupingBy(Car::getManufacturer));
因此,这将按制造商对所有汽车进行分组,输出将类似于:
TOYOTA = [AURIS的AURIS参数,AVIA的AVIA参数],MERCEDES = [S500的S500参数 S500 ...]
编辑2
public class Main {
List<Car> listOfCars = new ArrayList<>();
public static void main(String[] args) {
List<Car> listOfCars = new ArrayList<>();
listOfCars.add(new Car(new Manufacturer("Toyota"), "Toyota A"));
listOfCars.add(new Car(new Manufacturer("Toyota"), "Toyota B"));
listOfCars.add(new Car(new Manufacturer("Jeep"), "Jeep A"));
listOfCars.add(new Car(new Manufacturer("Mercedes"), "Mercedes C"));
listOfCars.add(new Car(new Manufacturer("Toyota"), "Toyota C"));
listOfCars.add(new Car(new Manufacturer("Audi"), "Audi A"));
listOfCars.add(new Car(new Manufacturer("Toyota"), "Toyota C"));
Map<String,List<Car>> listOfCarsByManufacturers = listOfCars.stream().collect(Collectors.groupingBy(d->d.getManufacturer().getName()));
System.out.println(listOfCarsByManufacturers);
}
}
public class Car {
private Manufacturer manufacturer;
private String nameOfTheCar;
public Car() {
}
public Car(Manufacturer manufacturer, String nameOfTheCar) {
this.manufacturer = manufacturer;
this.nameOfTheCar = nameOfTheCar;
}
public Manufacturer getManufacturer() {
return manufacturer;
}
public String getNameOfTheCar() {
return nameOfTheCar;
}
@Override
public String toString() {
return "Manufacturer: " + getManufacturer() + ", name: " + getNameOfTheCar();
}
}
public class Manufacturer {
private String name;
public Manufacturer() {
}
public Manufacturer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return getName();
}
}
编辑3:
Map<String,List<Car>> listOfCarsByManufacturers = listOfCars.stream().collect(Collectors.groupingBy(d->d.getManufacturer().getName()));
System.out.println(listOfCarsByManufacturers);