0% found this document useful (0 votes)
17 views16 pages

List Interface

Uploaded by

Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views16 pages

List Interface

Uploaded by

Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

List Interface

The List interface in Java provides a way to store the ordered collection. It
is a child interface of Collection. It is an ordered collection of objects in
which duplicate values can be stored. Since List preserves the insertion
order, it allows positional access and insertion of elements.

The List interface is found in java.util package and inherits the Collection
interface. It is a factory of the ListIterator interface. Through the
ListIterator, we can iterate the list in forward and backward directions.
The implementation classes of the List interface are ArrayList, LinkedList,
Stack, and Vector. ArrayList and LinkedList are widely used in Java
programming. The Vector class is deprecated since Java 5.

Syntax of Java List


List<Obj> list = new ArrayList<Obj> ();

Operations & Methods in a Java List Interface

1. Adding elements to List class using add() method


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.
Parameters: It takes 2 parameters, namely:
• add(Object): This method is used to add an element at the end
of the List.
• add(int index, Object): This method is used to add an element
at a specific index in the List

2. 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:
Parameters:
• remove(Object): This method is used to simply remove an
object from the List. If there are multiple such objects, then the
first occurrence of the object is removed.
• remove(int index): Since a List is indexed, this method takes an
integer value which simply removes the element present at that
specific index in the List. After removing the element, all the
elements are moved to the left to fill the space and the indices
of the objects are updated.

Example:
// Java Program to Remove Elements from a List

// Importing List and ArrayList classes


// from java.util package
import java.util.ArrayList;
import java.util.List;

// Main class
class GFG {

// Main driver method


public static void main(String args[])
{

// Creating List class object


List<String> al = new ArrayList<>();

// Adding elements to the object


// Custom inputs
al.add("Geeks");
al.add("Geeks");
// Adding For at 1st indexes
al.add(1, "For");

// Print the initialArrayList


System.out.println("Initial ArrayList " + al);

// Now remove element from the above list


// present at 1st index
al.remove(1);

// Print the List after removal of element


System.out.println("After the Index Removal " + al);

// Now remove the current object from the updated


// List
al.remove("Geeks");

// Finally print the updated List now


System.out.println("After the Object Removal "+ al);

}
}
3. 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.

Example:
// Java Program to Update Elements in a List

// Importing utility classes


import java.util.*;

// Main class
class GFG {

// Main driver method


public static void main(String args[])
{
// Creating an object of List interface
List<String> al = new ArrayList<>();

// Adding elements to object of List class


al.add("Geeks");
al.add("Geeks");
al.add(1, "Geeks");

// Display theinitial elements in List


System.out.println("Initial ArrayList " + al);
// Setting (updating) element at 1st index
// using set() method
al.set(1, "For");

// Print and display the updated List


System.out.println("Updated ArrayList " + al);
}
}

4. Searching for 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.
The indexOf() method returns the index of the first occurrence of a
specified element in the list, while the lastIndexOf() method returns the
index of the last occurrence of a specified element.
Parameters:
• indexOf(element): Returns the index of the first occurrence of
the specified element in the list, or -1 if the element is not
found
• lastIndexOf(element): Returns the index of the last occurrence
of the specified element in the list, or -1 if the element is not
found
Example:
import java.util.ArrayList;
import java.util.List;

public class ListExample {


public static void main(String[] args)
{
// create a list of integers
List<Integer> numbers = new ArrayList<>();

// add some integers to the list


numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(2);

// use indexOf() to find the first occurrence of an


// element in the list
int index = numbers.indexOf(2);
System.out.println(
"The first occurrence of 2 is at index "
+ index);

// use lastIndexOf() to find the last occurrence of


// an element in the list
int lastIndex = numbers.lastIndexOf(2);
System.out.println(
"The last occurrence of 2 is at index "
+ lastIndex);
}
}

5. Accessing Elements
In order to access an element in the list, we can use the get() method,
which returns the element at the specified index
Parameters:
get(int index): This method returns the element at the specified index in
the list.

Example:
// Java Program to Access Elements of a List

// Importing all utility classes


import java.util.*;

// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an object of List interface,
// implemented by ArrayList class
List<String> al = new ArrayList<>();
// Adding elements to object of List interface
al.add("Geeks");
al.add("For");
al.add("Geeks");

// Accessing elements using get() method


String first = al.get(0);
String second = al.get(1);
String third = al.get(2);

// Printing all the elements inside the


// List interface object
System.out.println(first);
System.out.println(second);
System.out.println(third);
System.out.println(al);
}
}
6. Checking if an element is present in the List
In order to check if an element is present in the list, we can use
the contains() method. This method returns true if the specified element
is present in the list, otherwise, it returns false.
Parameters:
contains(Object): This method takes a single parameter, the object to be
checked if it is present in the list.

Example:
// Java Program to Check if an Element is Present in a List

// Importing all utility classes


import java.util.*;

// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an object of List interface,
// implemented by ArrayList class
List<String> al = new ArrayList<>();

// Adding elements to object of List interface


al.add("Geeks");
al.add("For");
al.add("Geeks");

// Checking if element is present using contains()


// method
boolean isPresent = al.contains("Geeks");
// Printing the result
System.out.println("Is Geeks present in the list? "
+ isPresent);
}
}

7. addAll()

This method appends all of the elements in the specified collection to the
end of this list, in the order that they are returned by the specified
collection’s iterator.

Parameters: This function has a single parameter, i.e, Collection c, whose


elements are to be appended to the list.
Returns: It returns true if the elements of specified list is appended and
list changes.
Below programs show the implementation of this method.
Example:
// Java code to show the implementation of
// addAll method in list interface
import java.util.*;
public class GfG {

// Driver code
public static void main(String[] args)
{
// Initializing a list of type arraylist
List<Integer> l = new ArrayList<>();
l.add(10);
l.add(15);
l.add(20);
System.out.println(l);

// Initializing a collection to be appended to list


ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(100);
arr.add(200);
arr.add(300);
System.out.println(arr);

l.addAll(arr);

System.out.println(l);
}
}
8. size() method

The size() method of the List interface in Java is used to get the number
of elements in this list. That is, the list size() method returns the count of
elements present in this list container.
// Java program to Illustrate size() method
// of List class for Integer value

// Importing required classes


import java.util.*;

// Main class
public class GFG {

// Main driver method


public static void main(String[] arg)
{
// Creating object of ArrayList class
List<Integer> list = new ArrayList<Integer>();

// Populating List by adding integer elements


// using add() method
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);

// Printing elements of List


System.out.println("Before operation: " + list);

// Getting total size of list


// using size() method
int size = list.size();

// Printing the size of List


System.out.println("Size of list = " + size);
}
}

9. clear() method
The clear() method of List interface in Java is used to remove all of the
elements from the List container. This method does not deleted the List
container, instead it just removes all of the elements from the List.

Example
// Java code to illustrate clear() method
import java.io.*;
import java.util.*;

public class ListDemo {


public static void main(String[] args)
{
// create an empty list with an initial capacity
List<String> list = new ArrayList<String>(5);

// use add() method to initially


// add elements in the list
list.add("Geeks");
list.add("For");
list.add("Geeks");

// Remove all elements from the List


list.clear();

// print the List


System.out.println(list);
}
}

Iterating over List Interface in Java


Till now we are having a very small input size and we are doing
operations manually for every entity. Now let us discuss various ways by
which we can iterate over the list to get them working for a larger
sample set.
Methods: There are multiple ways to iterate through the List. The most
famous ways are by using the basic for loop in combination with a get()
method to get the element at a specific index and the advanced for a
loop.
Example:
// Java program to Iterate the Elements
// in an ArrayList

// Importing java utility classes


import java.util.*;

// Main class
public class GFG {

// main driver method


public static void main(String args[])
{
// Creating an empty Arraylist of string type
List<String> al = new ArrayList<>();

// Adding elements to above object of ArrayList


al.add("Geeks");
al.add("Geeks");

// Adding element at specified position


// inside list object
al.add(1, "For");

// Using for loop for iteration


for (int i = 0; i < al.size(); i++) {

// Using get() method to


// access particular element
System.out.print(al.get(i) + " ");
}

// New line for better readability


System.out.println();

// Using for-each loop for iteration


for (String str : al)

// Printing all the elements


// which was inside object
System.out.print(str + " ");
}
}

You might also like