最近测试段代码,发现在reduce过程中使用List保存Text的列表会出现元素替换的问题。大家有没有遇到,大神是否能指出其中的原因。
问题描述:
往List中添加Text元素。例如 添加'PA 、PB、ADD:DB。都是字符串。
然后再遍历List 结果则变为[ADD:DB,ADD:DB,ADD:DB]。三个元素都变为最后一次add的数据。
但是将元素按照String类型加入List,则无次情况。
代码如下(请大神指正错误地方):
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
List<Text> list = new ArrayList<Text>();
//key : uid
//value ADD:DD 或PD
System.out.println("=====key " + key + " reduce=====");
list.clear();
//System.out.println("key:" + key.toString());
for (Text value : values) {
String pid = value.toString();
//System.out.println(value.toString());
System.out.print("" + pid + "\t");
list.add(value);
}
System.out.println();
System.out.println(list.toString());
System.out.println();
}
结果如:
String 添加:
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
List<String> list = new ArrayList<String>();
//key : uid
//value ADD:DD 或PD
System.out.println("=====key " + key + " reduce=====");
list.clear();
//System.out.println("key:" + key.toString());
for (Text value : values) {
String pid = value.toString();
//System.out.println(value.toString());
System.out.print("" + pid + "\t");
list.add(pid);
}
System.out.println();
System.out.println(list.toString());
System.out.println();
}
结果是正确的: