Tutorial by Examples

The Collections class offers two standard static methods to sort a list: sort(List<T> list) applicable to lists where T extends Comparable<? super T>, and sort(List<T> list, Comparator<? super T> c) applicable to lists of any type. Applying the former requires amending...
Giving your list a type To create a list you need a type (any class, e.g. String). This is the type of your List. The List will only store objects of the specified type. For example: List<String> strings; Can store "string1", "hello world!", "goodbye", etc, b...
The List API has eight methods for positional access operations: add(T type) add(int index, T type) remove(Object o) remove(int index) get(int index) set(int index, E element) int indexOf(Object o) int lastIndexOf(Object o) So, if we have a List: List<String> strings = new ArrayL...
For the example, lets say that we have a List of type String that contains four elements: "hello, ", "how ", "are ", "you?" The best way to iterate over each element is by using a for-each loop: public void printEachElement(List<String> list){ for...
Lets suppose you have 2 Lists A and B, and you want to remove from B all the elements that you have in A the method in this case is List.removeAll(Collection c); #Example: public static void main(String[] args) { List<Integer> numbersA = new ArrayList<>(); List<Intege...
Suppose you have two lists: A and B, and you need to find the elements that exist in both lists. You can do it by just invoking the method List.retainAll(). Example: public static void main(String[] args) { List<Integer> numbersA = new ArrayList<>(); List<Integer> numb...
List<Integer> nums = Arrays.asList(1, 2, 3); List<String> strings = nums.stream() .map(Object::toString) .collect(Collectors.toList()); That is: Create a stream from the list Map each element using Object::toString Collect the String values into a List using Collectors...
ArrayList is one of the inbuilt data structures in Java. It is a dynamic array (where the size of the data structure not needed to be declared first) for storing elements (Objects). It extends AbstractList class and implements List interface. An ArrayList can contain duplicate elements where it mai...
This example is about replacing a List element while ensuring that the replacement element is at the same position as the element that is replaced. This can be done using these methods: set(int index, T type) int indexOf(T type) Consider an ArrayList containing the elements "Program sta...
The Collections class provides a way to make a list unmodifiable: List<String> ls = new ArrayList<String>(); List<String> unmodifiableList = Collections.unmodifiableList(ls); If you want an unmodifiable list with one item you can use: List<String> unmodifiableList = Col...
The Collections class allows for you to move objects around in the list using various methods (ls is the List): Reversing a list: Collections.reverse(ls); Rotating positions of elements in a list The rotate method requires an integer argument. This is how many spots to move it along the line b...
The List interface is implemented by different classes. Each of them has its own way for implementing it with different strategies and providing different pros and cons. Classes implementing List These are all of the public classes in Java SE 8 that implement the java.util.List interface: Abs...

Page 1 of 1