在Java 8中编写以下代码的可能方法

时间:2019-02-23 18:13:29

标签: java lambda java-8

我有一个可以按要求工作的代码,但是我想用Java 8重写它。


这段代码将生成一个地图。每个列表项都将分配有所有服务器。

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;
    }


输出

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]


λ等于多少?

4 个答案:

答案 0 :(得分:2)

创建一个“项目”流,并将它们收集到一个映射中,其中键是该流中的对象,值是服务器列表的副本。

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)));

答案 1 :(得分:2)

让我们先简化代码,然后再将其转换为lambda表达式

 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;
}

我只是简化了数组副本的创建。

在Java 8中从一种数据形式转换为另一种形式的管道包含以下步骤:

  1. 地图(从一种形式转换为另一种形式)
  2. 减少(过滤掉不需要的值)
  3. 收集(收集结果)

因为您的数据不需要转换,只需以不同的结构收集,就不需要映射和缩小

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));
}

可以内联这些函数,结果将是:

    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)));
}

答案 2 :(得分:1)

一种简单的forEach方法是

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

答案 3 :(得分:0)

尝试一下:

let
div = document.querySelector('div'),
text = div.innerText
;
div.addEventListener('mouseup', evt => {
    let
    sel = getSelection(),
    result = {start: null, end: null}
    ;
    if (!sel) return;
    ['start', 'end'].forEach(which => {
        let
        counter = 1,
        tmpNode = div.querySelector('span'),
        node = which == 'start' ? 'anchor' : 'focus'
        ;
        while(tmpNode !== sel[node+'Node'].parentElement) {
            result[which] += tmpNode.innerText.length;
            counter++;
            tmpNode = div.querySelector('span:nth-child('+counter+')')
        }
        result[which] += sel[node+'Offset'] + (which == 'start' ? 1 : 0);
    });
    alert('Selection starts at '+result.start+' and ends at '+result.end);
}, false);