[英]Collection to store objects with combination of ids as unique
我有一个带有属性的类测试
--> Integer id1
--> Integer id2
--> String stringValue.
因此,每个(id1和id2)唯一ID组合都有stringValues列表。 我希望能够将其存储在集合中。 例:
test1 : id1 = 1, id2 = 2 , stringValue = one
test11 : id1 = 1, id2 = 2 , stringValues =two
test111 : id1 = 1, id2 = 3 , stringValues =three
test2 : id1 = 5, id2 = 3, stringValues = four
test22: id1 = 5, id2 = 2, stringValues = five
test222: id = 5, id2 = 3, stringValues = six
所需的最终结果是将对象存储为
-> {id = 1, id = 2, String = {one, two} }
-> {id = 1 , id =3 , String = {three}}
-> {id = 5, id = 3 , String = {four,six}}
-> {id = 5, id = 2 , String = {five}}
我希望能够将“测试”对象的列表存储在集合中。 我尝试了Set <>,覆盖了equals和hashcode,但是没有按预期工作。 干得好
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id1 == null) ? 0 : id1.hashCode());
result = prime * result + ((id2 == null) ? 0 : id2.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if( obj instanceof Test){
Test test = (Test) obj;
return (test.id1 == this.id1 && test.id2 == this.id2);
} else {
return false;
}
}
我遍历每个对象并将其存储在SET中。 它确实消除了([id1和id2]的冗余组合,但也消除了各自的字符串值。)糟糕,我忘了提一下我从数据库检索的结果集中获取数据。 因此,每一行都包含id1,id2和一个字符串值(所有行都不同)。 一些行将具有共同的id1和id2值
有人可以建议我什么建议吗?
先感谢您
这样可以解决您的问题吗?
import java.util.*;
public class Playground1 {
public static void main(String[] args) {
// fake resultset
List<DBResultSetRowEmulation> resultSetEmulation = new ArrayList<>();
resultSetEmulation.add(new DBResultSetRowEmulation(1, 2, "one"));
resultSetEmulation.add(new DBResultSetRowEmulation(1, 2, "two"));
resultSetEmulation.add(new DBResultSetRowEmulation(1, 3, "three"));
resultSetEmulation.add(new DBResultSetRowEmulation(5, 3, "four"));
resultSetEmulation.add(new DBResultSetRowEmulation(5, 2, "five"));
resultSetEmulation.add(new DBResultSetRowEmulation(5, 3, "six"));
Map<DoubleIndex, List<String>> resultData = new HashMap<>();
// iterate through resultset
for(DBResultSetRowEmulation row: resultSetEmulation) {
DoubleIndex curRecordIdx = new DoubleIndex(row.a, row.b);
if (resultData.containsKey(curRecordIdx)) {
// append current string to some existing id1+id2 combination
resultData.get(curRecordIdx).add(row.c);
} else {
// create a new list for new id1+id2 combination
resultData.put(curRecordIdx, new ArrayList<>(Collections.singletonList(row.c)));
}
}
System.out.println(resultData);
}
private static class DBResultSetRowEmulation {
Integer a, b;
String c;
DBResultSetRowEmulation(Integer a, Integer b, String c) {
this.a = a;
this.b = b;
this.c = c;
}
}
private static class DoubleIndex {
private Integer a,b;
public DoubleIndex(Integer a, Integer b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DoubleIndex that = (DoubleIndex) o;
return a.equals(that.a) && b.equals(that.b);
}
@Override
public int hashCode() {
int result = a.hashCode();
result = 31 * result + b.hashCode();
return result;
}
@Override
public String toString() {
return "id{" +
"a=" + a +
", b=" + b +
'}';
}
}
}
输出:
{id {a = 1,b = 2} = [一个,两个],
id {a = 1,b = 3} = [三],
id {a = 5,b = 2} = [5],
id {a = 5,b = 3} = [四个,六个]}
您将必须弄清楚如何自己对结果进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.