我正在编写一段代码,其中包含具有重复项的元组列表,我希望从该列表中删除重复项,而不使用HASHSET或SET。我只被允许使用数组、ArrayList和迭代器。
下面的代码将溢出/无限循环。我理解为什么它会进入无限循环周期,因为我添加了元素,大小也增加了。
元组类:
public class Tuple {
private int key;
private String value;
public Tuple(int keyP, String valueP) {
key = keyP;
value = valueP;
}
public int getKey() {
return key;
}
public String getValue() {
return value;
}
public boolean equals(Tuple t) {
if (this.key == t.getKey() && this.value.equals(t.getValue())) {
return true;
}
else return false;
}
}
ArrayList < Tuple > union= new ArrayList<Tuple>();
ArrayList < Tuple > unique= new ArrayList<Tuple>();
Union: [Tuple < 15, a >, Tuple < 17, a >, Tuple < 15, a >, Tuple<79, b>]
unique.add(union.get(0));
for (int k = 1; k < union.size(); k++) {
Tuple test= union.get(k);
for (int j= 0; j < unique.size();j++) {
if (!test.equals(unique.get(j))) {
System.out.println(union.get(k));
unique.add(union.get(k));
}
}
}
唯一的应该是
[Tuple< 15, a >, Tuple< 17, a >, Tuple< 79, b >]
我完全被困了一天多。请帮帮我。
发布于 2018-03-08 04:43:09
在元组的键、值变量上重写equals()
和hashCode()
方法。这样就可以直接使用List的contains()
方法,而不是每次都循环遍历unique
List。
文件: Tuple.java
public class Tuple {
private int key;
private String value;
public Tuple(int keyP, String valueP) {
key = keyP;
value = valueP;
}
public int getKey() {
return key;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "Tuple [key=" + key + ", value=" + value + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + key;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Tuple other = (Tuple) obj;
if (key != other.key)
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}
文件: TupleMain.java
public class TupleMain {
public static void main(String[] args) {
List<Tuple> union = Arrays.asList(new Tuple(15, "a"), new Tuple(17, "a"), new Tuple(15, "a"),
new Tuple(79, "b"));
List<Tuple> unique = new ArrayList<Tuple>();
unique.add(union.get(0));
for (int k = 1; k < union.size(); k++) {
Tuple test = union.get(k);
if (!unique.contains(test)) {
unique.add(test);
}
}
System.out.println(unique);
// The Java 8 way
union.stream().distinct().forEach(System.out::println);
}
}
发布于 2018-03-08 04:46:47
可能还有更好的选择,但根据您的需求,您可以在循环中添加一个标志……
ArrayList<Tuple> union= new ArrayList<Tuple>();
ArrayList<Tuple> unique= new ArrayList<Tuple>();
union.add(new Tuple(15, "a"));
union.add(new Tuple(17, "a"));
union.add(new Tuple(15, "a"));
union.add(new Tuple(79, "b"));
unique.add(union.get(0));
for(int k=1;k<union.size();k++) {
Tuple test=union.get(k);
Boolean exists = false;
for(int j=0;j<unique.size();j++) {
if(test.equals(unique.get(j))) {
exists = true;
break;
}
}
if(!exists) {
System.out.println(union.get(k));
unique.add(union.get(k));
}
}
发布于 2018-03-08 04:29:46
如果没有数据,我们可以承认编译器对一个空的元组列表执行操作:
-> union.stream().distinct()
| Expression value is: java.util.stream.DistinctOps$1@4493d195
| assigned to temporary variable $58 of type Stream<Tuple>
https://stackoverflow.com/questions/49165234
复制