Tutorial by Examples

We can create an ArrayList (following the List interface): List aListOfFruits = new ArrayList(); Java SE 5 List<String> aListOfFruits = new ArrayList<String>(); Java SE 7 List<String> aListOfFruits = new ArrayList<>(); Now, use the method add to add a String: ...
Standard Collections Java Collections framework A simple way to construct a List from individual data values is to use java.utils.Arrays method Arrays.asList: List<String> data = Arrays.asList("ab", "bc", "cd", "ab", "bc", "cd"); ...
Following ways can be used for joining lists without modifying source list(s). First approach. Has more lines but easy to understand List<String> newList = new ArrayList<String>(); newList.addAll(listOne); newList.addAll(listTwo); Second approach. Has one less line but less readab...
It is tricky to remove items from a list while within a loop, this is due to the fact that the index and length of the list gets changed. Given the following list, here are some examples that will give an unexpected result and some that will give the correct result. List<String> fruits = new...
Sometimes it's not a good practice expose an internal collection since it can lead to a malicious code vulnerability due to it's mutable characteristic. In order to provide "read-only" collections java provides its unmodifiable versions. An unmodifiable collection is often a copy of a mod...
Iterating over List List<String> names = new ArrayList<>(Arrays.asList("Clementine", "Duran", "Mike")); Java SE 8 names.forEach(System.out::println); If we need parallelism use names.parallelStream().forEach(System.out::println); Java SE 5 fo...
Sometimes it is appropriate to use an immutable empty collection. The Collections class provides methods to get such collections in an efficient way: List<String> anEmptyList = Collections.emptyList(); Map<Integer, Date> anEmptyMap = Collections.emptyMap(); Set<Number> anEmptySe...
Collections in Java only work for objects. I.e. there is no Map<int, int> in Java. Instead, primitive values need to be boxed into objects, as in Map<Integer, Integer>. Java auto-boxing will enable transparent use of these collections: Map<Integer, Integer> map = new HashMap<&g...
Above I noticed an example to remove items from a List within a Loop and I thought of another example that may come in handy this time using the Iterator interface. This is a demonstration of a trick that might come in handy when dealing with duplicate items in lists that you want to get rid of. N...
To ensure that our collection can be iterated using iterator or for-each loop, we have to take care of following steps: The stuff we want to iterate upon has to be Iterable and expose iterator(). Design a java.util.Iterator by overriding hasNext(), next() and remove(). I have added a simple ...
This exception occurs when a collection is modified while iterating over it using methods other than those provided by the iterator object. For example, we have a list of hats and we want to remove all those that have ear flaps: List<IHat> hats = new ArrayList<>(); hats.add(new Ushanka...
List subList(int fromIndex, int toIndex) Here fromIndex is inclusive and toIndex is exclusive. List list = new ArrayList(); List list1 = list.subList(fromIndex,toIndex); If the list doesn't exist in the give range, it throws IndexOutofBoundException. What ever changes made on the list1 wi...

Page 1 of 1