我正在寻找一种从列表中删除重复项的好方法。
List<String[]> rowList = new ArrayList();
rowList.add(new String[]{"1","a", "abc"});
rowList.add(new String[]{"2","b", "def"});
rowList.add(new String[]{"3","c", "ghi"});
rowList.add(new String[]{"4","a", "jkl"});
rowList.add(new String[]{"5","d", "mno"});
rowList.add(new String[]{"6","e", "pqr"});
rowList.add(new String[]{"7","b", "stu"});
从这个rwoList,我只想要条目:1,2,3,5和6.这意味着我只有一列intrest,在这种情况下第2列(a,b,c,..) 这只是一个简单的例子,我必须处理有300列和最少300000行的数量表。另一个重点是,我不会放弃列表中的方向。
注意:我从csv文件接收数据。
答案 0 :(得分:2)
List<String[]> rowList = new ArrayList<String[]>();
rowList.add(new String[]{"1","a", "abc"});
rowList.add(new String[]{"2","b", "def"});
rowList.add(new String[]{"3","c", "ghi"});
rowList.add(new String[]{"4","a", "jkl"});
rowList.add(new String[]{"5","d", "mno"});
rowList.add(new String[]{"6","e", "pqr"});
rowList.add(new String[]{"7","b", "stu"});
Set<String[]> s = new TreeSet<String[]>(new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return o1[1].compareTo(o2[1]);
}
});
通过添加设置"s"
s.addAll(rowList);
List<Object> res = Arrays.asList(s.toArray());
打印结果
for (Object object : res) {
String[] array = (String[])object;
System.out.println(array[0]+" "+ array[1] +", "+array[2]);
}
<强>输出强>
1 a, abc
2 b, def
3 c, ghi
5 d, mno
6 e, pqr
答案 1 :(得分:1)
制作自定义方法isContain(List<String[]> rowList, String string)
private static boolean isContain(List<String[]> rowList, String secStr) {
for (String[] strings : rowList) {
if(strings[1].equals(secStr)){
return true;
}
}
return false;
}
在将项目添加到List
之前检查此方法以删除重复项目,例如:
List<String[]> rowList = new ArrayList();
String[] sts= new String[]{"1", "a", "abc"};
boolean contain= isContain(rowList,sts[1]);
if(!contain){
rowList.add(sts);
}