Java 8 Stream - Sort HashMap based on Keys and Values

By default, Java HashMap doesn’t maintain any order. However, if you need to sort the HashMap, we sort the HashMap explicitly based on your requirements. So, in this section, let’s understand how to sort the hashmap according to the keys and values by using Java 8’s Stream API. 1. Sort HashMap by keys in natural order import java.util. *; import ; public class Main { public static void main ( String [] args) { Map < Integer , String > map = new HashMap<>(); map .put( 44 , "Java" ); map .put( 66 , "Ada" ); map .put( 29 , "Go" ); map .put( 98 , "Kotlin" ); map .put( 11 , "C" ); map .put( 125 , "Rust" ); Map < Integer , String > sortedMap = map .entrySet() .stream() .sorted( Map . Entry . comparingByKey ()) .collect( Collectors . toMap ( Map . Entry ::get

Spring RestClient Example

In this section, we'll learn how to use RestClient to perform HTTP requests in a Spring Boot application. Spring Framework 6.1 introduced the RestClient API. As we all know, Spring Framework 6.1 is part of Spring Boot version 3.2. About RestClient RestClient is a synchronous client to perform HTTP requests. It is a higher-order API since it performs HTTP requests by using an HTTP client library like the JDK HttpClient, Apache HttpComponents, and others.  We can use the Spring RestClient interface  to perform HTTP requests  using a fluent API.  Fluent APIs are designed to make the code more readable and therefore easier to use. Wiring objects together using method chaining helps accomplish these readability and usability goals. Creating a RestClient  In this example we are creating RestClient using static method create(String url).  @Configuration public class RestClientConfig { @Value ( "${user.base.url}" ) String baseUrl ; @Bean RestClient restClient () {