Learn to remove duplicate elements in ArrayList in Java using different techniques such as LinkedHashSet in Collections framework and using java 8 stream apis. 1. Remove duplicate elements in arraylist using LinkedHashSet The LinkedHashSet is the best approach for removing duplicate elements in an arraylist. LinkedHashSet does two things internally : Remove duplicate elements Maintain […]
ArrayList spliterator() returns the instance of Spliterator which is last-binding and fail-fast. Java Spliterator is used for traversing and partitioning elements of a source such as array, Set, List or IO channel. 1. ArrayList spliterator() method 1.1. Method definition In case of arraylist, spliterator() method return the instance of ArrayListSpliterator class which in inner class […]
ArrayList removeIf() iterate the list and removes all of the elements of this list that satisfy the given predicate. 1. ArrayList removeIf() method The removeIf() method takes single argument of type Predicate. The Predicate interface is a functional interface that represents a condition (boolean-valued function) of one argument. It checks that is a given argument […]
ArrayList sort() method sorts the list according to the order induced by the specified Comparator instance. All elements in the list must must be mutually comparable. 1. ArrayList sort() method The sort() method accepts an instance of Comparator implementing class which must be able to compare the elements contained in the arraylist. Internally, the sort() […]
ArrayList replaceAll() retains only the elements in this list that are contained in the specified method argument collection. Rest all elements are removed from the list. This method is exact opposite to removeAll() method. 1. ArrayList replaceAll() method The replaceAll() method takes single argument of type UnaryOperator. The UnaryOperator interface is a functional interface that […]
ArrayList retainAll() retains only the elements in this list that are contained in the specified method argument collection. Rest all elements are removed from the list. This method is exact opposite to removeAll() method. 1. ArrayList retainAll() method Internally, the retainAll() method iterate over all elements of arraylist. For each element, it pass element to […]
ArrayList removeAll() removes all of matching elements that are contained in the specified method argument collection. It removes all occurrences of matching elements, not only first occurrence.
ArrayList remove() removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, list remain unchanged.
ArrayList listIterator() returns a list iterator over the elements in this list. It is a bi-directional iterator which is fail-fast in nature. By default, elements returned by the list iterator are in proper sequence. 1. ArrayList listIterator() method The listIterator() method is overloaded and comes in two variants: ListIterator listIterator() – Returns a list iterator […]
ArrayList forEach() method iterate the list and performs the argument action for each element of the list until all elements have been processed.
ArrayList clone() method is used to create a shallow copy of the list. In the new list, only object references are copied. If we change the object state inside first arraylist, then changed object state will be reflected in cloned arraylist as well. 1. ArrayList clone() method clone() method create a new arraylist and then […]
ArrayList clear() method is used to removes all of the elements from the list. The list will be empty after this call returns.
ArrayList addAll() method is used to append all of the elements of argument collection to the list at the end. The order of appended elements is that they are returned by the argument collection’s Iterator.
ArrayList add() method is used to add an element in the list. We can add elements of any type in arraylist, but make program behave in more predicatable manner, we should add elements of one certain type only in any goven list instance. Use generics for compile time type safety while adding the element to […]
Java LinkedList vs ArrayList. Both are different in many aspects and we need to understand both to make a wise decision when to use which class.
ArrayList and Vector, both implements java.util.List interface and provide capability to store and get objects within using simple API methods. Still they are different in many aspects and we need to understand both classes in detail to make a wise decision when to use which class. 1. ArrayList vs Vector – Thread safety Vector is […]
Learn to convert hashset to arraylist in Java using arraylist constructor. Also learn to convert arraylist to hashset back from arraylist.
Learn different and useful ways to convert array to arraylist in Java. In this example, we will use Java 8 classes and Google guava library for creating an arraylist from elements of given array.
Learn different ways to convert ArrayList to string array in Java with examples. 1. Convert arraylist to array – List.toArray() This is most basic way to convert an arraylist containing string values to a string array. Program output. 2. Convert arraylist to string array – Java 8 stream If you are using Java 8 or […]
Learn to convert LinkedList to ArrayList in Java with example. We will also learn to convert arraylist to linkedlist in Java.