@Test
public void checkDuplicate1() {
List<String> list = getStringList();
String temp;
for (int i = 0; i < list.size() - 1; i++) {
temp = list.get(i);
for (int j = i + 1; j < list.size(); j++) {
if (temp.equals(list.get(j))) {
System.out.println("第" + (i + 1) + "个跟第" +
(j + 1) + "个重复,值是:" + temp);
}
}
}
}
//第3个跟第8个重复,值是:3
/**
* 检查是否有重复
*/
@Test
public void checkDuplicate2() {
final List<String> list = getStringList();
Disposable disposable = Observable.fromIterable(list)
.distinct()
.toList()
.flatMap(new Function<List<String>, SingleSource<Long>>() {
@Override
public SingleSource<Long> apply(List<String> strings) throws Exception {
return Observable.fromIterable(strings).count();
}
})
.map(new Function<Long, Boolean>() {
@Override
public Boolean apply(Long aLong) throws Exception {
System.out.println("========去重前list.size()=======" + list.size());
System.out.println("========去重后aLong=======" + aLong);
return aLong == list.size();
}
})
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
if (aBoolean) {
System.out.println("========没有重复==========");
} else {
System.out.println("=========有重复!!!=========");
}
}
});
}
//========去重前list.size()==========10
//========去重后aLong==========9
//=========有重复!!!=========
private List<String> getStringList() {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
list.add("6");
list.add("7");
list.add("3");//
list.add("8");
list.add("9");
return list;
}
rxjava : 检测重复,不去重
于 2019-10-15 16:19:05 首次发布