0

I'm doing a code exercise and can't find a way to insert in a HashMap duplicate values.

This my code:

import java.util.*;
public class Bug {

    private String Errore;
    private String Dev;
    static Set<Bug> NotAssigned = new HashSet<>();
    static Set<Bug> getAssigned = new HashSet<>();
    static Map<String, Bug> Assigned = new HashMap<>();

    Bug(String DescrErrore){
        Errore=DescrErrore;
        NotAssigned.add(this);
    }
    public static Set<Bug> getUnassigned(){
        return NotAssigned;
    }
    public void assignTo(String Dev){
        this.Dev=Dev;
        Assigned.put(Dev,this);
        NotAssigned.remove(this);
    }
    public static Set<Bug> getAssignedTo(String Dev){
        getAssigned.add(Assigned.get(Dev));
        return getAssigned;
    }

    @Override
    public String toString() {
        return "[(" +
                Errore + '\'' + "to "+ Dev +
                ")]";
    }

    public static void main(String[] args) {
        Bug b1 = new Bug("Application crashes on Windows 8"),
                b2 = new Bug("Application freezes after 2 hours"),
                b3 = new Bug("Application does not print on laser printer"),
                        b4 = new Bug("Data missing after partial save");
        Set<Bug> unassigned = Bug.getUnassigned();
        System.out.println(unassigned.size ());
        b2.assignTo("Paolo");
        b3.assignTo("Filomena");
        b4.assignTo("Filomena");
        System.out.println(unassigned.size());
        Set<Bug> filo = Bug.getAssignedTo("Filomena");
        System.out.println( filo );
    }
}

The correct output should be:

4
1
[("Data missing after partial save",assigned to Filomena),("Application does notprint on laser printer",assigned to Filomena)]

But, "Filomena" is a duplicate and does not display the other error message from HashMap. How can I do? Sorry for my imperfect translation.

GURU Shreyansh
  • 881
  • 1
  • 7
  • 19
  • 1
    A map doesn't support multiple values under the same key, you'll have to work around that. See the link above the question on how to do that. Also, although it is unrelated to the issue here, you'll have issues when using `Bug` in Sets, because you didn't override `equals` and `hashCode`. See [Why do I need to override the equals and hashCode methods in Java?](//stackoverflow.com/q/2265503) for that. Also, another issue here is that you mutate `Bug`, so using such instances in a Set _will_ fail to work as expected. – Tom Jun 24 '21 at 16:53
  • Thanks Tom, follow your advice – Massimo Colucci Jun 24 '21 at 19:52

0 Answers0