Java Priority Queue (+ Comparator Example)
Java PriorityQueue is an unbounded Queue implementation that processes the items based on priorities. Custom ordering can be enforced with a Comparator.
Java PriorityQueue is an unbounded Queue implementation that processes the items based on priorities. Custom ordering can be enforced with a Comparator.
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.
The Java ListIterator interface is a bidirectional iterator that iterates over the elements in both forward and backward directions.
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 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 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 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 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 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 …
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.
Learn to find, count and remove duplicate elements from an array in Java using Streams, Map and Set from the Collections framework.
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.
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. …
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 …
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.
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.
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 …
Learn to use Spring Boot RestTemplate for sending POST requests with JSON body, and HTTP headers including basic auth details.
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 …
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 …
In Spring Boot 3 REST POST API tutorial, create an HTTP POST REST API, return the API response and add validations for the request body.
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 use client side load balancing using spring cloud ribbon with eureka. Learn to build microservice which use ribbon as client side load balancer.
Learn to use Spring retry module for implementing retry-based remote API invocations that can handle runtime exceptions or network failures.
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, …
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.