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.
- Operation 1: Adding elements to List using add() method
- Operation 2: Updating elements in List using set() method
- Operation 3: Searching for elements using indexOf(), lastIndexOf methods
- Operation 4: Removing elements using remove() method
- Operation 5: Accessing Elements in List using get() method
- Operation 6: Checking if an element is present in the List using contains() method
- import list in java from java.util.package;
- Create a list object but call List and create object of Arraylist
- Now add data in list in this list am define a list type is String, so add data as String data .
- Now run forEach loop in list use keyword is 'for' not write forEach
- Now get a specific list data and print.
- 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
- Now get the size of list ,
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 elementREMOVE WIRH INDEX
- REMOVE WITH ELEMENT
- Now find element indexing position && searching element in list
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.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.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
- 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 :
full code in my github repo checkout : Github
checkout my insta account : insta
checkout my medium profile : medium
Top comments (0)