ArrayList.removeAll()

ArrayList.removeAll(list) removes all matching objects of input list.
Returns true if any element is removed, 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.removeAll(front_end);
System.out.println(languages); //[Java, PHP, Python]
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 removeAll() has removed these two matching elements from our main list languages.

As we get boolean output, we can use if else to show messages.
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.removeAll(front_end)) {
System.out.println("Removed Elements"); 
}else {
  System.out.println("Not removed ");	
}
  System.out.println(languages); //[Java, PHP, Python]
}
Output
[Java, PHP, HTML, Python, PhotoShop]
Removed Elements
[Java, PHP, HTML, Python]
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]
Not removed 
[Java, PHP, HTML, Python, PhotoShop]
Note that we will get true output even if there is only one matching removal. If no matching element is there ( no removal ) then we will get false as output.

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.removeAll(front_end);
System.out.println(languages); //[Java, PHP, Python]
You can see HTML is removed in both places. So all the matching positions are removed.

ArrayList Tutorials


Java

Subscribe

* indicates required
Subscribe to plus2net

    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-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer