Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8273557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:31:14+00:00 2026-06-08T07:31:14+00:00

HashMap<Integer,Integer> hashmapsample= new HashMap<Integer, Integer>(); I can have values like (1 , 7) (2

  • 0
HashMap<Integer,Integer> hashmapsample= new HashMap<Integer, Integer>();

I can have values like

(1 , 7)
(2 , 4)
(4 , 5)
(3,  7)

There wont be any duplicate Keys . Only Duplicate values can occur

I want to pick the (Key,Value) pair which has duplicate values.

If I get the Duplicate (Key,Value) as another Hashmap it would be great.
How Do I go about it.?

I expect the output

 (1 , 7)
 (3,  7)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:31:15+00:00Added an answer on June 8, 2026 at 7:31 am

    How about this?

    public HashMap getDuplicateValues(HashMap in)
    {
       // Clone input HashMap because we're removing stuff from it
       in = (HashMap)in.clone();
       HashMap rval = new HashMap();
       Object[] keys = in.keySet().toArray();
    
       // iterate through all keys
       for(int x=0;x<keys.length;x++) {
          Object value = in.get(keys[x]);
          in.remove(keys[x]);
          // if value is in input HashMap, store it in duplicate HashMap because it has another value
          if(in.containsValue(value)) {
             rval.put(keys[x],value);
          }
          // if value is in duplicate HashMap, store it also because it HAD another value earlier
          if(rval.containsValue(value)) {
             rval.put(keys[x],value);
          }
       }
    
       return(rval);
    }
    

    This method will return key/value pairs for all the duplicate values in the input HashMap.


    Test code:

      HashMap map = new HashMap();
    
      map.put("1","2");
      map.put("2","1");
      map.put("3","8");
      map.put("4","4");
      map.put("5","6");
      map.put("6","8");
      map.put("7","3");
      map.put("8","4");
      map.put("9","4");
    
      HashMap dups = getDuplicateValues(map);
    
      System.out.println("MAP = "+map);
      System.out.println("DUP = "+dups);
    

    Output:

    MAP = {3=8, 2=1, 1=2, 7=3, 6=8, 5=6, 4=4, 9=4, 8=4}
    DUP = {3=8, 6=8, 4=4, 9=4, 8=4}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have hashmap that has keys like this:'2+4+5' , '653+65+1324+75'.(integer values delimited by a
I have a Hashmap in Java like this: private Map<String, Integer> team1 = new
HashMap selections = new HashMap<Integer, Float>(); How can i get the Integer key of
I have a nested map: Map<Integer, Map<Integer, Double>> areaPrices = new HashMap<Integer, Map<Integer, Double>>();
I have a HashMap of a following structure: HashMap<Integer, int[]> map = new HashMap<Integer,
I have for instance this Map: Map<String, Integer> map = new HashMap<String,Integer>(); than I
I have this HashMap: HashMap<String, Integer> m which basically stores any word (String) and
Possible Duplicate: Java Ordered Map I have list of product object in the HashMap<Integer,Product>
I have an Hashmap <integer, Arraylist<Double>> . I would like to make a copy
I have my HashMap, private final HashMap<Integer, Poll> pollmap = new HashMap<Integer, Poll>(); and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.