如何使用 Java 8 根据某些条件检查集合中的值是否唯一
How to check values in collection for uniques by some criteria using Java 8
我有以下 class:
public final class InfoWrapper {
private int count;
private int itemCode;
private String name;
public InfoWrapper() {
}
public InfoWrapper(final int count, final int itemCode, final String name) {
super();
this.count = count;
this.itemCode = itemCode;
this.name = name;
}
public int getCount() {
return count;
}
public int getItemCode() {
return itemCode;
}
public String getName() {
return name;
}
}
这是我的测试 class:
public class TestClass {
public static void main(String [] args) {
Deque<InfoWrapper> list = new ArrayDeque<InfoWrapper>();
list.add(new InfoWrapper(3, 111, "aaa"));
list.add(new InfoWrapper(7, 112, "bbb"));
list.add(new InfoWrapper(12, 113, "ccc"));
list.add(new InfoWrapper(6, 113, "ccc"));
list.add(new InfoWrapper(8, 113, "ccc"));
list.add(new InfoWrapper(3, 113, "ccc"));
System.out.println("Is good enought : " + isGoodEnought(list, 3));
}
private static boolean isGoodEnought(final Deque<InfoWrapper> list, final int maxFailsCount) {
return !list.stream().limit(maxFailsCount).skip(1).anyMatch((res) -> res.getName().equals("ccc"));
}
}
我的方法 isGoodEnough() 检查前 3 个元素的名称是否不等于 "ccc",但实际上我需要检查名称是否唯一。
private static boolean isGoodEnought(Deque<InfoWrapper> list,
int maxFailsCount) {
return list.stream()
.limit(maxFailsCount)
.map(InfoWrapper::getName)
.distinct()
.count() == maxFailsCount;
}
请注意,即使前两个名称相同,这也会遍历 maxFailsCount
个元素。如果 maxFailsCount
可能是一个很大的值,您可能想要使用
之类的东西
private static boolean isGoodEnought(Deque<InfoWrapper> list, int max) {
Set<String> seenNames = new HashSet<>();
for (Iterator<InfoWrapper> i = list.iterator(); i.hasNext() && max-- > 0;)
if (!seenNames.add(i.next().getName()))
return false;
return true;
}
我有以下 class:
public final class InfoWrapper {
private int count;
private int itemCode;
private String name;
public InfoWrapper() {
}
public InfoWrapper(final int count, final int itemCode, final String name) {
super();
this.count = count;
this.itemCode = itemCode;
this.name = name;
}
public int getCount() {
return count;
}
public int getItemCode() {
return itemCode;
}
public String getName() {
return name;
}
}
这是我的测试 class:
public class TestClass {
public static void main(String [] args) {
Deque<InfoWrapper> list = new ArrayDeque<InfoWrapper>();
list.add(new InfoWrapper(3, 111, "aaa"));
list.add(new InfoWrapper(7, 112, "bbb"));
list.add(new InfoWrapper(12, 113, "ccc"));
list.add(new InfoWrapper(6, 113, "ccc"));
list.add(new InfoWrapper(8, 113, "ccc"));
list.add(new InfoWrapper(3, 113, "ccc"));
System.out.println("Is good enought : " + isGoodEnought(list, 3));
}
private static boolean isGoodEnought(final Deque<InfoWrapper> list, final int maxFailsCount) {
return !list.stream().limit(maxFailsCount).skip(1).anyMatch((res) -> res.getName().equals("ccc"));
}
}
我的方法 isGoodEnough() 检查前 3 个元素的名称是否不等于 "ccc",但实际上我需要检查名称是否唯一。
private static boolean isGoodEnought(Deque<InfoWrapper> list,
int maxFailsCount) {
return list.stream()
.limit(maxFailsCount)
.map(InfoWrapper::getName)
.distinct()
.count() == maxFailsCount;
}
请注意,即使前两个名称相同,这也会遍历 maxFailsCount
个元素。如果 maxFailsCount
可能是一个很大的值,您可能想要使用
private static boolean isGoodEnought(Deque<InfoWrapper> list, int max) {
Set<String> seenNames = new HashSet<>();
for (Iterator<InfoWrapper> i = list.iterator(); i.hasNext() && max-- > 0;)
if (!seenNames.add(i.next().getName()))
return false;
return true;
}