Java 8 Example to Convert All Map Keys and Values into List
Use keyset().stream() and call collect() method to convert into List.
Use values().stream() and call collect() method to convert into List.
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Java8MapToListExample { public static void main(String[] args) { Map<Integer, String> numbers = new HashMap<>(); numbers.put(10, "ten"); numbers.put(20, "twenty"); numbers.put(30, "thirty"); numbers.put(40, "forty"); numbers.put(50, "fifty"); //java 8 - convert all keys to map List<Integer> keysList = numbers.keySet().stream().collect(Collectors.toList()); System.out.println("Map keys List :"); for (Integer integer : keysList) { System.out.println(integer); } // java 8 - convert all keys to map List<String> valuesList = numbers.values().stream().collect(Collectors.toList()); System.out.println("Map values list :"); for (String s : valuesList) { System.out.println(s); } System.out.println("removing odd even fruit id's as list : "); List<Integer> evenList = numbers.keySet().stream().filter(id -> id % 2 == 0).collect(Collectors.toList()); evenList.forEach(id -> System.out.println(id)); } }
Output:
Map keys List : 50 20 41 10 30 Map values list : fifty twenty forty one ten thirty removing odd even fruit id's as list : 50 20 10 30
No comments:
Post a Comment