Java 删除重复的对象属性
Java remove duplicate Object properties
我正在尝试 return 没有重复属性的 List<Object>
(使用 CSVRecord
)。
我收到了这样的 CSV:
brand;color
bmw;red
bmw;blue
jaguar;yellow
jaguar;red
mercedes;bleu
fiat;green
车子class长得像(举例):
@Builder
@Data
public class Car {
private String brand;
private List<String> color;
}
我有这样的方法(仅举个例子):
public List<Car> convert(List<CSVRecord> csvRecordList) {
Map<Car, List<CSVRecord>> csvRecordsByCar = csvRecordList.stream()
.collect(Collectors.groupingBy(csvRecord -> Car.builder()
.brand(csvRecord.get("brand"))
.color(Collections.singletonList(csvRecord.get("color")))
.build()
));
return csvRecordsByCar .entrySet().stream()
.map((Map.Entry<Car, List<CSVRecord>> csvRecordByCar) ->
Car.builder()
.brand(csvRecordByCar.getKey().getBrand())
.color(csvRecordByCar.getKey().getColor())
.build()
)
.collect(Collectors.toList());
}
现在 return 给我一个 Car
具有重复品牌的列表对象。我想要的是不要有重复的品牌并将品牌的颜色合并到列表中,例如:
Car{
brand: "bmw",
color: ["red", "blue"]
}
我是 java 的新手。
没有看到汽车 class,我从函数的第一部分假设你是 groupingBy
Car
,这取决于你实现 equals
和 hashcode
.
所以这可能就是为什么你会得到像
这样的重复项
Car{
brand: "bmw",
color: ["red"]
}
Car{
brand: "bmw",
color: ["blue"]
}
要考虑的另一个选择是 groupBy
brand
命名并收集 color
的列表,然后将该结果映射到您的 Car
.[=19 列表中=]
假设您的汽车 class 具有列表中的颜色,使 class 如下所示:
public class Car {
private String name;
private List<String> color;
}
您需要在您的汽车中添加以下方法class,
public void addColor(String color){
if(null==this.color){
this.color = new ArrayList<>();
}
this.color.add(color);
}
试试这个:
public List<Car> convert(List<CSVRecord> csvRecordList) {
Map<String, List<CSVRecord>> csvRecordsByCar = csvRecordList.stream()
.collect(Collectors.groupingBy(csvRecord -> csvRecord.get("brand")));
List<Car> result= new ArrayList<>();
for (Map.Entry mapElement : csvRecordsByCar.entrySet()) {
String key = (String)mapElement.getKey();
List<CSVRecord> value = (List<CSVRecord>) mapElement.getValue();
Car car=new Car();
car.setName(key);
for (CSVRecord csvRecord2 : value) {
car.addColor(csvRecord2.get("color"));
}
result.add(car);
}
return result;
}
您需要在分组后创建 Car
对象。目前,由于您是按 Car
对象本身进行分组,因此在结果列表中每个 brand-color
的组合都会有一个 Car
对象。首先你只需要按颜色分组,然后继续创建对象。
注意: 假设在下面的代码中静态导入 Collectors
方法 toList()
、mapping()
和 groupingBy()
。
Map<String, List<String>> map
= csvRecordList.stream()
.collect(groupingBy(record -> record.get("brand"),
mapping(record -> record.get("color"),
toList())));
// Now convert this map to list of Cars
return map.entrySet()
.stream()
.map(entry -> Car.builder()
.brand(entry.getKey())
.color(entry.getValue())
.build())
.collect(toList());
我正在尝试 return 没有重复属性的 List<Object>
(使用 CSVRecord
)。
我收到了这样的 CSV:
brand;color
bmw;red
bmw;blue
jaguar;yellow
jaguar;red
mercedes;bleu
fiat;green
车子class长得像(举例):
@Builder
@Data
public class Car {
private String brand;
private List<String> color;
}
我有这样的方法(仅举个例子):
public List<Car> convert(List<CSVRecord> csvRecordList) {
Map<Car, List<CSVRecord>> csvRecordsByCar = csvRecordList.stream()
.collect(Collectors.groupingBy(csvRecord -> Car.builder()
.brand(csvRecord.get("brand"))
.color(Collections.singletonList(csvRecord.get("color")))
.build()
));
return csvRecordsByCar .entrySet().stream()
.map((Map.Entry<Car, List<CSVRecord>> csvRecordByCar) ->
Car.builder()
.brand(csvRecordByCar.getKey().getBrand())
.color(csvRecordByCar.getKey().getColor())
.build()
)
.collect(Collectors.toList());
}
现在 return 给我一个 Car
具有重复品牌的列表对象。我想要的是不要有重复的品牌并将品牌的颜色合并到列表中,例如:
Car{
brand: "bmw",
color: ["red", "blue"]
}
我是 java 的新手。
没有看到汽车 class,我从函数的第一部分假设你是 groupingBy
Car
,这取决于你实现 equals
和 hashcode
.
所以这可能就是为什么你会得到像
这样的重复项Car{
brand: "bmw",
color: ["red"]
}
Car{
brand: "bmw",
color: ["blue"]
}
要考虑的另一个选择是 groupBy
brand
命名并收集 color
的列表,然后将该结果映射到您的 Car
.[=19 列表中=]
假设您的汽车 class 具有列表中的颜色,使 class 如下所示:
public class Car {
private String name;
private List<String> color;
}
您需要在您的汽车中添加以下方法class,
public void addColor(String color){
if(null==this.color){
this.color = new ArrayList<>();
}
this.color.add(color);
}
试试这个:
public List<Car> convert(List<CSVRecord> csvRecordList) {
Map<String, List<CSVRecord>> csvRecordsByCar = csvRecordList.stream()
.collect(Collectors.groupingBy(csvRecord -> csvRecord.get("brand")));
List<Car> result= new ArrayList<>();
for (Map.Entry mapElement : csvRecordsByCar.entrySet()) {
String key = (String)mapElement.getKey();
List<CSVRecord> value = (List<CSVRecord>) mapElement.getValue();
Car car=new Car();
car.setName(key);
for (CSVRecord csvRecord2 : value) {
car.addColor(csvRecord2.get("color"));
}
result.add(car);
}
return result;
}
您需要在分组后创建 Car
对象。目前,由于您是按 Car
对象本身进行分组,因此在结果列表中每个 brand-color
的组合都会有一个 Car
对象。首先你只需要按颜色分组,然后继续创建对象。
注意: 假设在下面的代码中静态导入 Collectors
方法 toList()
、mapping()
和 groupingBy()
。
Map<String, List<String>> map
= csvRecordList.stream()
.collect(groupingBy(record -> record.get("brand"),
mapping(record -> record.get("color"),
toList())));
// Now convert this map to list of Cars
return map.entrySet()
.stream()
.map(entry -> Car.builder()
.brand(entry.getKey())
.color(entry.getValue())
.build())
.collect(toList());