c++映射和Java入口
C++ Map and Java Entry
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++;
谁能帮我把上面的代码转换成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++;
}
这就是我尝试的第二个意思是getValues()和第一个意思是getKey()?
Java没有Pair元组类,我不确定在这种情况下使用Entry是最好的选择,因为你需要更新Entry的"key"。
你应该创建一个类来做这些。
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++;
如果(itr.second
。第一个> 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.
}
如果map不包含键,Java的Hashmap get函数将返回null。这似乎是你上面的java代码的差距。看到的:http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
请注意,有些人建议使用一个小的类,而不是尝试使用一个通用的结构,如Entry(它实际上是用来表示Map中的键/值对)。参见:c++的Pair<L,R>在Java中?
相关文章:
- 在java中解决这段代码时面临循环中的问题
- 尝试用java代码编译和运行c++代码
- 在这种情况下,java对象是否可以调用本机函数
- 在java中读取c++字节的位字段
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 使用已使用 java 编码的 openssl 解码数据
- SWIG Java 在使用 -DSWIGWORDSIZE64 时将int64_t转换为 jlong
- Android Java USB for native cpp
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++ equivalent to Java Map getOrDefault?
- C++ 中的 Java 样式枚举
- 是否有技术原因阻止 Java 中的 final C++ 像 const 一样严格?
- 加密在 Windows、C++ 和 Java 中传输中的数据
- Java从C++回调到C++回调
- 在 c++ 中模拟输入并在 JAVA 中读取它?
- 用C++包装 Java 库 (JNI)
- 使用 TreeSet Java 对反转进行计数
- 验证openssl c++中的签名,这是由JAVA DSA签名的?
- 如何将 Java 程序的入口点更改为 C 签名
- c++映射和Java入口