title: Iterable throws NoSuchElementException even after I have verified by printing each element
body: <p>I have a bunch of <code>Map&lt;Integer, TreeSet&lt;Double&gt;&gt;</code> objects, and I am passing the values off to another method. I got a <code>NoSuchElementException</code> in that method, so started debugging, and saw that the call to <code>Map#values()</code> throws this exception. But, when I loop through the values, it prints all the values perfectly. Here's the code snippet that's causing this issue:</p>
  
  <pre><code>Map&lt;Integer, TreeMultimap&lt;String, Double&gt;&gt; kMap = Maps.newTreeMap();
  for (int i = 0; i &lt; 3; i++) {
      TreeMultimap&lt;String, Double&gt; treeMultimap = TreeMultimap.create();
      treeMultimap.putAll("a" + i, Lists.newArrayList(0.0, 0.06, 0.17, 0.23));
      treeMultimap.putAll("b" + i, Lists.newArrayList(0.0, 0.16, 0.34, 0.49));
      kMap.put(i, treeMultimap);
  }
  
  Map&lt;Integer, TreeSet&lt;Double&gt;&gt; rescaledKMap =
      Maps.transformEntries(kMap, ToRescaledValueMap(1.2));
  
  Iterable&lt;TreeSet&lt;Double&gt;&gt; treeSets = rescaledKMap.values();
  
  // Prints out each element correctly. No exception is thrown.
  for (TreeSet&lt;Double&gt; set : treeSets)
      System.out.println(Joiner.on(',').join(set));
  
  // NoSuchElementException thrown in getMeanPlot() before anything happens there.
  TreeMap&lt;Double, Double&gt; mean_rescaledKMap = 
      getMeanPlot(rescaledKMap.values());
  </code></pre>
  
  <p>The signature is <code>getMeanPlot(Iterable&lt;TreeSet&lt;Double&gt;&gt;)</code>.</p>
  
  <p>How is the iterable correct just before it gets passed to the second method, but throws an exception just as the second method start?</p>
  
  <p><strong>Stack Trace</strong></p>
  
  <p>The stack trace shows the <code>Maps.EntryTransformer</code> object <code>ToRescaledValueMap</code> as the culprit. Here's the trace:</p>
  
  <pre><code>Exception in thread "main" java.util.NoSuchElementException
      at java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1113)
      at java.util.TreeMap$EntryIterator.next(TreeMap.java:1151)
      at java.util.TreeMap$EntryIterator.next(TreeMap.java:1146)
      at com.google.common.collect.AbstractMapBasedMultimap$Itr.next(AbstractMapBasedMultimap.java:1145)
      at com.google.common.collect.Ordering.min(Ordering.java:460)
      at com.google.common.collect.Ordering.min(Ordering.java:479)
      at DistributionMetricExperiment$6.transformEntry
  </code></pre>
  
  <p>Does this have anything to with Guava's lazy initialization? I can write my own little method instead of using the <code>Maps#transformEntries</code> method, but not knowing the reason behind this bug is ... well ... bugging me. The entry transformation is as follows:</p>
  
  <pre><code>static Maps.EntryTransformer&lt;Integer, TreeMultimap&lt;String,Double&gt;, TreeSet&lt;Double&gt;&gt;
  ToRescaledValueMap(final double interval_length) {
      return new Maps.EntryTransformer&lt;Integer, TreeMultimap&lt;String,Double&gt;, TreeSet&lt;Double&gt;&gt;()
      {
          public TreeSet&lt;Double&gt; transformEntry(Integer i, TreeMultimap&lt;String,Double&gt; mmap) {
              double mmap_min = Ordering.natural().min(mmap.values());
              double mmap_max = Ordering.natural().max(mmap.values());
              TreeSet&lt;Double&gt; rescaledValues = Sets.newTreeSet();
              for (double val : mmap.values())
                  rescaledValues.add(interval_length * (val - mmap_min)/(mmap_max - mmap_min));
              return rescaledValues;
          }
      };
  }
  </code></pre>
  
  <p><em>Edit</em>:</p>
  
  <ul>
  <li>I don't know whether this matters or not, but wanted to add that I have also issued the same print statements (the for loop over <code>Iterable&lt;TreeSet&lt;Double&gt;</code> in the above code) as the first statement of <code>getMeanPlot</code>. Even there, it prints alright, and then throws the exception.</li>
  <li>My apologies for the use of underscore in variable names. The variables are reflecting subscripts I have been using in the maths behind my code. I am aware of the naming convention, but in this code I was more focused on having the ability to recognize the mathematical meaning of a variable just by looking at it.</li>
  </ul>