我有两个数组列表例如
List<Date> a;
contains : 10/10/2014, 10/11/2016
List<Date> b;
contains : 10/10/2016
我该怎么办列表a
和b
之间的检查,以便返回了在b
缺少价值?e.g。 10/10/2014
你可以将它们转换为Set
集合,并对其进行一系列差异化经营。
像这样:
Set<Date> ad = new HashSet<Date>(a);
Set<Date> bd = new HashSet<Date>(b);
ad.removeAll(bd);
如果你只是想找到失踪B值,你可以这样做:
List toReturn = new ArrayList(a);
toReturn.removeAll(b);
return toReturn;
如果你想了解这是目前在任一列表中的值,可以执行两次上的代码。随着改变的名单。
您可以通过Java的8个流图书馆使用过滤器
List<String> aList = Arrays.asList("l","e","t","'","s");
List<String> bList = Arrays.asList("g","o","e","s","t");
List<String> result = aList.stream().filter(aObject -> {
return bList.contains(aObject);
}).collect(Collectors.toList());
//or more reduced without curly braces and return
List<String> result2 = aList.stream().filter(aObject ->
!bList.contains(aObject)).collect(Collectors.toList());
System.out.println(result);
结果:
[e, t, s]
您可以使用CollectionUtils从Apache Commons Collections 4.0:
new ArrayList<>(CollectionUtils.subtract(a, b))
我一直在寻找类似的,但我想在任何名单的差值(2个列表之间罕见的元素):
让说我有:
List<String> oldKeys = Arrays.asList("key0","key1","key2","key5");
List<String> newKeys = Arrays.asList("key0","key2","key5", "key6");
我想知道哪些项已添加和钥匙拔出即我想获得(KEY1,键6)
使用org.apache.commons.collections.CollectionUtils
List<String> list = new ArrayList<>(CollectionUtils.disjunction(newKeys, oldKeys));
结果
[可以1, 可以6]
我一直在寻找不同的问题和碰到这个来了,所以我将我的解决方案添加到一个相关的问题:比较两个地图。
// make a copy of the data
Map<String,String> a = new HashMap<String,String>(actual);
Map<String,String> e = new HashMap<String,String>(expected);
// check *every* expected value
for(Map.Entry<String, String> val : e.entrySet()){
// check for presence
if(!a.containsKey(val.getKey())){
System.out.println(String.format("Did not find expected value: %s", val.getKey()));
}
// check for equality
else{
if(0 != a.get(val.getKey()).compareTo(val.getValue())){
System.out.println(String.format("Value does not match expected: %s", val.getValue()));
}
// we have found the item, so remove it
// from future consideration. While it
// doesn't affect Java Maps, other types of sets
// may contain duplicates, this will flag those
// duplicates.
a.remove(val.getKey());
}
}
// check to see that we did not receive extra values
for(Map.Entry<String,String> val : a.entrySet()){
System.out.println(String.format("Found unexpected value: %s", val.getKey()));
}
它适用于同样的原则与其他解决方案,但也比较不仅如此值都存在,但它们包含相同的值。大多是我在会计软件有两个来源的数据进行比较时,使用这种(员工和管理人员输入的值匹配,客户与企业的交易对手; ...等)
你可以调用underscore-java库U.difference(名单)方法。我是这个项目的维护者。 Live example
import com.github.underscore.U;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(1, 2);
List<Integer> list3 = U.difference(list1, list2);
System.out.println(list3);
// [3]
}
}
首先转换列表集。
// create an empty set
Set<T> set = new HashSet<>();
// Add each element of list into the set
for (T t : list)
set.add(t);
您可以使用Sets.difference(Set1, Set2)
,它返回存在于SET1额外的项目。
您可以使用Sets.difference(Set2, Set1)
,它返回存在于SET2多余的物品。
List<String> l1 = new ArrayList<String>();
l1.add("apple");
l1.add("orange");
l1.add("banana");
l1.add("strawberry");
List<String> l2 = new ArrayList<String>();
l2.add("apple");
l2.add("orange");
System.out.println(l1);
System.out.println(l2);
for (String A: l2) {
if (l1.contains(A))
l1.remove(A);
}
System.out.println("output");
System.out.println(l1);
输出:
[apple, orange, banana, strawberry]
[apple, orange]
output
[banana, strawberry]