当前位置: 首页 > 知识库问答 >
问题:

意外绑定-为什么我不能在此收集器中使用类似t extends Collection的东西?

姬旭
2023-03-14

您好,我编写了这个自定义收集器,用于将元素列表转换为多重映射:

public class MultiMapCollector implements Collector<Entity,
        Map<String, Collection<String>>, Map<String, Collection<String>>> {

    @Override
    public Supplier<Map<String, Collection<String>>> supplier() {
        return HashMap::new;
    }

    @Override
    public BiConsumer<Map<String, Collection<String>>, Entity> accumulator() {
        return (map, e) -> map.computeIfAbsent(e.getName(),
                k -> new HashSet<>()).add(e.getValue());
    }

    @Override
    public BinaryOperator<Map<String, Collection<String>>> combiner() {
        return (map1, map2) -> {
            map2.keySet().forEach(key -> map1.computeIfAbsent(key, k -> new HashSet<>()).addAll(map2.get(key)));
            return map1;
        };
    }

    @Override
    public Function<Map<String, Collection<String>>, Map<String, Collection<String>>> finisher() {
        return Collections::unmodifiableMap;
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Set.of(Characteristics.UNORDERED);
    }
}

现在我希望能够传递一个List或一个Set

所以不是Map

但当我这样做的时候,我会得到一个模糊的错误,说:

Unexpected Bounds

我做错了什么?

我似乎无法理解这一点。


共有1个答案

燕永昌
2023-03-14

如果希望允许不同的集合实现

例如:

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

public class MultiMapCollector<V extends Collection<String>>
    implements Collector<Entity, Map<String, V>, Map<String, V>> {

  private final Supplier<? extends V> valueSupplier;

  public MultiMapCollector(Supplier<? extends V> valueSupplier) {
    this.valueSupplier = valueSupplier;
  }

  @Override
  public Supplier<Map<String, V>> supplier() {
    return HashMap::new;
  }

  @Override
  public BiConsumer<Map<String, V>, Entity> accumulator() {
    return (map, e) -> map.computeIfAbsent(e.getName(), k -> valueSupplier.get()).add(e.getValue());
  }

  @Override
  public BinaryOperator<Map<String, V>> combiner() {
    return (map1, map2) -> {
      for (var entry : map2.entrySet()) {
        map1.merge(
            entry.getKey(),
            entry.getValue(),
            (curValue, newValue) -> {
              curValue.addAll(newValue);
              return curValue;
            });
      }
      return map1;
    };
  }

  @Override
  public Function<Map<String, V>, Map<String, V>> finisher() {
    return Collections::unmodifiableMap;
  }

  @Override
  public Set<Characteristics> characteristics() {
    return Set.of(Characteristics.UNORDERED);
  }
}

注意:MultiMapCollector现在具有类型参数

另外,请注意,我更新了您的combiner()函数,以使用Map#merge(…) ,尽管这不是严格必要的。

 类似资料:
  • 创建jar文件时,我编写了以下java文件: 我将文件命名为MyClass.java。 我通过键入“javac src/main/java/myjar/myclass.java”创建了一个类文件 我通过命令“jar cvf myjar.jar src/main/java/myjar/myclass.class”生成了一个jar文件 在IDE中,我写了“import myjar.myclass”,单

  • 我正在对实现接口的类的对象进行流式处理。我希望将它们收集为接口的元素列表,而不是实现类。 对于Java16.0.1的方法,这似乎是不可能的。例如,在下面的代码中,最后一条语句将无法编译。 我们可以显式地将每个元素从转换为。但至少为了简洁起见,我们可以使用。 为什么我不能在Java 16中使用Stream#ToList来收集类的接口列表? 如果有人有比明确选角更好的解决方案,我也很乐意听到:)

  • 问题内容: 试图弄清楚React的基础知识。 查看此页面上的第二个示例:https : //facebook.github.io/react/ 我看到tick()函数设置Timer类的状态,将前一个值加1。 但是,当我尝试实现自己的简单Counter类时,它失败了,并且出现控制台错误,提示 无法读取未定义的setState属性。 一些谷歌搜索显示我必须将其绑定到增量函数。但是,为什么在我看到的第一

  • 我在做一个非常简单的两个按钮状态。如果我点击abutton,A组件显示,如果点击bButk,那么B组件。我正在映射通过数组的项目,以便每个项目都有自己的按钮状态。假设我点击项目1的按钮B,那么我只想要第一个项目B显示。现在所有这些都会同时被触发。我已经在构造函数中限制了它们,但我仍然无法只获得一次点击触发并显示相关组件。

  • 由于找不到文件,send方法返回null BufferedReader。Eclipse只是说有一个NullPointerException是因为print方法,但是当我移除所有try/catch语句时,Eclipse说我需要编写方法抛出IOException或FileNotFoundException,它也允许我这样做,如果我不这样做,它就抛出一个FileNotFoundException。然而,

  • 我正在尝试使用文件系统。我的< code>CMakeLists.txt中有< code>-std=c 11 -std=c 1y。GCC版本为4.9.2。然而,我得到了一个错误: 使用的正确方法是什么?