import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Java8Questions {
public static void main(String[] args) {
List<Integer> listOfIntegers = Arrays.asList(2,5,3,1,6,9);
Map<Boolean, List<Integer>> oddEvenNumbersMap =
listOfIntegers.stream().collect(Collectors.partitioningBy(i -> i % 2 == 0));
System.out.println(oddEvenNumbersMap);
}
}
{false=[5, 3, 1, 9], true=[2, 6]}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Java8Questions {
public static void main(String[] args) {
List<String> listOfStrings = Arrays.asList("Code","Junction","Code","Innovation","Junction");
List<String> uniqueStrngs = listOfStrings.stream().distinct().collect(Collectors.toList());
System.out.println(uniqueStrngs);
}
}
[Code, Junction, Innovation]
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Java8Questions {
public static void main(String[] args) {
List<Integer> listOfInt = Arrays.asList(2,5,3,2,1,6,5,9,4);
List<Integer> uniqueVal = listOfInt.stream().distinct().collect(Collectors.toList());
System.out.println(uniqueVal);
}
}
[2, 5, 3, 1, 6, 9, 4]
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Java8Questions {
public static void main(String[] args) {
List<Integer> listOfInt = Arrays.asList(2,5,3,2,6,5,4);
Map<Integer,Long> freqOfMap= listOfInt.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(freqOfMap);
}
}
{2=2, 3=1, 4=1, 5=2, 6=1}
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Java8Questions {
public static void main(String[] args) {
List<String> stationeryList = Arrays.asList("Pen", "Eraser", "Pen", "Pencil", "Stapler", "Pencil");
Map<String, Long> stationeryCountMap = stationeryList.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(stationeryCountMap);
}
}
Output
{Pen=2, Stapler=1, Pencil=2, Eraser=1}
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Java8Questions {
public static void main(String[] args) {
String inputString="java";
Map stationeryCountMap = inputString.chars().mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(stationeryCountMap);
}
}
{a=2, v=1, j=1}
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class Java8Questions {
public static void main(String[] args) {
List<Integer> listOfIntegers = Arrays.asList(2, 5, 3, 1, 6, 9);
List<Integer> sortedList = listOfIntegers.stream().sorted().collect(Collectors.toList());
List<Integer> reverseSortedList = listOfIntegers.stream().sorted(Collections.reverseOrder())
.collect(Collectors.toList());
System.out.println("Ascending Order:" + sortedList);
System.out.println("Decending Order:" + reverseSortedList);
}
Ascending Order:[1, 2, 3, 5, 6, 9]
Decending Order:[9, 6, 5, 3, 2, 1]
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class Java8Questions {
public static void main(String[] args) {
List<String> listOfString = Arrays.asList("D","F","A","C","B");
List<String> sortedList = listOfString.stream().sorted().collect(Collectors.toList());
List<String> reverseSortedList = listOfString.stream().sorted(Collections.reverseOrder())
.collect(Collectors.toList());
System.out.println("Ascending Order:" + sortedList);
System.out.println("Decending Order:" + reverseSortedList);
}
}
Output
Ascending Order:[A, B, C, D, F]
Decending Order:[F, D, C, B, A]
Copyright © 2020 Inovatik - All rights reserved