Category Archives: ArrayList

Get, Add, Remove, Retain, Sort & iterate methods of arraylist collection (java/ example)

ArrayList class is resizable array implementation of the List interface. ArrayList maintains the insertion order of element or string objects. ArrayList allows the duplicate elements & we can randomly access elements by index. ArrayList is not thread safe. If multiple threads access an ArrayList instance concurrently, then arraylist must be synchronized externally. Given an arraylist… Read More »

Iterate or loop arraylist collection of String objects/elements in java (example)

ArrayList class is resizable array implementation of the List interface. ArrayList maintains the insertion order of element or string objects. ArrayList allows the duplicate elements & we can randomly access elements by index. ArrayList is not thread safe. If multiple threads access an ArrayList instance concurrently, then arraylist must be synchronized externally. The iterators returned… Read More »

Retain or remove String objects/ elements of arraylist collection (java /example)

Given an arraylist collection of String objects in java. We would like to retain certain elements of arraylist (and would like to remove rest of elements of arraylist). Arraylist has following method to retain elements. boolean retainAll(Collection<?> c) Retains only the elements in this list that are contained in the specified collection. We will create… Read More »

Sort elements of arraylist in java (ascending and descending order)

Given an arraylist of string objects in java. We would like to sort elements of arraylist. Sort elements of arraylist in ascending and descending order. 1.  Sort String objects of arraylist in ascending & descending order (java). package org.learn.collection.list.arrayList; import java.util.ArrayList; import java.util.Collections; public class SortElementInArrayList { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>(); arrayList.add(“archery”);… Read More »

Add/ insert elements/String objects to arraylist collection (java/ example)

Given an arraylist collection, add or insert elements/Objects to arraylist collection. ArrayList class is resizable array implementation of the List interface. ArrayList maintains the insertion order of element or string objects. ArrayList allows the duplicate elements & we can randomly access elements by index. ArrayList is not thread safe. If multiple threads access an ArrayList… Read More »

Remove or delete elements/nodes/objects from arraylist collection (java/example)

Given an arraylist containing string objects, we would like to remove or delete elements from arraylist collection. Arraylist has following methods to remove or delete element(s)/nodes/objects. Program – remove or delete elements/nodes from arraylist in java package org.learn.collection.list.arrayList; import java.util.ArrayList; public class DemoRemoveInArrayList { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>();… Read More »