ArrayList — Most Important Methods

Kablumndl
2 min readDec 10, 2021
Below are some important methods to know1. boolean add(E e);2. void add(int index, E element);3. boolean addAll(Collection<? extends E> c);4. boolean addAll(int index, Collection<? extends E> c);5. E remove(int index);6. boolean remove(Object o);7. boolean removeAll(Collection<?> c);8. default boolean removeIf(Predicate<? super E> filter)9. boolean removeAll(Collection<?> c);

Example to cover all above methods:-

package com.demo.collections;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.function.Predicate;public class ArrayListMethods {public static void main(String[] args) {List<String> movieList = new ArrayList<>();movieList.add(“Hero”);movieList.add(“Disco”);movieList.add(“Love”);movieList.add(“Friendship”);movieList.add(“Dragon”);System.out.println(“===Printing full list===”);System.out.println(movieList);movieList.add(2, “Dancer”);System.out.println(“===Printing list after adding at index 2===”);System.out.println(movieList);List<String> oldList = new ArrayList<>();oldList.add(“Indus”);oldList.add(“Civil war”);System.out.println(“===Adding collection to another collection===”);movieList.addAll(oldList);System.out.println(movieList);System.out.println(“===Adding collection to another collection at particular index===”);List<String> newList = new ArrayList<>();newList.add(“Baggi”);newList.add(“Tigers”);movieList.addAll(4, newList);System.out.println(movieList);System.out.println(“===Removing element by index===”);String el = movieList.remove(3);System.out.println(“Element Going To Remove is:” + el);System.out.println(movieList);System.out.println(“===Removing element by index with object===”);boolean removeFlag = movieList.remove(“Indus”);System.out.println(“removeFlag:” + removeFlag);System.out.println(movieList);System.out.println(“===Removing element by collection===”);List<String> listToRemove = Arrays.asList(“Dragon”, “Tigers”);movieList.removeAll(listToRemove);System.out.println(movieList);System.out.println(“===Remove element by using predicate===”);movieList.removeIf(new Predicate<String>() {@Overridepublic boolean test(String t) {return t.startsWith(“B”);}});System.out.println(movieList);System.out.println(“===Remove by another collection===”);movieList.removeAll(Arrays.asList(“Civil war”, “Disco”));System.out.println(movieList);}}

Output:-

===Printing full list===[Hero, Disco, Love, Friendship, Dragon]===Printing list after adding at index 2===[Hero, Disco, Dancer, Love, Friendship, Dragon]===Adding collection to another collection===[Hero, Disco, Dancer, Love, Friendship, Dragon, Indus, Civil war]===Adding collection to another collection at particular index===[Hero, Disco, Dancer, Love, Baggi, Tigers, Friendship, Dragon, Indus, Civil war]===Removing element by index===Element Going To Remove is:Love[Hero, Disco, Dancer, Baggi, Tigers, Friendship, Dragon, Indus, Civil war]===Removing element by index with object===removeFlag:true[Hero, Disco, Dancer, Baggi, Tigers, Friendship, Dragon, Civil war]===Removing element by collection===[Hero, Disco, Dancer, Baggi, Friendship, Civil war]===Remove element by using predicate===[Hero, Disco, Dancer, Friendship, Civil war]===Remove by another collection===[Hero, Dancer, Friendship]

=============== Happy Learning ================

--

--

Kablumndl

Java Developer, Software Engineer, Spring, Spark, MicroService, PostgresSQL