List Interface in Java with Examples
The Java.util.The list 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. List Interface is implemented by the classes of ArrayList, LinkedList, Vector and Stack.
Declaration:
public abstract interface List extends Collection
Creating List Objects:
The list is an interface, and the instances of List can be created by implementing various classes in the following ways:
List a = new ArrayList(); List b = new LinkedList(); List c = new Vector(); List d = new Stack();
Generic List Object:
After the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the List. The type-safe List can be defined in the following way:
// Obj is the type of object to be stored in List List<Obj> list = new ArrayList<Obj> ();
Operations on List:
List Interface extends Collection, hence it supports all the operations of Collection Interface, along with following additional operations:
- Positional Access:
The list allows adding, remove, get and set operations based on numerical positions of elements in List. The list provides the following methods for these operations:- void add(int index, Object O): This method adds a given element at a specified index.
- boolean addAll(int index, Collection c): This method adds all elements from the specified collection to list. The first element gets inserted at the given index. If there is already an element at that position, that element and other subsequent elements(if any) are shifted to the right by increasing their index.
- Object remove(int index): This method removes an element from the specified index. It shifts subsequent elements(if any) to left and decreases their indexes by 1.
- Object get(int index): This method returns the element at the specified index.
- Object set(int index, Object new): This method replaces elements at a given index with a new element. This function returns the element which was just replaced by a new element.
edit
play_arrow
brightness_4
// Java program to demonstrate positional access
// operations on List interface
import
java.util.*;
public
class
ListDemo {
public
static
void
main(String[] args)
{
// Creating a list
List<Integer> l1 =
new
ArrayList<Integer>();
l1.add(
0
,
1
);
// adds 1 at 0 index
l1.add(
1
,
2
);
// adds 2 at 1 index
System.out.println(l1);
// [1, 2]
// Creating another list
List<Integer> l2 =
new
ArrayList<Integer>();
l2.add(
1
);
l2.add(
2
);
l2.add(
3
);
// Will add list l2 from 1 index
l1.addAll(
1
, l2);
System.out.println(l1);
// Removes element from index 1
l1.remove(
1
);
System.out.println(l1);
// [1, 2, 3, 2]
// Prints element at index 3
System.out.println(l1.get(
3
));
// Replace 0th element with 5
l1.set(
0
,
5
);
System.out.println(l1);
}
}
[1, 2] [1, 1, 2, 3, 2] [1, 2, 3, 2] 2 [5, 2, 3, 2]
- Search:
The list provides methods to search element and returns its numeric position. Following two methods are supported by List for this operation:- int indexOf(Object o): This method returns the first occurrence of a given element or -1 if the element is not present in the list.
- int lastIndexOf(Object o): This method returns the last occurrence of a given element or -1 if the element is not present in the list.
edit
play_arrow
brightness_4
// Java program to demonstrate search
// operations on List interface
import
java.util.*;
public
class
ListDemo {
public
static
void
main(String[] args)
{
// Type safe array list, stores only string
List<String> l =
new
ArrayList<String>(
5
);
l.add(
"Geeks"
);
l.add(
"for"
);
l.add(
"Geeks"
);
// Using indexOf() and lastIndexOf()
System.out.println(
"first index of Geeks:"
+ l.indexOf(
"Geeks"
));
System.out.println(
"last index of Geeks:"
+ l.lastIndexOf(
"Geeks"
));
System.out.println(
"Index of element"
+
" not present : "
+ l.indexOf(
"Hello"
));
}
}
first index of Geeks:0 last index of Geeks:2 Index of element not present : -1
- Iteration:
- ListIterator(extends Iterator) is used to iterate over the List element. List iterator is a bidirectional iterator. For more details about ListIterator refer to Iterators in Java.
- Range-view:
List Interface provides a method to get the List view of the portion of the given List between two indices. Following is the method supported by List for range view operation.- List subList(int fromIndex, int toIndex):This method returns List view of specified List between fromIndex(inclusive) and toIndex(exclusive).
edit
play_arrow
brightness_4
// Java program to demonstrate subList operation
// on List interface.
import
java.util.*;
public
class
ListDemo {
public
static
void
main(String[] args)
{
// Type safe array list, stores only string
List<String> l =
new
ArrayList<String>(
5
);
l.add(
"GeeksforGeeks"
);
l.add(
"Practice"
);
l.add(
"GeeksQuiz"
);
l.add(
"IDE"
);
l.add(
"Courses"
);
List<String> range =
new
ArrayList<String>();
// Return List between 2nd(including)
// and 4th element(excluding)
range = l.subList(
2
,
4
);
System.out.println(range);
}
}
[GeeksQuiz, IDE]
This article is contributed by Dharmesh Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeek's main page and help other Geeks.
post a comment