Java TreeSet class
Java TreeSet class extends AbstractSet and implements NavigableSet interface. It is very similar to HashSet class, except it stores the element in sorted order.
Java collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:
Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
Implementations, i.e., Classes: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface.
In addition to collections, the framework defines several map interfaces and classes. Maps store key/value pairs. Although maps are not collections in the proper use of the term, but they are fully integrated with collections.
Java TreeSet class extends AbstractSet and implements NavigableSet interface. It is very similar to HashSet class, except it stores the element in sorted order.
Java LinkedHashSet class extends HashSet and implements Set interface. It is very very similar to HashSet class, except if offers the predictable iteration order.
Java HashSet class implements the Set interface, backed by a hash table(actually a HashMap instance). If does not offer any guarantees as to the iteration order, and allows null element.
Java Hashtable class is an implementation of hash table data structure. It is very much similar to HashMap but it is synchronized while HashMap is not.
Java TreeMap class stores key-value pairs very similar to the HashMap class. The difference is that TreeMap provides an efficient way to store key/value pairs in sorted order. The TreeMap class is a red-black tree-based NavigableMap implementation. This Java TreeMap tutorial will teach us about TreeMap class, methods, usecases, and …
LinkedHashMap in Java is used to store key-value pairs very similar to HashMap class. Difference is that LinkedHashMap maintains the order of elements inserted into it while HashMap is unordered. In this Java collection tutorial, we will learn about LinkedHashMap class, it’s methods, usecases and other important details. Table of …
Learn to create clone of a HashMap in Java. We will see the java programs to create shallow copy and deep copy of a HashMap. 1. Creating a Shallow Copy of Map We can create a shallow copy of a given HashMap in two ways. The first uses the clone() …
Learn to remove duplicate elements from an ArrayList using different techniques such as HashSet, LinkedHashSet, and using Java 8 stream.
Java HashMap is not synchronized by default. Learn to use synchronized maps using Collections.synchronizedMap() and ConcurrentHashMap.
Learn merging two hashmaps in both cases – ignoring duplicate keys (overwrites the value) or handling duplicate keys. 1. Merge Two HashMaps Ignoring Duplicate Keys This one is a simple solution. Use firstMap.putAll(secondMap) method that copies all of the mappings from the secondMap to firstMap. As we know hashmap does …
Learn to compare two Java Maps for the equality of their entries, key and values, and find the Map differences using Guava’s MapDifference API.
Since Java 8, Spliterators are a special type of iterator that supports parallel iteration of portions of the source such as list, set, or array.
Java ArrayList.removeIf() removes all elements that satisfy a condition by iterating through the elements and matching against the specified Predicate.
ArrayList replaceAll() transform each element in the list by applying a lambda expression or UnaryOperator implementation.
In Java, the ArrayList.retainAll() retains only those elements in this list that are contained in the specified collection. Rest all elements are removed from the list. This method is exactly the opposite to removeAll() method. 1. Syntax The syntax to use the retainAll() method is: Method Argument – a collection …
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, else the list remains unchanged.
Java ArrayList.listIterator() returns a bi-directional list iterator that iterates over the elements of the current list.
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() creates a shallow copy of the given ArrayList. Learn to create deep copy and shallow copy of an arraylist with examples.
Java ArrayList.addAll() appends all of the elements of argument collection to the list at the end or the specified index position.
The ArrayList.add() in Java adds a single element to the list, either at the end of the list or at the specified index position. Always use generics for compile-time type safety while adding the element to the arraylist. 1. ArrayList.add() Method The add() method first ensures that there is sufficient …
Java LinkedList and ArrayList are different in many aspects, and we need to understand both to decide when to use which class.
In Java, ArrayList and Vector, both implement java.util.List interface and provide the 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 about when to use which class. …
Learn to convert a Set to List in Java using constructor, addAll() and Java 8 streams. Learn to convert list to set to remove duplicates.
Learn different and useful ways to convert array to list in Java. Learn to use Java 9 List, Java 8 Stream, Collections and Guava library.
Learn to convert LinkedList to ArrayList in Java with example. We will also learn to convert arraylist to linkedlist in Java.
Learn to convert ArrayList to an array using toArray() method. The toArray() returns an array containing all of the elements in the list.
ArrayList contains() method is used to check if the specified element exists in the given arraylist or not. If element exist then method returns true.
Learn to use ArrayList ensureCapacity() method to increase the capacity of already initialized arraylist to a desired size. It improve performance of list.
Learn to update or replace an existing element in ArrayList with a new specified element or value, using set (int index, Object newItem) method. 1. Replacing an Existing Item To replace an existing item, we must find the item’s exact position (index) in the ArrayList. Once we have the index, …
Learn to check if an ArrayList is empty using isEmpty() and size() methods. Note isEmpty() method internally checks the size of the list.
Learn to clear arraylist or empty an arraylist in Java. Clearing a list means to remove all elements from the list. Difference between clear and removeAll.
Learn how to merge two arraylists into a combined single arraylist in Java. Also learn to join arraylists without duplicates in the combined list.
Learn to serialize and/or deserialize an ArrayList in Java with easy-to-follow examples. Note that the list items must also be Serializable.
Learn to swap two specified elements in ArrayList in Java. We will use Collections.swap() method to swap two elements within a specified arraylist at specified indices. 1. Collections.swap() API The Collections.swap() method swaps the elements at the specified positions in the specified list. The index arguments must be a valid …
Learn to synchronize an ArrayList using either Collections.synchronizedList() or CopyOnWriteArrayList class with examples.
Learn to compare two arraylists in Java with List Items. Learn to test whether two arraylists are equal and then find different list items.
Learn how to get the element from an ArrayList. We will be using ArrayList.get() method to get the object at the specified index of the arraylist.
Learn to get the index of the first occurrence of an element in an arraylist in Java using ArrayList.indexOf() method with a simple example.
Learn to get the index of last occurrence of an element in the arraylist in Java using Arraylist.lastIndexOf() method with a simple example.
Learn to get a sublist from an existing ArrayList using ArrayList.subList() and modify the original list using the sublist view.
The Java ArrayList class is part of the Collection framework and is an implementation of a resizable array data structure. It automatically grows and shrinks when elements are added or removed in the runtime, whenever required, This Java tutorial discussed the different ways to add multiple items to an ArrayList …
Learn to remove element from ArrayList. Remove element at specifed index, or element value. Remove all elements from arraylist for spcified value example.
Java programs to add single or multiple elements at the specified index of arraylist with ArrayList.add() and addAll() methods.
The Java iterate through ArrayList programs. Learn how to retrieve values from ArrayList in Java using for loop, while loop, iterator and stream api.
Java ArrayList can be initialized in one line using List.of(), and new ArrayList constructors. Learn to add elements one by one and in bulk.
Learn to create a Java List with only one element in it using Arrays.asList() and Collections.singletonList() methods.
Learn different techniques to iterate over a Java Collection (List, Set and Map) using for-loop, enhanced for-loop, Iterator and Java Stream.
Java enumerations are only applicable for legacy classes e.g HashTable, and Vector. Iterator is the recommended way to iterate over collections since Java 1.2 and takes place of Enumeration.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.