Skip to main content

Java 8 Coding Interview Questions | Asked In Many Interviews

 Q 1) Given A List . Write Java 8 Program to find Out 1st Element From List?

=====================================================================


import java.util.Arrays;
import java.util.List;

public class FirstElementOfList {
    // Given a List. Find First Element From the List
 
    public static void main(String[] args) {
       
           List<Integer> list=Arrays.asList(10,15,24,7,24,74,10,7);

           list.stream().findFirst().ifPresent(System.out::println);



    }


}


Q 2) Given A List ? Find Out the Count of All Element In A List?

==============================================================================

import java.util.Arrays;
import java.util.List;

public class TotalNumberOfElements {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(10, 20, 30, 40, 50, 60, 70);

        long count = list.stream().count();
        System.out.println("The Total Number Of elements in list:" + count);

    }

}


Q 3) Use map() method of Stream API for Converting String Object into Uppercase.

===================================================================================



import java.util.stream.Stream;

public class StringObjUppercase {

    // Use map for converting String Object Into UpperCase In Java

    public static void main(String[] args) {

        String str[] = {"aa","bb","cc","dd"};
       
        Stream<String> stringStream=Stream.of(str);

        stringStream.map(String::toUpperCase).forEach(System.out::println);


    }

}


Q 4) Write A Java 8 Program to find out All Character Occurances In A String.

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class StringCharOccurnace {

    public static void main(String[] args) {
       

        String str = "Welcome To Bikkad IT";

        IntStream intStream=new String(str).chars();

        Map<Character, Long> characterFrequnecyMap=intStream.mapToObj(c -> (char)c).map(Character::toLowerCase).
        collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        characterFrequnecyMap.forEach((key, value) -> System.out.println("Character :" + key + " , Occrances :" + value ));

       

   
}
}


Q 5) Write A Java 8 program to sort the elements?


import java.util.Arrays;
import java.util.List;

public class SortIntegers {
   
// Given List Of Intgers. Sort the all values inside the list

   public static void main(String[] args) {

    List<Integer> list=Arrays.asList(10,15,24,7,24,74,10,7);

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

   


   }

}

Q 6) Write A Java Program To Sort A List In Reverse Order.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SortInReverseOrder {

    // Given a List Of Integers. Sort them According to Descending order.

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(10, 24, 17, 42, 6, 13, 20);

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

    }

}


Q 7) Convert An Array Into Stream And Print the All Elements From Stram.

import java.util.Arrays;

public class SortArray {

    // Take an Array and Sort this array and then Convert this array into Stream.

    public static void main(String[] args) {

        int arr[] = { 10, 22, 12, 75, 2, 4, 9 };

        // Sorted the Array Using Parallel sort
        Arrays.parallelSort(arr);

        // Converted into a Stream and then printed Using forEach
        Arrays.stream(arr).forEach(n -> System.out.print(n + " "));

    }

}


Q 8 ) Given A String is There. You want To Find Out First Repeated Character From The String?


import java.util.LinkedHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

public class RepeatedCharacterInString {

    // Given String is there. Find Out the First Repeated Character in the String.

    public static void main(String[] args) {

        String str = "Welcome To Java";

        Character result = str.chars().mapToObj(c -> Character.toLowerCase((Character.valueOf((char) c))))
                .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
                .entrySet()
                .stream()
                .filter(entry -> entry.getValue() > 1L)
                .map(entry -> entry.getKey())
                .findFirst()
                .get();

        System.out.println("The First repeating Character in java:" + result);

    }

}


Q 9) Given A List Of Numbers. Find Out the Numbers Which are Starts With 1.

import java.util.Arrays;
import java.util.List;

public class NumbersStartsWith1 {

    // Given list of numbers. Find Out the Numbers which are starts with 1

    public static void main(String[] args) {
       
         List<Integer> list=Arrays.asList(10,24,17,42,6,13,20);
         // Consider this list. Find out the Numbers which are starts with 1

         list.stream().map(n -> n + "").filter(n -> n.startsWith("1")).forEach(n -> System.out.println(n));


    }
   
}


Q 10) Find Out the Maximum Number From A List?


import java.util.Arrays;
import java.util.List;

public class MaxNumberFromList {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(10, 20, 30, 40, 50, 60, 70);

        Integer max_num = list.stream().max(Integer::compare).get();

        System.out.println("The maximun number from list: " + max_num);

    }
}














































































Comments

Popular posts from this blog

Java Programs Asked In An Interview | Java 8 | Core Java

 Q1) // Write a Java Program To Find Out the Character Occurnaces In A String. import java . util . HashMap ; import java . util . Map ; public class CharOccurnacesInString {     // Write a Java Program To Find Out the Character Occurnaces In A String.     public static void main ( String [] args ) {         String str = "I am Sandeep Aswar" ;         str = str . replaceAll ( " " , "" ). toLowerCase ();         char [] ch = str . toCharArray ();         Map < Character , Integer > charMap = new HashMap <>();         for ( char c : ch ) {             if ( charMap . containsKey ( c )) {                 charMap . put ( c , charMap . get ( c ) + 1 );             } else {                 charMap . pu...

Java 8 Programs Asked In Interview For Experienced Professionals | Java 8 Coding Interview Questions Asked In The Interview.

 Q1) // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different. package J ava C oncept O f D ay ; import java . util . stream . Collectors ; import java . util . stream . Stream ; public class AnagramStrings {     // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different.     public static void main ( String [] args ) {         String str1 = "Listen" ;         String str2 = "Silent" ;         str1 = Stream . of ( str1 . split ( "" )). map ( String :: toLowerCase ). sorted (). collect ( Collectors . joining ());...

How many primitive data types are there in Java?

Java includes a set of fundamental data types known as primitive data types. These data types are the essential components for storing and managing various kinds of data, including numbers, characters, and boolean values. Java encompasses a total of eight primitive data types, and this article aims to provide an in-depth explanation of each type in a simplified manner. 1.      byte: The byte data type is designed for storing small integer values, with a range spanning from -128 to 127. 2.      short: Slightly more accommodating than byte , the short data type can be employed for storing relatively larger integer values, ranging from -32,768 to 32,767. 3.      int: Among the most commonly used primitive data types, int can hold larger integer values, with a range extending to approximately -2 billion to 2 billion. 4.      long: When dealing with very large integer values, the long data t...