Make iterators cloneable?

Dave Brosius dbrosius at mebigfatguy.com
Mon Sep 12 08:26:16 UTC 2016


That would give you unwanted duplicates

Fi-Fum and Fum-Fi for instance


On 09/11/2016 08:58 PM, Tagir Valeev wrote:
> Actually given the fact that we're iterating the Set (so the elements 
> are unique) and using the assumption that iteration of the same set is 
> stable, you can avoid numbering like this:
>
> for(String e1 : set) {
>   for(String e2 : set) {
>     if(e1 == e2) break;
>     System.out.println(e1+" <-> "+e2);
>   }
> }
>
> Again, such algorithm is fragile to concurrent changes.
>
> With best regards,
> Tagir Valeev.
>
> On Mon, Sep 12, 2016 at 7:51 AM, Tagir Valeev <amaembo at gmail.com 
> <mailto:amaembo at gmail.com>> wrote:
>
>     Hello, Peter!
>
>     I thought about numbering, but original Dave's code involved
>     concurrent set, so I presume that it's expected to be modified
>     from other threads. In this case my algorithm would output some
>     legal pairs (probably reflecting changes or not or reflecting only
>     partially) while your algorithm can output garbage (pair with
>     equal e1, e2 or two pairs like e1 <-> e2, e2 <-> e1 or can even
>     die with NoSuchElementException). Not sure what is better in
>     author's case.
>
>     Tagir.
>
>     On Sun, Sep 11, 2016 at 7:20 PM, Peter Levart
>     <peter.levart at gmail.com <mailto:peter.levart at gmail.com>> wrote:
>
>         Hi,
>
>         Even if the elements are not comparable, you could rely on the
>         fact that Collection(s) usually create iterators with stable
>         iteration order, so you could do the following:
>
>
>                 Set<Integer> set = Set.of(1, 2, 3, 4);
>
>                 Iterator<Integer> it1 = set.iterator();
>                 for (int n1 = 0; it1.hasNext(); n1++) {
>                     Integer e1 = it1.next();
>                     Iterator<Integer> it2 = set.iterator();
>                     for (int n2 = 0; n2 < n1; n2++) {
>                         Integer e2 = it2.next();
>                         System.out.println(e1 + " <-> " + e2);
>                     }
>                 }
>
>
>         Regards, Peter
>
>
>         On 09/11/2016 02:02 PM, Tagir F. Valeev wrote:
>>         Hello!
>>
>>         As your keys are comparable, you can create normal iterators and
>>         filter the results like this:
>>
>>         for(String v1 : s) {
>>            for(String v2 : s) {
>>              if(v1.compareTo(v2) < 0) {
>>                System.out.println(v1 + " <-->" + v2);
>>              }
>>            }
>>         }
>>
>>         Or using Stream API:
>>
>>         s.stream().flatMap(v1 -> s.stream()
>>              .filter(v2 -> v1.compareTo(v2) < 0).map(v2 -> v1 + " <-->" + v2))
>>           .forEach(System.out::println);
>>
>>         With best regards,
>>         Tagir Valeev.
>>
>>
>>         DB> It would be nice to be able to associate each element in a collection
>>         DB> with another element in the collection, which is something very easily
>>         DB> done with index based collections, but with sets, etc this isn't so
>>         DB> easy... unless i'm having a brainfart.
>>
>>         DB> So i'd like to do this, but Iterator doesn't implement Cloneable... Any
>>         DB> reason not to? or is there another way that's missing me?
>>
>>         DB> public class ItClone {
>>
>>         DB>      public static void main(String[] args) {
>>         DB>          Set<String> s = Collections.newSetFromMap(new
>>         DB> ConcurrentHashMap<String, Boolean>());
>>
>>         DB>          s.add("Fee");
>>         DB>          s.add("Fi");
>>         DB>          s.add("Fo");
>>         DB>          s.add("Fum");
>>
>>         DB>          Iterator<String> it1 = s.iterator();
>>         DB>          while (it1.hasNext()) {
>>         DB>              String v1 = it1.next();
>>
>>         DB>              Iterator<String> it2 = (Iterator<String>) it1.*clone*();
>>         DB>              while (it2.hasNext()) {
>>         DB>                  String v2 = it2.next();
>>
>>         DB>                  System.out.println(v1 + " <-->" + v2);
>>         DB>              }
>>         DB>          }
>>         DB>      }
>>         DB> }
>>
>
>
>



More information about the core-libs-dev mailing list