mayank bisht
mayank bisht

Reputation: 618

Possible way to write below code in java 8

I have a code, which is working as required, but I want to re-write it in Java 8.


This code will produce a map.Each list item will have all the servers allocated to it.

public static Map<String, List<String>> agg(){
        List<String> list = Arrays.asList("Item A", "Item B", "Item C");
        List<String> servers = Arrays.asList("Server A", "Server B", "Server C", "Server D");
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            ArrayList<String> temp = new ArrayList<>();
            for (int j = 0; j < servers.size(); j++) {
                temp.add(servers.get(j));
            }
            map.put(list.get(i), temp);
        }
        return map;
    }


Output

Item C ::[Server A, Server B, Server C, Server D]
Item B ::[Server A, Server B, Server C, Server D]
Item A ::[Server A, Server B, Server C, Server D]


What would be the lambda equivalent?

Upvotes: 5

Views: 96

Answers (4)

Naman
Naman

Reputation: 31858

A simple forEach approach would be

Map<String, List<String>> output = new HashMap<>();
list.forEach(s -> output.put(s, servers));

Upvotes: 1

nucandrei
nucandrei

Reputation: 951

Let's simplify code before converting it to lambda expressions

 public static Map<String, List<String>> agg(){
    List<String> list = Arrays.asList("Item A", "Item B", "Item C");
    List<String> servers = Arrays.asList("Server A", "Server B", "Server C", "Server D");
    Map<String, List<String>> map = new HashMap<>();
    for (int i = 0; i < list.size(); i++) {
        map.put(list.get(i), new ArrayList<>(servers));
    }
    return map;
}

I just simplified creating an array copy.

The pipeline to convert from a form of data to another in Java 8 contains the following steps:

  1. stream
  2. map (convert from a form to another)
  3. reduce (filter out unwanted values)
  4. collect (collect results)

Because your data does not need to be converted, just collected in a different structure, map and reduce are not needed

public static Map<String, List<String>> agg(){
    List<String> list = Arrays.asList("Item A", "Item B", "Item C");
    List<String> servers = Arrays.asList("Server A", "Server B", "Server C", "Server D");

    Function<String, String> keyFunction = key -> key;
    Function<String, List<String>> valueFunction = key -> new ArrayList<>(servers);

    return list.stream()
            .collect(Collectors.toMap(keyFunction, valueFunction));
}

Those functions can be inlined and the result would be:

    public static Map<String, List<String>> agg(){
    List<String> list = Arrays.asList("Item A", "Item B", "Item C");
    List<String> servers = Arrays.asList("Server A", "Server B", "Server C", "Server D");

    return list.stream()
            .collect(Collectors.toMap(key -> key, key -> new ArrayList<>(servers)));
}

Upvotes: 2

Hadi
Hadi

Reputation: 17289

Try this one:

list.stream()
            .map(item->new AbstractMap.SimpleEntry<>(item,servers))
            .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));

Upvotes: 0

dtanabe
dtanabe

Reputation: 1661

Create a stream of your "items", and collect them into a map where the keys are the objects from that stream, and the values are copies of the server list.

import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toMap;

...

List<String> servers = Arrays.asList("Server A", "Server B", "Server C", "Server D");
Map<String, List<String>> map = Stream.of("Item A", "Item B", "Item C")
    .collect(toMap(Function.identity(), (__) -> new ArrayList<>(servers)));

Upvotes: 2

Related Questions