1

I'm trying to fix this piece of code where I'm printing from a hashmap having a list of plate numbers and owners (that format). I'm trying to print out just the owners via printOwners(); but I can't get it to not print duplicates.

I've played around with it for a while, just can't seem to skip over duplicates.

Here is my code:

import java.util.ArrayList;
import java.util.HashMap;

public class VehicleRegister {

    private HashMap<RegistrationPlate, String> owners;

    public VehicleRegister() {
        owners = new HashMap<RegistrationPlate, String>();
    }

    public boolean add(RegistrationPlate plate, String owner) {
        //search for existing plate
        if (!(owners.containsKey(plate))) { // add if no plate
            owners.put(plate, owner);
            return true;
        }

        //if plate is found, check for owner
        else if (owners.keySet().equals(owner)) {
           return false;
        }

        return false;
    }

    public String get(RegistrationPlate plate) {
        return owners.get(plate);
    }

    public boolean delete(RegistrationPlate plate) {
        if (owners.containsKey(plate)) {
            owners.remove(plate);
            return true;
        }

        return false; 
    }

    public void printRegistrationPlates() {
        for (RegistrationPlate item : owners.keySet()) {
            System.out.println(item);
        }
    }

    public void printOwners() {

        for (RegistrationPlate item : owners.keySet()) {
            System.out.println(owners.get(item));            
        }
    }
}

1 Answer 1

4

To remove the duplicates, use a HashSet<String>:

public void printOwners() {
    for (String s : new HashSet<>(owners.values())) {
        System.out.println(s);            
    }
}

Alternatively with Java 8 Stream and the distinct() method:

public void printOwners() {
    owners.values().stream().distinct().forEach(System.out::println);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I'm new. Is that because the printing of the new HashSet will only print a key if it's not a duplicate? Can you explain that if you will, I'm new to Java and just learning hash stuff. Also, thank you for answering. I was able to send it to an arraylist and print from there, but was wondering if there was a simpler way.
In Java, Set structures (which HashSet implements) do not contain duplicates: if an element is added several times to the set, only one is actually contained in it. Using Set is usually the way to go to remove duplicates from an other collection, such as a List which can contain the same element multiple times.
Ahh, one line of code would be a pretty simple solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.