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 6891939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:28:21+00:00 2026-05-27T06:28:21+00:00

map<string, pair<int, int> > common; map<string, pair<int, int> >::iterator cIter = common.find(code); if(cIter ==

  • 0
  map<string, pair<int, int> > common;     
 map<string, pair<int, int> >::iterator cIter = common.find(code);
        if(cIter == common.end())
        {
            pair<int, int> values(1, count);
            common.insert(make_pair(code, values));
        }
        else
            cIter->second.first++;

Anyone can help me convert the code above to Java?

 private java.util.HashMap<String, Entry<Integer, Integer>> common = new java.util.HashMap<String, Entry<Integer, Integer>>();
 Entry<Integer, Integer> cIter = common.get(code);
            if (cIter == common.) {
                Entry<Integer, Integer> values =  new AbstractMap.SimpleEntry<Integer, Integer>(1, count);
                common.put(code, values);
            } else {
                cIter.second.first++;
            }

This is what I tried does second means getValues() and first means getKey()?

  • 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-05-27T06:28:21+00:00Added an answer on May 27, 2026 at 6:28 am

    Java doesn’t have a Pair tuple class and I’m not sure using Entry is the best choice int he case, as you need to update the “key” of the Entry.

    Instead you should create a class to do this.

    class Counters {
        int counter1; // use meaningful names here
        final counter2;
        public Counters(counter1, counter2) { this.counter1 = counter1; this.counter2 = counter2; }
    }
    
    Map<String, Counters> common = new HashMap<>();
    
    Counters counters = common.get(code);
    if (counters == null)
        common.put(code, counters = new Counters(1, count));
    else
        counters.counter2++;
    

    if (itr.second.first > max) { max = itr.second.first; minCount = itr.second.second; code = itr.next().getKey(); }

    if(counters.counter1 > max) {
         max = counters.counter1;
         minCount = counters.counter2;
         code = null; // isn't needed AFAIK.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

map <int, string> rollCallRegister; map <int, string> :: iterator rollCallRegisterIter; pair <map <int, string>
I want to insert a pair< string, vector<float> > into a map, first it
If I have a vector<string*> *vect or a map<pair<string*, int*>, string*> *map , how
I'd like to copy values that match a predicate (equal ints) from a map<string,int>
So lets say I have: map<pair<int, MyEnum>, string> myMap; and I add a key
What's the difference between the following two cases? std::pair<int,std::string> example_1 (std::make_pair (1,foo)); int value_1
I have this map<string, vector <pair<int, int> > > variable and I'm pushing back
I have a variable with a type similar to: map<bool, map<string, pair<string, int> >
Since std::pair<std::string, unsigned int> is not defined for __gnu_cxx hash map, how do I
map <int, string> rollCallRegister; map <int, string> :: iterator rollCallRegisterIter; map <int, string> ::

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.