Returns an unmodifiable view of the intersection of two sets. The returned set contains all elements that are contained by both backing sets. The iteration order of the returned set matches that of set1. Results are undefined if set1 and set2 are sets based on diff
(
final Set<E> set1, final Set<?> set2)
| 697 | |
| 698 | |
| 699 | public static <E> SetView<E> intersection( |
| 700 | final Set<E> set1, final Set<?> set2) { |
| 701 | checkNotNull(set1, "set1"); |
| 702 | checkNotNull(set2, "set2"); |
| 703 | final Predicate<Object> inSet2 = Predicates.in(set2); |
| 704 | return new SetView<E>() { |
| 705 | @Override |
| 706 | public Iterator<E> iterator() { |
| 707 | return Iterators.filter(set1.iterator(), inSet2); |
| 708 | } |
| 709 | |
| 710 | @Override |
| 711 | public int size() { |
| 712 | return Iterators.size(iterator()); |
| 713 | } |
| 714 | |
| 715 | @Override |
| 716 | public boolean isEmpty() { |
| 717 | return !iterator().hasNext(); |
| 718 | } |
| 719 | |
| 720 | @Override |
| 721 | public boolean contains(Object object) { |
| 722 | return set1.contains(object) && set2.contains(object); |
| 723 | } |
| 724 | |
| 725 | @Override |
| 726 | public boolean containsAll(Collection<?> collection) { |
| 727 | return set1.containsAll(collection) && set2.containsAll(collection); |
| 728 | } |
| 729 | }; |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Returns an unmodifiable <b>view</b> of the difference of two sets. The |
no test coverage detected