Tuesday, September 24, 2024

coding question in streams


JPMC Interview Questions

List of numbers, find starting with 1

List<Integer> start1List = intList.stream().map(num -> (String.valueOf(num).startsWith("1") == true ? num : null)).filter(n -> n != null).collect(Collectors.toList());

 

find three maximum and three minimum numbers from the list you can use the same list

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

intList.stream().sorted(Comparator.reverseOrder()).limit(3).forEach(System.out::println);


Program to filter even numbers from an array using Java streams

  • Use Java streams to filter even numbers from an array

  • Use filter() method with lambda expression to check if a number is even

  • Arrays.asList(intArr).stream().filter(num -> num%2 == 0).forEach(System.out::println);



  • Collect the filtered numbers into a new array using collect() method

  • Arrays.asList(intArr).stream().filter(num -> num%2 == 0).collect(Collectors.toList());



SUM of list

System.out.println(intList.stream().reduce(0, (x, y)->x+y));


flatMap -> data flattening -> streams of streams will be converted to stream


//find duplicates values

intList.stream().filter(s -> Collections.frequency(intList, s) > 1).forEach(System.out::print);

// use hashset

HashSet<Integer> set1 = new HashSet<Integer>();

intList.stream().filter(s -> !set1.add(s)).forEach(System.out::print);


//find highest nos

System.out.println(intList.stream().sorted(Comparator.reverseOrder()).findFirst().get());


//find second highest nos

System.out.println(intList.stream().sorted(Comparator.reverseOrder()).limit(2).skip(1).findFirst().get());


//Find the intersection of two lists using Java streams:

intList.stream().filter(intList1::contains).forEach(System.out::println);


// Max integer from list

int max = myList.stream()
.max(Integer::compare)
.get();



PROGRAM:


package java8sample;


import java.util.Arrays;

import java.util.Collections;

import java.util.Comparator;

import java.util.HashSet;

import java.util.List;

import java.util.stream.Collectors;


public class streammain {


public static void main(String[] args) {

// Given a list of integers, find out all the even numbers that exist in the list using Stream functions?

List<Integer> intList = Arrays.asList(1,4,6,7,8,9,1,7);

List<Integer> intList1 = Arrays.asList(1,4,6);

List<Integer> evenList = intList.stream().filter(num -> num%2 == 0).collect(Collectors.toList());

// System.out.println("find out all the even numbers - "+evenList);

//multile by 2

// intList.stream().map(a -> a*2).forEach(System.out::print);

//sort

// StringBuilder s =

//filter and sort

List<Integer> l1 = intList.stream().filter(s -> s<=9).sorted().collect(Collectors.toList());

// System.out.println("filter and sort =- "+l1);

//Integer to string list, concat last but 1

List<String> s1 = l1.parallelStream().map(Object::toString).collect(Collectors.toList());

// System.out.println("Integer to string list, concat:");

// s1.stream().map(a -> a == s1.get(s1.size()-1)?a:a.concat(",")).forEach(System.out::print);

//Program to filter even numbers from an array using Java streams

Integer[] intArr = {1,3,12,5,6};

// Arrays.asList(intArr).stream().filter(num -> num%2 == 0).forEach(System.out::println);

Arrays.asList(intArr).stream().filter(num -> num%2 == 0).collect(Collectors.toList());

// List of numbers, find starting with 1

List<Integer> start1List = intList.stream().map(num -> (String.valueOf(num).startsWith("1") == true ? num : null)).filter(n -> n != null).collect(Collectors.toList());

// System.out.println(start1List);

// find three maximum and three minimum numbers from the list you can use the same list

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

// intList.stream().sorted(Comparator.reverseOrder()).limit(3).forEach(System.out::println);

//add all values

// System.out.println(intList.stream().reduce(0, (x, y)->x+y));

//find duplicates values

// intList.stream().filter(s -> Collections.frequency(intList, s) > 1).forEach(System.out::print);

// use has set

HashSet<Integer> set1 = new HashSet<Integer>();

// intList.stream().filter(s -> !set1.add(s)).forEach(System.out::print);

//find second highest nos

// System.out.println(intList.stream().sorted(Comparator.reverseOrder()).limit(2).skip(1).findFirst().get());

// streammain sm = new streammain();

// sm.printPrime();

// Find the intersection of two lists using Java streams:

intList.stream().filter(intList1::contains).forEach(System.out::println);

}

// public boolean isPrime(Integer in) {

// if(in <= 1)

// return false;

// for(int i=2;i<in;i++) {

// if(in%i == 0)

// return false;

// }

// return true;

// }//end isPrime


public boolean isPrime(int number) {

if (number <= 1) {

return false;

}

for (int i = 2; i <= Math.sqrt(number); i++) {

if (number % i == 0) {

return false;

}

}

return true;

}

private void printPrime() {

List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10, 11, 12, 13, 14, 15);

boolean containsPrime = numbers.stream()

.anyMatch(this::isPrime);

System.out.println("List contains a prime number: " + containsPrime);


}


}

No comments:

Post a Comment