Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: check duplicate key and value in hashmap

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Thanked 0 Times in 0 Posts

    Default check duplicate key and value in hashmap


    i was wondering how you would check for a duplicate item that has both the same key and value.

    ie, if you do like

    checkDuplicates.put("David", 123)
    is there a way to check a string that has exactly that key?

    if i map another "David" with 555, that's totally fine.

    but im having trouble trying to figure out a way to check if there is another item that has "David" with 123.

    any pseudocode would be great = )
    thank you = )

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 832 Times in 772 Posts
    Blog Entries

    Default Re: check duplicate key and value in hashmap

    By default, keys are unique in a Map (in other words there aren't duplicate keys). If you are trying to look for duplicate values, you could a) iterate over the Map to look for duplicates or b) stick the values from the first Map (call it mapA) in another map (call it mapB) as keys, valued to a list of the keys from mapA - basically reverse the Map. You can then retrieve them and see if there are duplicates based upon the List size.

Similar Threads

  1. hashmap & arraylist mapping
    By abhi178 in forum Collections and Generics
    Replies: 3
    Last Post: November 29th, 2010, 07:32 AM
  2. duplicate t:dataTable
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: August 4th, 2010, 10:35 AM
  3. Problem in HashMap
    By Hikari9 in forum Collections and Generics
    Replies: 2
    Last Post: April 19th, 2010, 10:44 PM
  4. Bitstrings vs Hashmap
    By April in forum Collections and Generics
    Replies: 3
    Last Post: February 2nd, 2010, 11:56 AM
  5. Eliminating duplicate values
    By Harry_ in forum Collections and Generics
    Replies: 7
    Last Post: November 9th, 2009, 06:35 AM

Tags for this Thread