Posts

Showing posts with the label Java 8

Java 8 Stream - Sort HashMap based on Keys and Values

Image
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 java.util.stream.Collectors ; 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

How to convert a List of String to a comma separated String in Java

In this section, we will show you how to  convert a List of String to a comma separated String in Java. 1. List 1.1 Using String.join() join (CharSequence delimiter, Iterable<? extends CharSequence> elements) : This method is useful when we have to join iterable elements. Some of the common Iterables are – List, Set, Queue, and Stack. import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "Java" , "Kotlin" , "Python" , "C" ); String result = String . join ( "," , list ); System . out .println( result ); } } Console Output: Java,Kotlin,Python,C 1.2 Stream Collectors.joining() joining(CharSequence delimiter) :We pass a delimiter to get back a Collector, which concatenates the elements separated by the specified delimiter. import java.util.Arrays ; import java.util.List ; import jav

Java 8 Stream – How to sort a List with stream.sorted()

In this section, we will show you how to sort list using stream.sorted() in Java 8. 1. List 1.1 Sort a List in natural order. import java.util.Arrays ; import java.util.Comparator ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "8" , "B" , "A" , "1" , "Z" , "Y" , "2" , "b" , "d" ); List < String > sortedList1 = list .stream() .sorted( Comparator . naturalOrder ()) .collect( Collectors . toList ()); System . out .println( sortedList1 ); List < String > sortedList2 = list .stream() .sorted((o1,o2)-> o1.compareTo(o2)) .collect( Collectors . toList ()); System . out .println( sortedList2 ); List < String > sortedList3 = list .stream(). sorted().

Java - How to capitalize the first letter of each word in a String

In this section, we will show you four different ways to  capitalize the first letter of each word in a String using Java. Following ways can be used to capitalize the first letter of each word in a String in Java : By using Java 8 Stream By using  StringTokenizer By using Pattern.compile() and replaceAll method By using Apache Commons Text Example 1: Using Java 8 Stream First split the string into an array using the split() method. Then array is passed to Arrays.stream() as a parameter that turns it into a Stream object. Then, we use the map() method from streams to capitalize each word and finally collect all words again and join them into one sentence. import java.util.Arrays ; import java.util.stream.Collectors ; public class Main { // Driver code public static void main ( String [] args) { String str = "how are you" ; System . out .println( "Input => " + str ); String result = capitalize ( str ); System . out .println(