Manipulating List in Java
Working with List in java
Introduction
List interface is an ordered collection of objects in which duplicate values can be stored.
Properties of a List
Elements can be inserted or accessed by their position in the list.
A list may contain duplicate elements.
/// try this out
import java.util.*;
public class ListController {
public static void main(String[] args) {
List<String> a1 = new ArrayList<>();
a1.add("Mercy");
a1.add("Mercy");
a1.add("Jemosop");
System.out.print("\t" + a1);
}
}
A list interface is implemented by the following classes:
Check the above example on ArrayList implementation
- LinkedList
List<String> a1 = new LinkedList<>();
a1.add("Mercy");
a1.add("Mercy");
a1.add("Jemosop");
System.out.print("\t" + a1);
- Vector, it implements grow-able array
List<String> a1 = new Vector<>();
a1.add("Mercy");
a1.add("Mercy");
a1.add("Jemosop");
System.out.print("\t" + a1);
- Stack, this class is based on the basic principle of last-in-first-out
List<String> a1 = new Stack<>();
a1.add("Mercy");
a1.add("Mercy");
a1.add("Jemosop");
System.out.print("\t" + a1);
Creating list objects.
List a = new ArrayList();
List b = new LinkedList();
List c = new Vector();
List d = new Stack();
List Method
Common list methods
- void add(int index, Object obj)
Inserts obj into the invoking list at the index passed in the index. Any pre-existing elements at or beyond the point of insertion are shifted up. Thus, no elements are overwritten.
List<String> a1 = new Stack<>();
a1.add(0,"Mercy a1");
a1.add(1,"Mercy a1" );
a1.add(2,"Jemosop a1");
System.out.print("\t" + a1);///output[Mercy a1, Mercy a1, Jemosop a1]
- boolean addAll(int index, Collection c)
Inserts all elements of c(another list) into the invoking list(present list) at the index passed in the index. Any pre-existing elements at or beyond the point of insertion are shifted up. Thus, no elements are overwritten. Returns true if the invoking list changes and returns false otherwise.
List<String> a1 = new Stack<>();
a1.add(0,"Mercy a1");
a1.add(1,"Mercy a1" );
a1.add(2,"Jemosop a1");
List<String> a2 = new Stack<>();
a2.add(0,"Java ");
a2.add(1,"Angular " );
a2.add(2,"Dart ");
a1.addAll(0,a2);
System.out.print("\t" + a1);/// output
[Java , Angular , Dart , Mercy a1, Mercy a1, Jemosop a1]
- Object get(int index)
Returns the object stored at the specified index within the invoking collection. N.B index starts at 0.
List<String> a2 = new Stack<>();
a2.add(0,"Java ");
a2.add(1,"Angular " );
a2.add(2,"Dart ");
a1.addAll(0,a2);
System.out.print("Combined list"+"\n\t" + a1 +"\n");
String index2= a1.get(5);
System.out.print("Get element at index 5"+"\n\t" + index2);/// output
Combined list
[Java , Angular , Dart , Mercy a1, Mercy a1, Jemosop a1]
Get element at index 5
Jemosop a1
- int indexOf(Object obj)
Returns the index of the first instance of obj in the invoking list. If obj is not an element of the list, .1 is returned.
List<String> a1 = new Stack<>();
a1.add(0,"Mercy a1");
a1.add(1,"Mercy a1" );
a1.add(2,"Jemosop a1");
List<String> a2 = new Stack<>();
a2.add(0,"Java ");
a2.add(1,"Angular " );
a2.add(2,"Dart ");
a1.addAll(0,a2);
System.out.print("Combined list"+"\n\t" + a1 +"\n");
String index2= a1.get(5);
System.out.print("Get element at index 5"+"\n\t" + index2 +"\n");/// output
Combined list
[Java , Angular , Dart , Mercy a1, Mercy a1, Jemosop a1]
Get element at index 5
Jemosop a1
The index of dart in the list
2
- int lastIndexOf(Object obj)
Returns the index of the last instance of obj in the invoking list. If obj is not an element of the list, -1 is returned.
List<String> a2 = new Stack<>();
a2.add(0,"Java");
a2.add(1,"Angular" );
a2.add(2,"Dart");
a2.add(3,"Java");
Integer lastIndexOf=a2.lastIndexOf("Java");
System.out.printf("The last index of dart in the list \n" +lastIndexOf);// output
The last index of dart in the list
3
- Object remove(int index)
Removes the element at position index from the invoking list and returns the deleted element. The resulting list is compacted. That is, the indexes of subsequent elements are decremented by one.
List<String> a2 = new Stack<>();
a2.add(0,"Java");
a2.add(1,"Angular" );
a2.add(2,"Dart");
a2.add(3,"Java");
System.out.printf("List before item in index 3(java) is removed \n" +a2+"\n");
a2.remove(3);
System.out.printf("List after item in index 3(java) is removed \n" +a2);///output
List before item in index 3(java) is removed
[Java, Angular, Dart, Java]
List after item in index 3(java) is removed
[Java, Angular, Dart]
- Object set(int index, Object obj)
Assigns obj to the location specified by index within the invoking list.This method replaces element at given index with new element.
List<String> a2 = new Stack<>();
a2.add(0,"Java");
a2.add(1,"Angular" );
a2.add(2,"Dart");
a2.add(3,"Java");
System.out.printf("List before item in index 3(java) is replaced \n" +a2+"\n");
a2.set(3,"python");
System.out.printf("List after item in index 3(java) is replaced \n" +a2);///output
List before item in index 3(java) is replaced
[Java, Angular, Dart, Java]
List after item in index 3(java) is replaced
[Java, Angular, Dart, python]
- ListIterator listIterator(int index)
Returns an iterator to the invoking list that begins at the specified index. index − index of the first element to be returned from the list-iterator.This method returns a ListIterator of the elements in this list (in proper sequence), starting at the specified position in the list. IndexOutOfBoundsException − is displayed if the index is out of range.
List<String> a2 = new Stack<>();
a2.add(0,"Java");
a2.add(1,"Angular" );
a2.add(2,"Dart");
a2.add(3,"Java");
// print the list
System.out.println("List:\n" + a2 +"\n");
// set Iterator at specified index
Iterator x = a2.listIterator(2);
// print list with the iterator
while (x.hasNext()) {
System.out.println(x.next());
}/// output
- ListIterator listIterator( )
Returns an iterator to the start of the invoking list.
List<String> a2 = new Stack<>();
a2.add(0,"Java");
a2.add(1,"Angular" );
a2.add(2,"Dart");
a2.add(3,"Java");
// print the list
System.out.println("List:\n" + a2 +"\n");
// set Iterator at specified index
Iterator x = a2.listIterator();
// print list with the iterator
while (x.hasNext()) {
System.out.println(x.next());
}/// ouput
List:
[Java, Angular, Dart, Java]Java
Angular
Dart
Java
N.B Iterator has many methods, you can research on that.
sources: javatpoint, tutorialspoint
Corrections and additions are welcomed.
Happy coding!!!!!!!!!!!!!!!!!!