Dev1ce
Dev1ce

Reputation: 5954

List to Map using Java8

I want to convert a list of objects to a Map,
I'm trying to do it using Java 8's stream API,
I'm getting 2 errors, 1 on import and 1 in the conversion code of List to Map.

Error while importing the Map interface -
The import java.util.Map conflicts with a type defined in the same file.

Error in the conversion code -

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

Following is my code -

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 + "]";
    }

}

My main class -

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;

    }
}

I referred the following question but I am unable to import the Map interface - The type HashMap is not generic; it cannot be parameterized with arguments <String, Integer>

Upvotes: 2

Views: 584

Answers (3)

Sibin Muhammed A R
Sibin Muhammed A R

Reputation: 1432

You don't need to use the fully qualified name if you are using Java 11 or above.

You can use the var keyword, introduced in Java 10, to declare local variables without explicitly specifying the type.

 var result = listDevs
                .stream()
                .collect(Collectors.toMap(Developer::getName, Developer::getSal));

Upvotes: 0

sergiotarxz
sergiotarxz

Reputation: 540

Instead of changing the class name, you can also do the following:

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

Upvotes: 1

Ousmane D.
Ousmane D.

Reputation: 56489

The containing class is called Map hence the compilation error. To solve the issue simply rename your class to a different name or use:

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

Upvotes: 6

Related Questions