0

我正在实现两相矩阵乘法。以下是第一阶段的减速器。键是左文件的行索引和右文件的列索引。我希望输出计数映射和减速器相同。但看起来内循环增加了与外循环相同的迭代器,因此减速器输出的数量等于键的数量。

代码片:

@Override
        public void reduce(Text key, Iterable<Text> values, Context context)
        throws IOException, InterruptedException
        {
            for (Text outer : values) {
                String [] outerLine = outer.toString().split("\t");
                //int outerIndex = 0;
                //outerIndex = outerLine[0].equalsIgnoreCase(leftFilePattern) ? outerIndex : 1;
                if(outerLine[0].equalsIgnoreCase(rightFilePattern))
                    continue;
                double outerValue = Double.parseDouble(outerLine[2]);
                for (Text inner : values) {
                    String [] innerLine = inner.toString().split("\t");
                    if(innerLine[0].equalsIgnoreCase(leftFilePattern))
                        continue;
                    context.write(new Text(key.toString() + "-" + innerLine[1]), 
                                new DoubleWritable(outerValue * Double.parseDouble(innerLine[2])));
                }
            }

但是当我有如下简单的java应用程序时:

List<Integer> l = Arrays.asList(10, 15);
        Iterable<Integer> it = l;
        for (Integer in : it) {
            for (Integer out : it) {
                System.out.println(in + " " + out);
            }
        }

这里的输出数为 4。如果内部循环的工作方式与减速器的情况相同,则输出计数应为 1,即(10 15)。

有人可以解释这种行为。

维沙尔

4

1 回答 1

0

There is a possible way of implementing Iterable that could cause that effect.

The intent seems to be that each iterator() call should return its own Iterator instance. The Iterable classes in java.util, including the private List class used by Arrays.asList(), all do so. That behavior is needed for nested foreach on the same Iterable, and the stated purpose of Iterable is foreach support.

I cannot find anything in the Iterable API document that directly requires that behavior.

Obviously, if the same object is used as result of two iterator() calls, nested foreach statements on the same Iterable will fail the way you describe.

=========================================================

If this is what is happening, my best suggestion is to do an initial pass over values copying each Text reference to a well-behaved List<Text> such as an ArrayList. You can safely do your nested loop over its elements.

于 2013-03-13T03:24:18.007 回答