在对象列表列表中查找重复字段
Find duplicate fields in a list of list of objects
我有一个这样的对象列表:
[{
supplier: ""
rating: [{
sid: "1"
},
{
sid:"1"
}]
}]
我想检查每个评分的 sid 是否在 Java 中没有重复项
class 看起来像:
class Entity {
private String supplier;
private List<Rating> ratings;
}
class Rating {
String sid;
String bar;
}
在每个 ratings
中,不会有重复的 sid
这是通过 PUT 调用保存到 mongo 的,所以我不确定通过添加索引来做类似 Mongo 的事情是否合理。
编辑:结构是这样
List<List<Rating>> dupCheck = ratings.stream().map(p -> p.getRating()).collect(Collectors.toList());
或
List<Entity>
所以当我调用它时它要求索引,我试图通过 IntStream 解决但不能。
更新:抱歉我没说清楚。我想检测一个实体内的重复项(在 sid 字段上)。
new Entity("A",
Arrays.asList(
new Rating("1", "a"),
new Rating("2", "b"),
new Rating("3", "c"),
new Rating("1", "d")
)),
所以在上面我想检测是否有重复的sids
您可以使用散列集来查明您的列表是否有重复项:
Set<String> sids = new HashSet<>();
// `numDuplicates` returns the number of duplicate ratings
long numDuplicates = ratings.stream()
.map(r -> r.sid)
// HashSet#add returns `true` if the element was not yet in the HashSet, and `false` if the HashSet already contained the element.
.filter(s -> !sids.add(s))
// Count the number of elements already contained.
// here you could also use `collect` if you want to find out which ratings are duplicated.
.count();
if (numDuplicates > 0) {
// there are duplicates
}
要检查您是否有重复的 sid
s,您可以这样做:
List<Entity> entities = Arrays.asList(
new Entity("A",
Arrays.asList(
new Rating("1", "a"),
new Rating("2", "b"),
new Rating("3", "c"),
new Rating("4", "d")
)),
new Entity("B",
Arrays.asList(
new Rating("1", "a"),
new Rating("5", "e")
))
);
Map<String, Long> sidToCount = entities.stream()
.map(Entity::getRatings)
.flatMap(ratings -> ratings.stream().map(Rating::getSid)) // stream of sids
.collect(Collectors.groupingBy(s -> s, Collectors.counting()));
boolean hasDuplicates = sidToCount.values()
.stream()
.anyMatch(count -> count > 1);
System.out.println(hasDuplicates); // true
编辑:
要查找某个实体是否具有重复评级,您可以执行以下操作:
boolean hasEntityWithDuplicates = entities.stream()
.map(Entity::getRatings)
.anyMatch(ratings -> ratings.size() > new HashSet<>(ratings).size());
通过创建 Set
我们可以计算唯一元素的数量。如果集合的大小小于列表的大小,则表示列表包含重复项。
我有一个这样的对象列表:
[{
supplier: ""
rating: [{
sid: "1"
},
{
sid:"1"
}]
}]
我想检查每个评分的 sid 是否在 Java 中没有重复项 class 看起来像:
class Entity {
private String supplier;
private List<Rating> ratings;
}
class Rating {
String sid;
String bar;
}
在每个 ratings
中,不会有重复的 sid
这是通过 PUT 调用保存到 mongo 的,所以我不确定通过添加索引来做类似 Mongo 的事情是否合理。
编辑:结构是这样
List<List<Rating>> dupCheck = ratings.stream().map(p -> p.getRating()).collect(Collectors.toList());
或
List<Entity>
所以当我调用它时它要求索引,我试图通过 IntStream 解决但不能。
更新:抱歉我没说清楚。我想检测一个实体内的重复项(在 sid 字段上)。
new Entity("A",
Arrays.asList(
new Rating("1", "a"),
new Rating("2", "b"),
new Rating("3", "c"),
new Rating("1", "d")
)),
所以在上面我想检测是否有重复的sids
您可以使用散列集来查明您的列表是否有重复项:
Set<String> sids = new HashSet<>();
// `numDuplicates` returns the number of duplicate ratings
long numDuplicates = ratings.stream()
.map(r -> r.sid)
// HashSet#add returns `true` if the element was not yet in the HashSet, and `false` if the HashSet already contained the element.
.filter(s -> !sids.add(s))
// Count the number of elements already contained.
// here you could also use `collect` if you want to find out which ratings are duplicated.
.count();
if (numDuplicates > 0) {
// there are duplicates
}
要检查您是否有重复的 sid
s,您可以这样做:
List<Entity> entities = Arrays.asList(
new Entity("A",
Arrays.asList(
new Rating("1", "a"),
new Rating("2", "b"),
new Rating("3", "c"),
new Rating("4", "d")
)),
new Entity("B",
Arrays.asList(
new Rating("1", "a"),
new Rating("5", "e")
))
);
Map<String, Long> sidToCount = entities.stream()
.map(Entity::getRatings)
.flatMap(ratings -> ratings.stream().map(Rating::getSid)) // stream of sids
.collect(Collectors.groupingBy(s -> s, Collectors.counting()));
boolean hasDuplicates = sidToCount.values()
.stream()
.anyMatch(count -> count > 1);
System.out.println(hasDuplicates); // true
编辑:
要查找某个实体是否具有重复评级,您可以执行以下操作:
boolean hasEntityWithDuplicates = entities.stream()
.map(Entity::getRatings)
.anyMatch(ratings -> ratings.size() > new HashSet<>(ratings).size());
通过创建 Set
我们可以计算唯一元素的数量。如果集合的大小小于列表的大小,则表示列表包含重复项。