Saturday, February 11, 2023

10 Examples of ArrayList in Java

ArrayList Example in Java
Hello guys, In this Java ArrayList Tutorial, we will see 10 common usage of ArrayList with examples. You will learn how to add elements in ArrayList, how to remove elements from ArrayList, how to check if ArrayList contains a given object, how to sort an ArrayList of objects  and several other ArrayList functions which we use daily as a Java programmer. ArrayList is one of the most popular classes from the Java Collection framework along with HashSet and HashMap and a good understanding of the ArrayList class and methods is imperative for Java developers. ArrayList is an implementation of List Collection which is ordered and allows duplicates

ArrayList is also index-based and provides constant-time performance for common methods e.g. get(). Apart from being very popular among Java programmers, ArrayList is also a very popular interview topic.

Questions like the difference between Vector and ArrayList and LinkedListvs ArrayList is hugely popular in various Java interviews, especially with 2 to 3 years of experience. Along with Vector, this is one of the first collection classes many Java programmers use. 

By the way, we have already seen some ArrayList tutorials e.g. ArrayList sorting example,  converting Array to ArrayList,  looping through ArrayList which is good to understand ArrayList in Java.





10 Java ArrayList Examples

In this section, we will see actual code examples of various ArrayList functionality like add, remove, contains, clear, size, isEmpty, etc. You can use this Java program to play with ArrayList yourself. For example, you can run this Java program in your favorite IDE like IntelliJIDEA, NetBeans, Eclipse, or even on any online Java Editor. 

10 Examples of ArrayList in Java


And, here is our complete Java program which demonstrates how to work with ArrayList. 


  
import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * Java ArrayList Examples - list of frequently used examples in ArrayList e.g. adding
 * elements, removing elements, contains examples, etc
 * @author
 */

public class ArrayListTest {

    public static void main(String args[]) {
     
        //How to create ArrayList in Java - example
        ArrayList<String> list = new ArrayList<String>();
     
        //Java ArrayList add Examples
        list.add("Apple");
        list.add("Google");
        list.add("Samsung");
        list.add("Microsoft");
   
        //Java ArrayList contains Example, equals method is used to check if
        //ArrayList contains an object or not
        System.out.println("Does list contains Apple :" + list.contains("Apple"));
        System.out.println("Does list contains Verizon :" + list.contains("Verizon"));
     
        //Java ArrayList Example - size
        System.out.println("Size of ArrayList is : " + list.size());
     
        //Java ArrayList Example - replacing an object
        System.out.println("list before updating : " + list);
        list.set(3, "Bank of America");
        System.out.println("list after update : " + list);
     
        //Java ArrayList Example - checking if ArrayList is empty
        System.out.println("Does this ArrayList is empty : " + list.isEmpty());
     
        //Java ArrayList Example - removing an Object from ArrayList
        System.out.println("ArrayList before removing element : " + list);
        list.remove(3); //removing fourth object in ArrayList
        System.out.println("ArrayList after removing element : " + list);
     
       //Java ArrayList Example - finding index of Object in List
        System.out.println("What is index of Apple in this list : " + list.indexOf("Apple"));
     
        //Java ArrayList Example - converting List to Array
        String[] array = list.toArray(new String[]{});
        System.out.println("Array from ArrayList : " + Arrays.toString(array));
     
        //Java ArrayList Example : removing all elements from ArrayList
        list.clear();
        System.out.println("Size of ArrayList after clear : " + list.size());
    }
 
}

Output:
Does the list contains Apple: true
Does the list contains Verizon: false
The size of ArrayList is: 4
list before updating : [Apple, Google, Samsung, Microsoft]
list after update : [Apple, Google, Samsung, Bank of America]
Does this ArrayList is empty: false
ArrayList before removing element : [Apple, Google, Samsung, Bank of America]
ArrayList after removing element : [Apple, Google, Samsung]
What is an index of Apple in this list : 0
Array from ArrayList : [Apple, Google, Samsung]
Size of ArrayList after clear : 0


These were some frequently used examples of ArrayList in Java. As a Java developer, you should be familiar with these common ArrayList operations so that you can use the ArrayList class in your Java programmer whenever you want. We have seen ArrayList contains an example that used the equals method to check if an object is present in ArrayList or not. We have also seen how to add, remove and modify the contents of ArrayList, etc.


Other Java Collection tutorial and Interview Questions


1 comment:

  1. Good tutorial for any beginners, who doesn't know anything about ArrayList in Java. I would like to see some practical example of using Java ArrayList, than operating over them e.g. How about using ArrayList for storing list of employees in a department? than you have different question to answer e.g.
    does a particular employee belongs to this department?
    sorting them based upon their age? etc

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.