How to find length/size of ArrayList in Java? Example

You can use the size() method of java.util.ArrayList to find the length or size of ArrayList in Java. The size() method returns an integer equal to a number of elements present in the array list. It's different than the length of the array which is backing the ArrayList, which is called the capacity of ArrayList. When you create an object of ArrayList in Java without specifying a capacity, it is created with a default capacity which is 10. Since ArrayList is a growable array, it automatically resizes when the size (number of elements in the array list) grows beyond a threshold. 

How to remove all elements of ArrayList in Java - RemoveAll Example

There are two ways to remove all elements of an ArrayList in Java, either by using clear() or by using the removeAll() method. Both methods are defined in the java.util.List and java.util.Collection interface, hence they are available not just to ArrayList but also to Vector or LinkedList, etc. Both elements remove all objects from ArrayList but there is a subtle difference in how they do. The clear() method is straightforward, it traverses through the ArrayList and sets all indices to null, which means the ArrayList becomes empty and all elements become eligible to Garbage collection, provided there are no more references to them.

How to loop over a TreeSet in Java with Example

In our earlier articles, we have learned how to loop over ArrayList in Java and you can use the same logic to loop over a TreeSet. You can use both, enhanced for loop and Iterator to traverse over TreeSet in Java. Though worth noting is that Iterator returned by the iterator() method of TreeSet returns elements in the sorted order which is imposed by the Comparator you have provided to TreeSet at the time of instantiation. By default, if you don't provide any custom Comparator then TreeSet sorts elements in their natural order like String elements are sorted in alphabetical order and Integer elements are sorted in numeric order. When you iterate over a TreeSet the iterator follows this order.

How to Convert Vector to Array in Java? 2 Examples

In the last article, we have learned how to convert a LinkedList to an array in Java, and in today's tutorial, you will learn how to convert a Vector to an array in Java. There is a similarity between Vector and LinkedList, both implement the List interface. Even though Vector is a legacy class and exists in Java from JDK 1.1 long before the Java Collection framework was introduced, it was later retrofitted to implement the List interface. You can use the same toArray() method to convert a Vector to an array in Java. 

How to sort a LinkedList in Java? Example Tutorial

Since LinkedList implements the java.util.List interface, you can sort the LinkedList by using the Collections.sort() method, just like you sort an ArrayList. Since the LinkedList class implements the linked list data structure which doesn't provide random access based upon the index, sorting is quite expensive. In order to access any element, you need to first traverse through that element which is the O(n) operator. This method uses an efficient strategy to handle this scenario. It first copies the contents of LinkedList to an array, sorts the array, and copies it back. 

10 Example of List in Java

Hello guys,  Java, as a versatile and widely used programming language, offers a plethora of data structures to developers. One of them is List which is also fundamental component in Java's collection framework, play a pivotal role in managing and manipulating sequences of elements. In the past, I have shared 10 Examples of HashMap in Java and In this article, we'll delve into Java lists, exploring their features and providing ten illustrative examples to deepen your understanding. List are also a popular topic from Java interviews with questions like difference between ArrayList and LinkedList which have been asked to me almost 10 times in past 20 years. 

How to Convert a List to a Set in Java with Example

How to convert a List to Set in Java
Many times we need to convert one collection to another like converting a List to a Set. It's also an easy way to copy contents from one collection to other in Java, like from List to Set or Set to List. Thanks to the Collection framework in Java, copying collections from one to another is extremely easy. Since every Collection class implements a Collection interface that defines the addAll() method, which can be used to create a collection from contents of another collection.

Difference between ArrayList and HashMap in Java

Difference between ArrayList and HashMap in Java
One of the most critical differences between the HashMap and ArrayList class is that the former is the implementation of the hash table while the latter is a dynamic array that can resize itself. The HashMap and ArrayList are two of the most popular classes from the Java Collection framework. Though both are used to store objects they are completely different in their implementation, working, and usage. The main difference between ArrayList and HashMap is that ArrayList is an index-based data structure backed by an array while HashMap is a map data structure that works on hashing to retrieve stored values. 

How to sort HashSet in Java? Example

Somebody asked me recently, how do you sort a HashSet? For lists, we use the Collections.sort(List) method, but there is nothing for Set. If I have a HashSet then how would I go about sorting it? The answer is you cannot sort a HashSet, why? because HashSet is an unordered collection. When you insert an element in HashSet then you lose the order guarantee. You cannot do reordering or sorting in Set because it does not have random access methods (ie, .get() an element at a given index), which is basically required for sort algorithms.