@Test public void testSetDiffViewModify() { HashSet<String> set1 = new HashSet<>(); HashSet<String> set2 = new HashSet<>(); Set<Object> diff = Views.setDiffView(set1, set2); assertNotNull(diff); assertEquals(0, diff.size()); set1.add("test1"); set1.add("test2"); set1.add("test3"); set2.add("test3"); set2.add("test4"); set2.add("test5"); assertEquals(2, diff.size()); set1.clear(); assertEquals(0, diff.size()); }
public static <E> Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getDiff(set1, set2).iterator(); } @Override public int size() { return getDiff(set1, set2).size(); } private Set<E> getDiff(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(); hashSet.addAll(set1); hashSet.addAll(set2); return hashSet.stream().filter(new Predicate<E>() { @Override public boolean test(E e) { return set1.contains(e) && !set2.contains(e); } }).collect(Collectors.toSet()); } }; }
Views { public static <E> Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getDiff(set1, set2).iterator(); } @Override public int size() { return getDiff(set1, set2).size(); } private Set<E> getDiff(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(); hashSet.addAll(set1); hashSet.addAll(set2); return hashSet.stream().filter(new Predicate<E>() { @Override public boolean test(E e) { return set1.contains(e) && !set2.contains(e); } }).collect(Collectors.toSet()); } }; } }
Views { public static <E> Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getDiff(set1, set2).iterator(); } @Override public int size() { return getDiff(set1, set2).size(); } private Set<E> getDiff(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(); hashSet.addAll(set1); hashSet.addAll(set2); return hashSet.stream().filter(new Predicate<E>() { @Override public boolean test(E e) { return set1.contains(e) && !set2.contains(e); } }).collect(Collectors.toSet()); } }; } private  Views(); }
Views { public static <E> Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getDiff(set1, set2).iterator(); } @Override public int size() { return getDiff(set1, set2).size(); } private Set<E> getDiff(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(); hashSet.addAll(set1); hashSet.addAll(set2); return hashSet.stream().filter(new Predicate<E>() { @Override public boolean test(E e) { return set1.contains(e) && !set2.contains(e); } }).collect(Collectors.toSet()); } }; } private  Views(); static List<T> listView(List<R> originalList, Function<R, T> transformer); static Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer); static Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2); static Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2); }
Views { public static <E> Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); return new AbstractSet<E>() { @Override public Iterator<E> iterator() { return getDiff(set1, set2).iterator(); } @Override public int size() { return getDiff(set1, set2).size(); } private Set<E> getDiff(Set<? extends E> set1, Set<? extends E> set2) { HashSet<E> hashSet = new HashSet<>(); hashSet.addAll(set1); hashSet.addAll(set2); return hashSet.stream().filter(new Predicate<E>() { @Override public boolean test(E e) { return set1.contains(e) && !set2.contains(e); } }).collect(Collectors.toSet()); } }; } private  Views(); static List<T> listView(List<R> originalList, Function<R, T> transformer); static Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer); static Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2); static Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2);  }