如何检查 ArrayList 的特定元素的唯一性
How to check uniqueness of particular elements of an ArrayList
我有一个名为 LineUp 的 class,它是一个名为 Event 的 class 的 ArrayList。一个 Event 具有三个值,一个 String Act,一个 Venue(它自己的 class)和一个 int Session.
事件可以这样声明。
事件 e1 = 新事件 ("Foo Fighters", northstage, "1")
LineUp 是一个 ArrayList,Event 是像 e1 这样的元素。
在我的 LineUp class 中,我必须创建一个不变量来检查 ArrayList 阵容中包含的每个 Event 是否具有唯一的 Venue 和 Session。因为这个任务要求我完全遵循规范,所以 Act、Venue 和 Session 的组合是否唯一是无关紧要的,为了遵循规范,我必须/只/确保 Venue 和 Session 是唯一的。
如何检查重复项,但仅检查 ArrayList 中的特定值?
谢谢。
如果我理解正确,你可以在方法中使用 Map 来存储值
Map<Map<Venue, Integer>, Act> lineup = new HashMap<>();
它结合了 Venue-Session 对的独特性。
但是,由于 Venue 是您自己的 class,您必须为 Venue 实施 equals() 和 hashCode() 方法才能使此解决方案起作用
编辑:
我的意思是这样的:
Map<Map<Integer, Venue>,String> uniqueMap = new HashMap<>();
for (Event event: events) { // assuming events is ArrayList
Map<Integer, Venue> sessionVenueMap = new HashMap<>();
sessionVenueMap.put(event.getSession(), event.getVenue());
//check if we stored this pair in our cool map
if (uniqueMap.get(sessionVenueMap) == null) {
//if not
//store this in our uniqieMap in our method
uniqueMap.put(sessionVenueMap, event.getAct);
sessionVenueMap.put(event.getSession(), event.getVenue);
} else {
// if map has this pair
// then it is not unique
return false;
}
venueSessionMap.put(.getVenue(); event.getSession();
}
return true;
虽然代码没有经过测试,但您已经了解了大致的思路,尽管它看起来很复杂。可能有更好的解决方案
如果您只需要检查是否存在重复项(考虑场地-会话对),您可以创建一个助手 Pair
class,其中仅包含在此特定情况下重要的属性。然后 map
事件到 Pair
对象,删除重复项并检查大小是否相同。
例如,您可以在 LineUp
:
中创建一个嵌套的 class
class LineUp {
private List<Event> events = new ArrayList<>();
private static final class Pair<U, V> {
final U first;
final V second;
Pair(U first, V second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Pair)) {
return false;
}
Pair<U, V> that = (Pair<U, V>) o;
return Objects.equals(this.first, that.first)
&& Objects.equals(this.second, that.second);
}
@Override
public int hashCode() {
return Objects.hash(this.first, this.second);
}
}
// rest of the LineUp class
}
然后创建一个 return false 如果有任何重复的方法:
public boolean duplicateVenueSessions() {
// Map each Event to a Pair<Venue, Integer> and remove the duplicates
long numDistinct = this.events.stream()
.map(e -> new Pair<>(e.venue, e.session))
.distinct()
.count();
// return false if the original number of events is different from the
// number of distinct events considering only venue and session values
return this.events.size() != numDistinct;
}
如果不能使用 Java 8,您可以使用 Set
代替:
public boolean duplicateVenueSessions() {
Set<Pair<String, Integer>> distinct = new HashSet<>();
for (Event e : this.events) {
Pair<String, Integer> venueSession = new Pair<>(e.venue, e.session);
if (distinct.contains(venueSession)) {
return true;
}
distinct.add(venueSession);
}
return false;
}
我有一个名为 LineUp 的 class,它是一个名为 Event 的 class 的 ArrayList。一个 Event 具有三个值,一个 String Act,一个 Venue(它自己的 class)和一个 int Session.
事件可以这样声明。 事件 e1 = 新事件 ("Foo Fighters", northstage, "1") LineUp 是一个 ArrayList,Event 是像 e1 这样的元素。
在我的 LineUp class 中,我必须创建一个不变量来检查 ArrayList 阵容中包含的每个 Event 是否具有唯一的 Venue 和 Session。因为这个任务要求我完全遵循规范,所以 Act、Venue 和 Session 的组合是否唯一是无关紧要的,为了遵循规范,我必须/只/确保 Venue 和 Session 是唯一的。
如何检查重复项,但仅检查 ArrayList 中的特定值?
谢谢。
如果我理解正确,你可以在方法中使用 Map 来存储值
Map<Map<Venue, Integer>, Act> lineup = new HashMap<>();
它结合了 Venue-Session 对的独特性。
但是,由于 Venue 是您自己的 class,您必须为 Venue 实施 equals() 和 hashCode() 方法才能使此解决方案起作用
编辑:
我的意思是这样的:
Map<Map<Integer, Venue>,String> uniqueMap = new HashMap<>();
for (Event event: events) { // assuming events is ArrayList
Map<Integer, Venue> sessionVenueMap = new HashMap<>();
sessionVenueMap.put(event.getSession(), event.getVenue());
//check if we stored this pair in our cool map
if (uniqueMap.get(sessionVenueMap) == null) {
//if not
//store this in our uniqieMap in our method
uniqueMap.put(sessionVenueMap, event.getAct);
sessionVenueMap.put(event.getSession(), event.getVenue);
} else {
// if map has this pair
// then it is not unique
return false;
}
venueSessionMap.put(.getVenue(); event.getSession();
}
return true;
虽然代码没有经过测试,但您已经了解了大致的思路,尽管它看起来很复杂。可能有更好的解决方案
如果您只需要检查是否存在重复项(考虑场地-会话对),您可以创建一个助手 Pair
class,其中仅包含在此特定情况下重要的属性。然后 map
事件到 Pair
对象,删除重复项并检查大小是否相同。
例如,您可以在 LineUp
:
class LineUp {
private List<Event> events = new ArrayList<>();
private static final class Pair<U, V> {
final U first;
final V second;
Pair(U first, V second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Pair)) {
return false;
}
Pair<U, V> that = (Pair<U, V>) o;
return Objects.equals(this.first, that.first)
&& Objects.equals(this.second, that.second);
}
@Override
public int hashCode() {
return Objects.hash(this.first, this.second);
}
}
// rest of the LineUp class
}
然后创建一个 return false 如果有任何重复的方法:
public boolean duplicateVenueSessions() {
// Map each Event to a Pair<Venue, Integer> and remove the duplicates
long numDistinct = this.events.stream()
.map(e -> new Pair<>(e.venue, e.session))
.distinct()
.count();
// return false if the original number of events is different from the
// number of distinct events considering only venue and session values
return this.events.size() != numDistinct;
}
如果不能使用 Java 8,您可以使用 Set
代替:
public boolean duplicateVenueSessions() {
Set<Pair<String, Integer>> distinct = new HashSet<>();
for (Event e : this.events) {
Pair<String, Integer> venueSession = new Pair<>(e.venue, e.session);
if (distinct.contains(venueSession)) {
return true;
}
distinct.add(venueSession);
}
return false;
}