List positional access and search operations

Manipulates elements based on their numerical position in the list. This includes methods such as get, set, add, addAll, and remove.

The basic positional access methods get, set, add and remove. (The set and remove operations return the old value that is being overwritten or removed.) Other operations (indexOf and lastIndexOf) return the first or last index of the specified element in the list.

Refer comments in source code are self descriptive.

// The basic positional access operations are get, set, add and remove.privatestaticvoid positionalAccess() {
List<String> list =newLinkedList<>();
list.add("element 1");
list.add("element 2");
list.add("element 3");
list.add("element 4");
// Replaces the element at the specified position in this list with// the specified element (optional operation).
list.set(3, "element 5");
list.forEach( str ->System.out.println(" set element 5 --"+ str));
// Inserts the specified element at the specified position in this list// (optional operation).// Shifts the element currently at that position (if any) and any// subsequent// elements to the right (adds one to their indices).
list.add(3, "element 4");
list.forEach( str ->System.out.println(" set element 5 --"+ str));
// Returns the element at the specified position in this list.System.out.println(list.get(0));
// Removes the element at the specified position in this list (optional// operation).// Shifts any subsequent elements to the left (subtracts one from their// indices).//// Returns the element that was removed from the list.
list.remove(1);
}

How to search elements in List?

indexOf() and lastIndexOf() methods can use to search an element in the list with specific index.

Please refer comments in source code are self descriptive.

privatestaticvoid searchListDemo() {
List<String> searchList =newArrayList();
searchList.add("element 1");
searchList.add("element 2");
searchList.add("element 3");
searchList.add("element 4");
// Returns the index of the first occurrence of the specified element in// this list,// or -1 if this list does not contain the element.int index = searchList.indexOf("element 2");
System.out.println(" search element at index 0 --->"+ index);
// Returns the index of the last occurrence of the specified element in// this list,// or -1 if this list does not contain the elementint lastIndex = searchList.lastIndexOf("element 2");
System.out.println(" search element at lastIndex 0 --->"+ lastIndex);
}

How to perform range view operations in List?

The range-view operation, subList(int fromIndex, int toIndex), returns a List view of the portion of this list whose indices range from fromIndex, inclusive, to toIndex, exclusive.

Traverse the list in either direction using ListIterator interface

An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().