如何遍历MapWriteable类型
时间: 2023-07-04 19:18:19 浏览: 79
MapWritable 是Hadoop中实现的一个可序列化的Map数据结构,它可以用来存储键值对。遍历MapWritable可以使用两种方式:
1.通过entrySet()方法获取键值对集合,然后遍历集合中的每个Entry对象,获取键和值的值。
例如:
```
MapWritable map = new MapWritable();
map.put(new Text("key1"), new IntWritable(1));
map.put(new Text("key2"), new IntWritable(2));
for (Map.Entry<Writable, Writable> entry : map.entrySet()) {
Text key = (Text) entry.getKey();
IntWritable value = (IntWritable) entry.getValue();
System.out.println(key.toString() + " : " + value.get());
}
```
2.通过keySet()方法获取所有的键的集合,然后遍历集合中的每个键,获取每个键对应的值。
例如:
```
MapWritable map = new MapWritable();
map.put(new Text("key1"), new IntWritable(1));
map.put(new Text("key2"), new IntWritable(2));
for (Writable writable : map.keySet()) {
Text key = (Text) writable;
IntWritable value = (IntWritable) map.get(key);
System.out.println(key.toString() + " : " + value.get());
}
```
阅读全文