0

hashmap key includes registration number and hashmap value includes owners name. Here is my code:

public static void main(String[] args) {

    HashMap<String, String> data = new HashMap<>();
    Scanner reader = new Scanner(System.in);

    data.put("AAA-111", "Jack");
    data.put("BBB-222", "Matt");
    data.put("CCC-333", "Jack");

    for (HashMap.Entry<String, String> entry: data.entrySet()) {
        System.out.println(entry.getValue());
        //data.values().remove(entry.getValue());
    }
}

And the current output:

Jack
Matt
Jack

The problem is that I don't want to print out same owner two times. My goal output is:

Jack
Matt

How I can print same values only one time?

1
  • 1
    Add names to set (it will remove duplicates) and print set content... Commented Mar 20, 2018 at 20:20

6 Answers 6

8

You can create a Set like this :

Set<String> names = new HashSet<>(data.values());

Outputs

[Matt, Jack]
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is the easiest (and most readable) way to do it. Use a TreeSet if you want the values sorted.
2

1. With Streams to obtain a List or a Set :

// 1 list
List<String> list = data.values().stream()
                        .distinct().collect(Collectors.toList()); //[Jack, Matt]
// 2 set
Set<String> set = data.values().stream()
                      .distinct().collect(Collectors.toSet()); //[Matt, Jack]

2. Without Streams to obtain a basic or sorted Set :

// 1 simple Set
Set<String> set = new HashSet<>(data.values()); //[Matt, Jack]
// 2 sorted Set
Set<String> set = new TreeSet<>(data.values()); //[Jack, Matt]

Comments

2

The easiest way to do that would be with Streams:

data.values().stream().distinct().forEach(System.out::println);

2 Comments

can directly take .values()
This is a way to do it but I wouldn't call it "the easiest way".
1

It also can be done like this using Map::values and Stream::distinct:

data.values().stream().distinct().forEach(System.out::println);

3 Comments

Nice. Did you just copy my answer?
No I didn't. Guess you had something different before.
@JuanCarlosMendoza this is not same he used values and not entryset
1

HashMap maps unique keys with values, values may be duplicate. The put() method accepts two parameter first being the Key and second being the Value.

data.put("AAA-111", "Jack");
data.put("BBB-222", "Matt");
data.put("CCC-333", "Jack");

Now data.put("BBB-222", "Jill"); will replace "Matt" and "BBB-222" will be mapped with "Jill".

To have unique elements you can use Set and add elements, it will not add duplicate element. If you are getting a HashMap then either you can use Stream API as mentioned in other answers to extract distinct values or you can iterate the HashMap values() and can keep on adding each value to Set (you can choose HashSet ,TreeSet). The duplicate values will be discarded and be added only once. Then you can iterate the set to read distinct elements.

Comments

-1

You must put like a key the owner and the code like a value.

The value of last owner overrides the last.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.