java 8 using lambda expression and stream api | Java 8 Programs using Lambda Expression and Stream
Categories: Java 8(JDK1.8) Java Java Examples
Java 8 Programs using Lambda Expression and Stream
- check two strings are anagrams or not
- Given a list of strings, join the strings with ‘[‘ as prefix, ‘]’ as suffix and ‘,’ as delimiter
- Get three maximum numbers and three minimum numbers from the given list of integers.
- Find common elements between two arrays.
- Find the day of the week.
- Find maximum and minimum of given Integer arrays
- Find second largest number in an integer array
- Find sum and average of all elements
- Find sum of all digits of a number in Java 8
- merge two unsorted arrays into single array using Java 8 stream
- merge two unsorted arrays into single sorted array using Java 8 streams
- merge two unsorted arrays into single sorted array without duplicates
- print the numbers which are multiples of 5
- Reverse a string in Java
- Reverse each word of a string using Java 8
- Given a list of strings, sort them according to increasing order of their length
- Write program to sort the given list of decimals in reverse order
Java 8 Interview Sample Coding Questions | Java 8 Sample Coding Practice | Java 8 Interview Sample Coding .
Java 8 Programming 1:- check two strings are anagrams or not using Java8 Lambda Expression and Stream
package r4r.co.in;
import java.util.stream.Collectors; import java.util.stream.Stream;
public class CheckTwoStringsAreAnagramsOrNot {
public static void main(String[] args) { // check two strings are anagrams or not String input1 = "rajesh"; String input2 = "rajesh"; String s1 = Stream.of(input1.split("")).map(f -> f.toLowerCase()).sorted().collect(Collectors.joining()); String s2 = Stream.of(input2.split("")).map(f -> f.toLowerCase()).sorted().collect(Collectors.joining()); if (s1.equals(s2)) System.err.println(true); else System.err.println(false); } } |
Java 8 Programming 2:- Given a list of strings, join the strings with ‘[‘ as prefix, ‘]’ as suffix and ‘,’ as delimiter using Java8 Lambda Expression and Stream
package r4r.co.in;
import java.util.stream.Collectors; import java.util.stream.Stream;
public class ExampleCollectorsJoining {
public static void main(String[] args) { //Given a list of strings, join the strings with ‘[‘ as prefix, ‘]’ as suffix and ‘,’ as delimiter
String[] input= {"Rajesh","Kumar","R4R"}; String joining=Stream.of(input).collect(Collectors.joining(",","[", "]")); System.out.print(joining);
} }
|
Java 8 Programming 3:- Get three maximum numbers and three minimum numbers from the given list of integers
package r4r.co.in;
import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors;
public class Find3MaxAndMin {
public static void main(String[] args) { //get three maximum numbers and three minimum numbers from the given list of integers List<Integer> input=Arrays.asList(1,23,43,334,422,5333,352,32,53,53,22235,355,25,53,53,22); List<Integer> max=input.stream().sorted(Collections.reverseOrder()).limit(3).toList(); System.out.println(max); List<Integer> min=input.stream().sorted().limit(3).toList(); System.out.println(min); } } |
Java 8 Programming 4:-Find common elements between two arrays
package r4r.co.in;
import java.util.Arrays; import java.util.stream.Stream;
public class FindCommonElementsBetweenTwoArrays {
public static void main(String[] args) { //Find common elements between two arrays Integer[] input1= {1,2,3,4}; Integer[] input2= {1,2,4}; Integer[] common=Stream.of(input1).filter(Arrays.asList(input2)::contains).toArray(Integer[]::new); System.out.println(Arrays.toString(common));
} } |
Java 8 Programming 5:- Find the day of the week
package r4r.co.in;
import java.time.DayOfWeek; import java.time.LocalDate;
public class FindDayOfWeek {
public static void main(String[] args) { //Find day of week. DayOfWeek dayOfWeek=LocalDate.now().getDayOfWeek(); System.out.println("Today is:-"+dayOfWeek);
} }
|
Java 8 Programming 6:- Find maximum and minimum of given Integer arrays.
package r4r.co.in;
import java.util.Comparator; import java.util.stream.Stream;
public class FindMaxAndMin {
public static void main(String[] args) { // Find maximum and minimum of given Integer arrays Integer[] integers= {10,202,2220202,30}; int min=Stream.of(integers).min(Comparator.naturalOrder()).get(); System.out.println("Min:-"+min); int max=Stream.of(integers).max(Comparator.naturalOrder()).get(); System.out.println("Max:-"+max); } }
|
Java 8 Programming 7:- Find second largest number in an integer array
package r4r.co.in;
import java.util.Collections; import java.util.Comparator; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.Stream;
public class FindSecondLargestNumberInAnIntegerArray {
public static void main(String[] args) { // Find second largest number in an integer array Integer[] input= {1,2,323,223,233,2213,213,231,321,222222,222223,2113,22,13}; int secondLaegest=Stream.of(input).sorted().skip(input.length-2).findFirst().get(); System.out.println(secondLaegest);
int secondLaegest2=Stream.of(input).sorted(Comparator.reverseOrder()).skip(1).findFirst().get(); System.out.println(secondLaegest2);
} } |
Java 8 Programming 8:-Find sum and average of all elements
package r4r.co.in;
import java.util.Arrays; import java.util.stream.Stream;
public class FindSumAndAverageOfAllElements {
public static void main(String[] args) { //Find sum and average of all elements int[] integers= {12,223,434,53,523,5323,2222,313}; double average=Arrays.stream(integers).average().getAsDouble(); System.out.println("Average:"+average); int sum=Arrays.stream(integers).sum(); System.out.println("Sum:-"+sum); } } |
Java 8 Programming 9:- Find sum of all digits of a number in Java 8
package r4r.co.in;
import java.util.stream.Collectors; import java.util.stream.Stream;
public class FindSumOfAllDigitsOfANumber {
public static void main(String[] args) { //Find sum of all digits of a number in Java 8 int number=123456789; int sum=Stream.of(String.valueOf(number).split("")).collect(Collectors.summingInt(Integer::valueOf)); System.out.println(sum); } }
|
Java 8 Programming 10 :- merge two unsorted arrays into single array using Java 8 stream
package r4r.co.in;
import java.util.Arrays; import java.util.stream.Stream;
public class Merge2UnsortedArraysIntoSingleArray {
public static void main(String[] args) { // merge two unsorted arrays into single array using Java 8 streams Integer[] input= {12,23,34,56,78,90,71,19}; Integer[] input2= {100,1001}; Integer[] output=Stream.concat(Stream.of(input),Stream.of(input2)).toArray(Integer[]::new); System.out.print(Arrays.toString(output));
} }
|
Java 8 Programming 11:- merge two unsorted arrays into single sorted array using Java 8 streams
package r4r.co.in;
import java.util.Arrays; import java.util.stream.Stream;
public class Merge2UnsortedArraysIntoSingleSortedArray {
public static void main(String[] args) { // merge two unsorted arrays into single sorted array using Java 8 streams
String[] str={"Rajesh","Verma","Patel"}; String[] str2= {"Kumar","R4r","Indian"}; String[] ouput=Stream.concat(Stream.of(str), Stream.of(str2)).sorted().toArray(String[]::new); System.out.println(Arrays.toString(ouput));
} }
|
Java 8 Programming 12:- merge two unsorted arrays into single sorted array without duplicates
package r4r.co.in;
import java.util.Arrays; import java.util.stream.Stream;
public class Merge2UnsortedArraysIntoSingleSortedArrayWithoutDuplicates { //merge two unsorted arrays into single sorted array without duplicates public static void main(String[] str) { Integer[] integers= {1212,1,24,6,10,220}; Integer[] integers2= {1,220,2200,1212,6}; Integer[] ouIntegers=Stream.concat(Stream.of(integers),Stream.of(integers2)).sorted().distinct().toArray(Integer[]::new); System.out.println(Arrays.toString(ouIntegers)); } } |
Java Programming 13:- print the numbers which are multiples of 5
package r4r.co.in;
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;
public class PrintTheNumbersWhichAreMultiplesOf5 {
public static void main(String[] args) { //print the numbers which are multiples of 5
Integer[] input= {12,10,15,20}; List<Integer> list=Stream.of(input).filter(f->f%5==0).collect(Collectors.toList()); System.out.println(list); } }
|
Java 8 Programming 14:- Reverse a string in Java
package r4r.co.in; public class ReverseAStringInJava { public static void main(String[] args) { // Reverse a string in Java String str="I am Rajesh Kumar"; //String[] ch=str.split(""); StringBuilder builder=new StringBuilder(); for(int i=str.length()-1;i>=0;i--) { builder.append(str.charAt(i)); } System.out.print(builder.toString()); } }
|
Java 8 Programming 15:-Reverse each word of a string using Java 8
package r4r.co.in;
import java.util.Arrays; import java.util.stream.Collectors;
public class ReverseEachWordofAStringUsingJava8 {
public static void main(String[] args) { //Reverse each word of a string using Java 8. String str = "I am Rajesh Kumar"; String output = Arrays.stream(str.split(" ")).map(word -> new StringBuilder(word).reverse()).collect(Collectors.joining(" ")); System.out.println(output); } }
|
Java 8 Programming 16:- Given a list of strings, sort them according to increasing order of their length
package r4r.co.in;
import java.util.Arrays; import java.util.Comparator; import java.util.stream.Stream;
public class SortAccordingToIncreasingOrderOfTheirLength {
public static void main(String[] args) { // Given a list of strings, sort them according to increasing order of their length String[] str= {"C","C++","Java","HTML","CSS","C#"}; String[] outputStrings=Stream.of(str).sorted(Comparator.comparing(String::length)).toArray(String[]::new); System.out.println(Arrays.toString(outputStrings)); } }
|
Java 8 Programming 17:- Write program to sort the given list of decimals in reverse order
package r4r.co.in;
import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;
public class SortTheGivenListOfDecimalsInReverseOrder {
public static void main(String[] args) { //Write program to sort the given list of decimals in reverse order. Double[] input= {56.90,60.30,30.3,10.11,12.19,20.20}; List<Double> output=Stream.of(input).sorted(Comparator.reverseOrder()).collect(Collectors.toList()); System.out.println(output);
} } |