How to convert stream to a map in Java

1 Answer

0 votes
import java.util.Map;
import java.util.stream.Stream;
import java.util.stream.Collectors;

public class MyClass 
{
    public static void main(String args[])
    {

        Stream<String[]> stream = Stream.of(new String[][] {
                            {"A", "C++"},
                            {"B", "C"},
                            {"C", "Java"}});
 
        Map<String, String> mp = stream.collect(Collectors.toMap(e -> e[0], e -> e[1]));
 
        System.out.println(mp);
    }
}




/*
run:

{A=C++, B=C, C=Java}

*/

 



answered Mar 23, 2023 by avibootz

Related questions

1 answer 147 views
1 answer 164 views
1 answer 233 views
233 views asked Feb 9, 2019 by avibootz
1 answer 112 views
112 views asked Mar 25, 2023 by avibootz
4 answers 234 views
1 answer 182 views
182 views asked Mar 23, 2023 by avibootz
1 answer 112 views
...