java - 使用 Java8 列表到 map

标签 java java-8 java-stream

我想将对象列表转换为 map ,
我正在尝试使用 Java 8 的流 API 来做到这一点,
我遇到 2 个错误,1 个在导入时,1 个在 List 到 Map 的转换代码中。

导入 map 界面时出错-
导入的 java.util.Map 与同一文件中定义的类型冲突。

转换代码错误-

The type Map is not generic; it cannot be parameterized with arguments <String, BigDecimal>

以下是我的代码-

public class Developer {

    private String name;
    private BigDecimal sal;
    private int age;

    /**
     * @param name
     * @param sal
     * @param age
     */
    public Developer(String name, BigDecimal sal, int age) {
        super();
        this.name = name;
        this.sal = sal;
        this.age = age;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the sal
     */
    public BigDecimal getSal() {
        return sal;
    }

    /**
     * @param sal
     *            the sal to set
     */
    public void setSal(BigDecimal sal) {
        this.sal = sal;
    }

    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }

    /**
     * @param age
     *            the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Developer [name=" + name + ", sal=" + sal + ", age=" + age + "]";
    }

}

我的主课-

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Map;

public class Map {

    public static void main(String[] args) {

        List<Developer> listDevs = getDevelopers();

        //Error here
        Map<String, BigDecimal> result =  listDevs.stream().collect(Collectors.toMap(Developer :: getName, Developer :: getSal));

    }

    private static List<Developer> getDevelopers() {

        List<Developer> result = new ArrayList<>();

        result.add(new Developer("mkyong", new BigDecimal("70000"), 33));
        result.add(new Developer("alvin", new BigDecimal("80000"), 20));
        result.add(new Developer("jason", new BigDecimal("100000"), 10));
        result.add(new Developer("iris", new BigDecimal("170000"), 55));

        return result;

    }
}

我提到了以下问题,但我无法导入 Map 界面 - The type HashMap is not generic; it cannot be parameterized with arguments <String, Integer>

最佳答案

包含类称为 Map 因此出现编译错误。要解决此问题,只需将您的类重命名为其他名称或使用:

java.util.Map<String, BigDecimal> result =  
        listDevs.stream()
                .collect(Collectors.toMap(Developer::getName, Developer::getSal));

关于java - 使用 Java8 列表到 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50667821/

相关文章:

java - 存在多个构造函数时使用 Autowiring ="constructor"进行依赖注入(inject)?

java - 基于运输时间的热图/轮廓(反向等时轮廓)

java - 获取 Java 时区的区域规则时出现异常?

Java 8 - 接口(interface)不再是抽象的了吗?

java 8 : how to stream an array of int, 提取低字节,并创建一个字节数组

java - Jackson:将枚举值序列化和反序列化为整数

java - 如何将 Base64 blob 字符串转换为 android 中的图像?

java - Java 中的条件委托(delegate)

Java 8 函数式接口(interface),无参数,无返回值

java - 流图签名