Комментарии:
Like these questions are asking for interview of java developer with 2years experience sir? Is it for Sure sir?? Im preparing now thats why asked !
ОтветитьThanks a lot
Ответитьthank u so much brother
ОтветитьHi @CloudTech
One more way
import java.util.*;
import java.util.stream.*;
public class MyClass {
public static void main(String args[]) {
List<Integer> intList = Arrays.asList(10,6,2,6,4,8);
int totalSum = intList.stream().mapToInt(Integer::intValue).sum();
int towDivisibleSum = intList.stream().filter(p -> p % 2 == 0).reduce(Integer::sum).orElse(0);
if(totalSum == towDivisibleSum)
System.out.println("All the numbers are divisible by 2");
else
System.out.println("All the numbers are NOT divisible by 2");
}
}
Hi @CloudTech,
The other way is given below
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MyClass {
public static void main(String args[]) {
List<Integer> intList = Arrays.asList(2,2,8,6,4,10,890,90004,890000063);
long divTwoList = intList.stream().filter(p-> p % 2 == 0).count();
if(divTwoList == intList.size())
System.out.println("All numbers in the given list are divisible by 2");
else
System.out.println("All numbers in the given list are NOT divisible by 2");
}
}
without using streams how to do?
Ответитьcan u please also provide interview questions?
Ответитьare these recent interviews?
ОтветитьGreat Thanks.
ОтветитьThat helps a lot.
Thanks a zillion.
The other way to get the desired output is
import java.util.*;
import java.util.stream.*;
import java.util.function.*;
public class MyClass {
public static void main(String args[]) {
List<Integer> intList = Arrays.asList(2,4,6,8);
Long totalNum = intList.stream().collect(
Collectors.filtering(x-> x % 2 == 0,
Collectors.counting()
)
);
if(totalNum == intList.size())
System.out.println("All the numbers are divisible by 2");
else
System.out.println("All the numbers are not divisible by 2");
}
}