Java 8 Stream Integer Programming Interview Questions and Answers (2024)

In this section we just covering the Integer Operations realted progams of Java 8  Stream api. Like as : sorting the elements, sum of the elements , average of given elements .

1). How do I sort a list without mutating it? or sort the elements without creating new list in stream java 8? or How to Sort a List in Java – Java List Sorting Example.


         import java.util.stream.IntStream;

import java.util.stream.Stream;

import java.util.List;


public class Sorting_Elements_Example{ // sorting elements with same list

public static void main(String[] args) {

List<Integer> listOfIntegers = Arrays.asList(2, 5, 6, 10, 25, 15, 20, 14, 34, 37, 40);

listOfIntegers.stream().sorted().forEach(System.out::println);

} }



2). Sort the elements by creating new list in java 8  stream api | How to sort the elements by creating new list java 8 stream ? 

   

        import java.util.stream.IntStream;

import java.util.stream.Stream;

import java.util.List;


public class Sorting_Elements_Example{ // sorting the elements with new list

public static void main(String[] args) {

List<Integer> listOfIntegers = Arrays.asList(2, 5, 6, 10, 25, 15, 20, 14, 34, 37, 40);

List<Integer> sortedInteger = listOfIntegers.stream().sorted().collect(Collectors.toList());

System.out.println(“Sorted elements are: “ + sortedInteger);

}

}

4). Sort Decimal elements form Reverse Order in java 8 . or How to Sort Decimal elements form Reverse Order in java 8 stream ?


       import java.util.stream.IntStream;

import java.util.stream.Stream;

import java.util.List;


public class Reverse_Order_Example{

public static void main(String[] args) {

List<Integer> listOfIntegers = Arrays.asList(2, 5, 6, 10, 25, 15, 20, 14, 34, 37, 40);

List<Double> list = Arrays.asList(12.45, 23.58, 17.13, 42.89, 33.78, 71.85, 56.98, 21.12);

list.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);

}

}

5). Find out the sum of elements in java 8 ? or How to find sum of elements in list in Java 8? or How to sum a list of integers with java streams? or How do you sum elements in Java? or How to add values in list in Java 8? or How to add two numbers in Java 8?  or How to sum double values in Java 8? or How do you sum all elements?


      import java.util.stream.IntStream;

import java.util.List;

import java.util.stream.Stream;


public class Sum_of_Numbers_Example{

public static void main(String[] args) {

List<Integer> listOfIntegers = Arrays.asList(2, 5, 6, 10, 25, 15, 20, 14, 34, 37, 40);

int sum = listOfIntegers.stream().mapToInt(Integer::intValue).sum();

// int sum1 = listOfIntegers.stream().reduce(0, (a, b) -> a + b);

System.out.println(“Sum of the elements” + sum);

}

}

6). Find out the Average of all elements ? How to find out the Average of all elements ? please find out the Average of elements .


        import java.util.stream.IntStream;

import java.util.List;

import java.util.stream.Stream;


public class Average_of_Numbers_Example{

public static void main(String[] args) {

int[] a = new int[] { 45, 12, 56, 15, 24, 75, 31, 89 };

double average = Arrays.stream(a).average().getAsDouble();

System.out.println(“Average of all numbers: “ + average);

}

}


7). How to find out the Even number in java 8 ? or  how to find out the Even number in java 8 stream ? how to find out the Even number in java 8 stream or How to print the even numbers in Java 8?  How do you find the even number in Java? How do you check if a value is even in Java? or Java 8 program to print odd numbers from a List or find out all the even numbers that exist in the list using Stream or How to find out the Even number in java 8                         (Most Imp Question)

 

        import java.util.stream.IntStream;

import java.util.List;

import java.util.stream.Stream;


public class Even_Number_Example{

public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(2, 3, 4, 6, 5, 9, 8);

List<Integer> even = numbers.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());

System.out.println(“Even numbers are: “ + even);

}

}

 

8). How to find out the Odd number in java 8 ? or How to find out the Odd number in java 8 


        import java.util.stream.IntStream;

import java.util.List;

import java.util.stream.Stream;


public class Odd_Number_Example{

public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(2, 3, 4, 6, 5, 9, 8);

List<Integer> odd = numbers.stream().filter(n -> n % 2 != 0).collect(Collectors.toList());

System.out.println(“Odd Numbers are: “ + odd);

}

}

   

9) Find duplicate elements in a Stream ? or Finding All Duplicates in a List in Java or How to find duplicates in array in Java 8? or How do you find duplicate elements in an array? or How to find duplicate elements in a Stream in Java or Java: Finding Duplicate Elements in a Stream or How to find duplicate elements in a Stream in Java .


        import java.util.stream.IntStream;

import java.util.List;

import java.util.stream.Stream;


public class Duplicate_Number_Example{

public static void main(String[] args) {

List<Integer> listOfIntegers1 = Arrays.asList(111, 222, 333, 111, 555, 333, 777, 222);

Set<Integer> uniqueElements = new HashSet<>();

Set<Integer> duplicateElements = listOfIntegers.stream().filter(i -> !uniqueElements.add(i))

.collect(Collectors.toSet());

System.out.println(duplicateElements);

}

}


10). Check if the First Letter of a String Is a Number . or How do you check if a string starts with a specific number? or How to check if a string contains numbers in Java 8? or java – How to check a string starts with numeric number? or Find out all the numbers starting with 1 using java8 Stream

   

       import java.util.stream.IntStream;

import java.util.List;

import java.util.stream.Stream;


public class String_startwith_Integer_Example{

public static void main(String[] args) {

List<String> listOfStrings = Arrays.asList(“One”, “2wo”, “3hree”, “Four”, “5ive”, “Six”);

listOfStrings.stream().filter(str -> Character.isDigit(str.charAt(0))).forEach(System.out::println);

}

}


12). How to Merge Two Arrays in Java ? or Merge Arrays into a New Object Array in Java or How to merge two arrays in Java 8? or 

How do we merge 2 arrays into single array?


        import java.util.stream.IntStream;

import java.util.List;

import java.util.stream.Stream;


public class Marge_Two_Array_Example{

public static void main(String[] args) {

int a1[] = { 2, 6, 1, 3 };

int b[] = { 7, 5, 9, 11 };


int c[] = IntStream.concat(Arrays.stream(a1), Arrays.stream(b)).sorted().toArray();

System.out.println(Arrays.toString(c));

}

}


13). Find out the to get 3 Maximum or Minimum numbers in java 8 Stream ? How to find the 3 Maximum or Minimum numbers in java 8 Stream ?


        import java.util.stream.IntStream;

import java.util.List;

import java.util.stream.Collectors;

import java.util.Comparator;

import java.util.stream.Stream;


public class 3_MaimmumAndMinimum_Number_Example{

public static void main(String[] args) {

// get 3 minimum Numbers

listOfIntegers.stream().sorted().limit(3).forEach(System.out::println);

// 3 Maximum Numbers

List<Integer> maxi = listOfIntegers.stream().sorted(Comparator.reverseOrder()).limit(3)

.collect(Collectors.toList());

System.out.println(“3 Maximum Numbers are: “ + maxi);

}

}