Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Retain elements from a Collection in another Collection in Java

import java.util.ArrayList;
import java.util.List;
public class Demo {
	public static void main(String args[]) throws Exception {
		List<Integer>list = new ArrayList<Integer>();
		list.add(100);
		list.add(200);
		list.add(200);
		list.add(200);
		list.add(300);
		list.add(400);
		list.add(400);
		list.add(500);
		List <Integer>list2 = new ArrayList<Integer>();
		list2.add(100);
		list2.add(200);
		list2.add(300);
		list2.add(400);
		list.retainAll(list2);
		System.out.println("Resultant list = "+list);
	}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.