convert list to map using java8

Ram Somani

I have a arraylist

List<Integer> h = Arrays.asList(1,2,3,4,5,6);

I want to filter even numbers and then store it in a map with key is unique number starting from 1 and value being the filtered value using java8

output ```` 1-2 2-4 3-6 ```

I will be using filter and Collectors.toMap() in collect

Map<Integer,Integer> j = h.stream().filter(fe->fe%2==0).collect(Collectors.toMap(?,?));

what will come in ?

bbahaida

something like that

List<Integer> h = Arrays.asList(1,2,3,4,5,6);
AtomicInteger f = new AtomicInteger(0);
Map<Integer,Integer> j = h.stream()
                    .filter(fe-> fe%2==0) 
                    .collect(Collectors.toMap(fs -> f.incrementAndGet(), fs -> fs));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert list to map using Java8

Group the list of Objects and convert to a map using java8

how to convert list to map by java8?

Java8: How to Convert Map<X, List<Y>> to Map<Y,X> using Stream?

How do I convert a List<Employee> to Map<Employee, List<String>> using java8?

List to Map using Java8

Java8 convert List of Map to List of string

Java8 convert list<P> in to Map<Integer, List<P>>

Convert List to map using java 8

Java8 lambda convert List to Map of Maps

Convert GraphTraversal<Vertex, Map<Object, List>> into Java8 stream

Java8: convert one map to an another using stream

Convert List<List<Object>> to Map<Object,List<Object>> Java8

convert list of object into a map of <String,Map<String,Integer>> java8 streams

Convert List<Map<String, Object>> to Map<String, List<Map<String, Object>>> using Java 8 streams API

Convert list of complex objects to Map using Java 8

Convert List<T> to Map<T, U> using Java 8

Convert List of Strings into Map using Java-8 Streams API

How to convert List to Map with indexes using stream - Java 8?

How to convert List of Map into Map of List using Java Streams

Java Stream - Convert List to Map using toMap

Convert List<MyObject> to List<List<String>> using java8 lambdas only

How to convert List<Object[]> to Map<String,BigInteger> with Streams & Lambda Java8

Java8 - How to convert a nested maps to list of nested maps gathered by value of inner map's key

Java 8 stream: how to Convert Map<String, List<Integer>> to Map<Integer, List<String>> using groupingBy(.)

Map to List after filtering on Map's key using Java8 stream

How to turn List<T> into List<Map<K,V>> using Lambda in JAVA8

How to convert List of Lists to single List using Java8 streams

Java 8 Convert List of List of String to Map