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()?

user236501
  • 8,538
  • 24
  • 85
  • 119

2 Answers2

0

Java's Hashmap get function will return null if the map does not contain the key. That seems to be the gap in your java code above. See: http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

Note that some people suggest using a small class rather try to use a generic structure like Entry (which actually was intended to represent a key/value pair in a Map). See: What is the equivalent of the C++ Pair<L,R> in Java?

Community
  • 1
  • 1
jeffrey_t_b
  • 1,779
  • 12
  • 18
  • what does it mean cIter->second.first++; – user236501 Dec 03 '11 at 07:18
  • If you are reusing the Entry class, then I guess that it would be get/setValue. See: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html. It might be better to use one of the pair classes from the Stackoverflow link above, or your own class, rather than the Entry class, as it doesn't seem like the semantics are correct. – jeffrey_t_b Dec 03 '11 at 07:27
0

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.
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Hi thanks, I got another code this: I am very confused with the first and second, which is getValue or getKey or counter1 and counter2? Can you help? if (itr.second.first > max) { max = itr.second.first; minCount = itr.second.second; code = itr.next().getKey(); } – user236501 Dec 03 '11 at 07:28
  • `I am very confused with the first and second` which one of the reasons Java doesn't have Pair. ;) – Peter Lawrey Dec 03 '11 at 07:34
  • Oic thanks so your counter2 is actually refer to values right? and for C++ pair first actually refer to key and second refer to values am I right? – user236501 Dec 03 '11 at 07:37
  • correct. That why I would give counter1 and counter2 meaningful names to avoid confusion for anyone who has to read your code (it could be you ;) – Peter Lawrey Dec 03 '11 at 07:42