Posts

Showing posts with the label Java 8

10 Common Java Interview Questions Related to the Stream API

Java interview questions related to streams are often focused on practical usage of the Stream API introduced in Java 8. Some of the most common practical questions include: 1. Filtering a List Problem: Given a list of integers, filter out the even numbers and return a list of odd numbers. Solution: import java.util.Arrays ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < Integer > numbers = Arrays . asList ( 1 , 2 , 3 , 4 , 5 , 6 ); List < Integer > oddNumbers = numbers .stream() .filter(n -> n % 2 != 0 ) .collect( Collectors . toList ()); } } 2. Mapping a List Problem:  Given a list of strings, convert all strings to uppercase. Solution: import java.util.Arrays ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > words =...

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