5 Replies - 303 Views - Last Post: 19 December 2013 - 03:16 PM Rate Topic: -----

#1 TLHPoE  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 25-September 13

Task of detecting duplicate values in a HashMap

Posted 18 December 2013 - 07:19 PM

How would I go about detecting duplicate values in a HashMap and then find the entry?

Not sure how to post the code for this problem, but here's my HashMap:
public static HashMap<String, Integer> idMap = new HashMap<String, Integer>();


This post has been edited by TLHPoE: 18 December 2013 - 07:22 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Task of detecting duplicate values in a HashMap

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10175
  • View blog
  • Posts: 37,579
  • Joined: 27-December 08

Re: Task of detecting duplicate values in a HashMap

Posted 18 December 2013 - 07:29 PM

Sounds like you might be going about your problem backwards. Why do you want to go about detecting duplicate values?

Note- you could easily do this by traversing through the EntrySet with nested loops, or use a HashSet and begin to add the values (again by iterating through the EntrySet). If you see no change in size() on the HashSet, your current entry is a duplicate.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2552
  • View blog
  • Posts: 10,636
  • Joined: 20-September 08

Re: Task of detecting duplicate values in a HashMap

Posted 19 December 2013 - 02:56 AM

Use http://technojeeves..../49-mapreverser and then iterate the entry set to find lists where size > 1
Was This Post Helpful? 0
  • +
  • -

#4 TLHPoE  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 25-September 13

Re: Task of detecting duplicate values in a HashMap

Posted 19 December 2013 - 02:42 PM

I'm not sure if comparing sizes will work. :(

My HashMap is filled with stuff like this:
BlockGrass>1
BlockDirt>2
BlockStone>3
...
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2552
  • View blog
  • Posts: 10,636
  • Joined: 20-September 08

Re: Task of detecting duplicate values in a HashMap

Posted 19 December 2013 - 03:07 PM

Quote

I'm not sure if comparing sizes will work. :(

Why would it not work?

Quote

How would I go about detecting duplicate values in a HashMap and then find the entry?


Quote

My HashMap is filled with stuff like this:
BlockGrass>1
BlockDirt>2
BlockStone>3

Well you haven't seen fit to show any 'duplicate values' there, so allow me:


BlockDirt>2
BlockSand>2
BlockIce>2

Map reversed:

2>[BlockDirt, BlockSand, BlockIce]

value.size() == 3, ergo duplicate values found
Was This Post Helpful? 0
  • +
  • -

#6 TLHPoE  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 25-September 13

Re: Task of detecting duplicate values in a HashMap

Posted 19 December 2013 - 03:16 PM

Oh, I didn't really understand what you were saying the first time.

Thanks :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1