ArrayList.retainAll()

ArrayList

ArrayList.retainAll(list) retains all matching objects of input list and removes others .
Returns true if any element is removed based on matching , otherwise false.

Examples with output

add()
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("HTML");
languages.add("Python");
languages.add("PhotoShop");
System.out.println(languages); //[Java, PHP, HTML, Python, PhotoShop]

ArrayList<String> front_end = new ArrayList<String>();
front_end.add("HTML");
front_end.add("PhotoShop");
front_end.add("JavaScript");

languages.retainAll(front_end);
System.out.println(languages); //[HTML, PhotoShop]
In above code we created a second lsit front_end, in this list two elements HTML and PhtoShop are matching with elements of our main ArrayList languages. So retainAll() has retained these two matching elements and removed all others from our main list languages.

As we get boolean output, we can use if else to show messages.
public static void main(String[] args) {
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("HTML");
languages.add("Python");
languages.add("PhotoShop");
System.out.println(languages); //[Java, PHP, HTML, Python, PhotoShop]

ArrayList<String> front_end = new ArrayList<String>();
front_end.add("HTML");
front_end.add("PhotoShop");
front_end.add("JavaScript");

if(languages.retainAll(front_end)) {
 System.out.println("Retained matching Elements only"); 
}else {
 System.out.println("Noting matched or removed ");	
}
 System.out.println(languages); //[HTML, PhotoShop]
Output
[Java, PHP, HTML, Python, PhotoShop]
Retained matching Elements only
[HTML, PhotoShop]
In above code as we removed two matching element , the output is true. Now change the name of the elements of front_end by changing them like this.
front_end.add("HTML2");
front_end.add("PhotoShop2");
front_end.add("JavaScript");
The output will change like this.
[Java, PHP, HTML, Python, PhotoShop]
Retained matching Elements only
[]
Note that we will get true output if there is any remeval of element. If all are matching ( no removal ) then we will get false as output.
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("HTML");
languages.add("Python");
languages.add("PhotoShop");
System.out.println(languages); //[Java, PHP, HTML, Python, PhotoShop]

ArrayList<String> front_end = new ArrayList<String>();
front_end.add("Java");
front_end.add("PHP");
front_end.add("HTML");
front_end.add("Python");
front_end.add("PhotoShop");
if(languages.retainAll(front_end)) {
  System.out.println("Retained matching Elements only"); 
}else {
  System.out.println("Noting removed ");	
}
  System.out.println(languages); //[Java, PHP, HTML, Python, PhotoShop]

More than one matching elements

What happens if there are more than one matching elements in the main list ?
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("HTML");
languages.add("Python");
languages.add("HTML");
languages.add("PhotoShop");
System.out.println(languages); //[Java, PHP, HTML, Python, HTML, PhotoShop]

ArrayList<String> front_end = new ArrayList<String>();
front_end.add("HTML");
front_end.add("PhotoShop");
front_end.add("JavaScript");

languages.retainAll(front_end);
System.out.println(languages); //[HTML, HTML, PhotoShop]
You can see HTML is retained in both places. So all the matching positions are retained.

ArrayList Tutorials



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer