DEV Community

Cover image for Learn List in Java
Sarthak sachdeva
Sarthak sachdeva

Posted on

Learn List in Java

Java List Interface

The List Interface in Java extends the Collection Interface and is a part of java.util package.

It is used to store the ordered collections of elements. So in a Java List, you can organize and manage the data sequentially.

Maintained the order of elements
Allows the duplicate elements.

The implementation classes of the List interface are ArrayList, LinkedList, Stack, and Vector.

Can add Null values that depend on the implementation.

Java List – Operations
Since List is an interface, it can be used only with a class that implements this interface. Now, let’s see how to perform a few frequently used operations on the List.

  1. Operation 1: Adding elements to List using add() method
  2. Operation 2: Updating elements in List using set() method
  3. Operation 3: Searching for elements using indexOf(), lastIndexOf methods
  4. Operation 4: Removing elements using remove() method
  5. Operation 5: Accessing Elements in List using get() method
  6. Operation 6: Checking if an element is present in the List using contains() method

  • import list in java from java.util.package;

Image description

  • Create a list object but call List and create object of Arraylist

Image description

  • Now add data in list in this list am define a list type is String, so add data as String data .

Image description

  • Now run forEach loop in list use keyword is 'for' not write forEach

Image description

  • Now get a specific list data and print.

Image description

  • Now set a data in list in specific data indexing position . it means when i using set() method so they create new data in list with new indexing set replace the new data with specific index which i want ,

Example in list we have a one data like my name ,
list list.add("coder");
so use set() method and replace coder to another data
like : set(0 , "coding"); so 0 is indexing value of coder

Image description

  • Now get the size of list ,

Image description

  • Now remove list element using** remove()** method,
    they remove() method is overloading method,
    like : remove(int index) , remove(element)
    remove by indexing value and remove by actual element

  • REMOVE WIRH INDEX

Image description

  • REMOVE WITH ELEMENT

Image description

  • Now find element indexing position && searching element in list

Image description


  1. Adding Elements
    In order to add an element to the list, we can use the add() method. This method is overloaded to perform multiple operations based on different parameters.

  2. Updating Elements
    After adding the elements, if we wish to change the element, it can be done using the set() method. Since List is indexed, the element which we wish to change is referenced by the index of the element. Therefore, this method takes an index and the updated element which needs to be inserted at that index.

  3. Searching Elements
    Searching for elements in the List interface is a common operation in Java programming. The List interface provides several methods to search for elements, such as the indexOf(), lastIndexOf() methods.
    Parameters:

indexOf(Object o): Returns the index of the first occurrence of the specified element in the list, or -1 if the element is not found

lastIndexOf(Object o): Returns the index of the last occurrence of the specified element in the list, or -1 if the element is not found

  1. Removing Elements In order to remove an element from a list, we can use the remove() method. This method is overloaded to perform multiple operations based on different parameters. They are:
  • METHODS TO USE IN LIST

-add(int index, element)
-addAll(int index , Collection collection)
-size()
-clear()
-remove(int index)
-remove(element)
-get(int index)
-set(int index , element)
-indexOf(element)
-lastindexOf(element)
-equals(element)
-hashCode()
-isEmpty()
-contain(element)


this is full code :

Image description

full code in my github repo checkout : Github

checkout my insta account : insta

checkout my medium profile : medium

Top comments (0)