Java Spliterator (with Examples)

Unlike traditional iterators, Spliterator is designed with parallelism in mind and mainly helps in parallel processing when the collection or stream has a large number of elements.

Java ListIterator (with Examples)

The Java ListIterator interface is a bidirectional iterator that iterates over the elements in both forward and backward directions.

Guide to Java Iterator

Java Iterator interface is used to iterate over the elements of a Collection (List, Set or Map). The Iterator helps in retrieving the elements from the specified collection one by one and optionally performs operations over each element. Java Iterator was first introduced in Java 1.2 as a replacement of Enumerations. …

Java Comparator Interface

Java Comparator interface used to sort a array or list of objects based on custom order. Custom ordering of elements is imposed by implementing Comparator.compare() method in the objects.

Java Comparable Interface

Java Comparable interface is part of Collection Framework. Learn the purpose of Comparable interface and use it in different scenarios. 1. Comparable Interface 1.1. Why Implement Comparable? In Java, if we want to sort a List of elements then we can Collections.sort() method. It sorts the list items according to …

Java LinkedList class

Java LinkedList class is doubly-linked list implementation of the List and Deque interfaces. It implements all optional list operations, and permits all elements (including null). Table of Contents 1. LinkedList Hierarchy 2. LinkedList Features 3. LinkedList Constructors 4. LinkedList Methods 5. LinkedList Example 6. LinkedList Usecases 7. LinkedList Performance 8. …

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 LinkedHashSet class

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

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

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

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 …

Java LinkedHashMap class

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 …

Gson – Serialize and Deserialize a Map

Learn to serialize HashMap using Google Gson library. Also, learn to deserialize JSON string to HashMap containing custom Objects using Gson such that field values are copied into appropriate generic types. These conversions can be used to create deep cloning of the HashMap. 1. Maven Include the latest version of …

Clone a HashMap – Shallow and Deep Copy

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() …

How to Join or Merge Two Maps in Java

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 …

How to Compare Two Maps in Java

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.

Basic Auth with Spring RestTemplate

Learn to add basic authentication to http requests invoked by Spring RestTemplate while accessing rest apis over the network. 1. Maven dependencies To work with Spring RestTemplate and HttpClient API, we must include spring-boot-starter-web and httpclient dependencies in pom.xml file. In this RestTemplate basic authentication tutorial, we are using dependencies. …

Securing Spring Boot REST API with Basic Auth

Learn to use basic authentication to secure the REST APIs created in a Spring boot application. The secured API will ask for user authentication credentials before giving access to the API response. 1. Maven Dependency The simplest way to add all required jars is to add the latest version of …

Java ArrayList spliterator() Example

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 replaceAll()

ArrayList replaceAll() transform each element in the list by applying a lambda expression or UnaryOperator implementation.

Java List retainAll()

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 …

Java ArrayList.listIterator()

Java ArrayList.listIterator() returns a bi-directional list iterator that iterates over the elements of the current list.

Java ArrayList forEach()

ArrayList forEach() method iterate the list and performs the argument action for each element of the list until all elements have been processed.

Java Tuple (with Examples)

A tuple is an immutable wrapper object that holds different pieces of information. Learn to create tuples using a custom Java class and Javatuple library.

Configure Timeouts with Spring RestTemplate

In this Spring boot2 RestTemplate timeout example, learn to configure connection timeout and read timeout in Spring RestTemplate with example. 1. Default Timeout By default, RestTemplate uses SimpleClientHttpRequestFactory which depends on the default configuration of HttpURLConnection. Look inside the class source, and you will find this. By default, RestTemplate uses …

Spring Boot RestTemplate GET Example

In this, Spring Boot RestTemplate GET request example, learn to use RestTemplate to invoke HTTP GET API and verify the response status code and the response entity body. To create the rest APIs, use the sourcecode provided in spring boot rest api example. 1. Setup Start with including the latest …

Testing POST APIs using Spring Boot TestRestTemplate

Learn to consume HTTP POST REST API with Spring TestRestTemplate. In this post request test example, we will be sending request body along with request headers. 1. Maven dependencies Make sure, you have spring-boot-starter-test dependency in your project to get access to TestRestTemplate class in runtime. If you are using …

Java ArrayList add() – Add a Single Element to List

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 Vector vs ArrayList

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. …

Spring Boot Retry Example

Learn to use Spring retry module for implementing retry-based remote API invocations that can handle runtime exceptions or network failures.

Java ArrayList.toArray()

Learn to convert ArrayList to an array using toArray() method. The toArray() returns an array containing all of the elements in the list.

Java ArrayList.ensureCapacity()

Learn to use ArrayList ensureCapacity() method to increase the capacity of already initialized arraylist to a desired size. It improve performance of list.

Replace an Existing Item in ArrayList

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, …

About Us

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.