Below example explains that, how
we can get the common data or intersection of two collections.
Intersection.java:
package com.lnn.utils;
import
java.util.ArrayList;
import
java.util.Collection;
public class Intersection {
public static void main(String[]
args) {
Collection<Integer>
two = new ArrayList<Integer>();
Collection<Integer>
three = new ArrayList<Integer>();
for (int i = 1; i
< 100; i++) {
if (i % 2 == 0) {
two.add(i);
}
if (i % 3 == 0) {
three.add(i);
}
}
Collection<Integer>
common = new ArrayList<Integer>();
common.addAll(two);
common.retainAll(three);
for (int i : common) {
System.out.println(i);
}
}
}
No comments:
Post a Comment