How to manipulate Iterator position

1

I need to compare all the elements of a list 2 to 2 I'm doing in the following way:

List: 1, 2, 3, 4.

Comparison: 1 and 2, 1 and 3, 1 and 4, 2 and 1, 2 and 3 ...

I'm using this way:

    Iterator<Policie> p1Iterator = setOfPolicies.iterator();
    Iterator<Policie> p2Iterator;

    while(p1Iterator.hasNext()) {
        p1 = p1Iterator.next();
        count++;
        p2Iterator = setOfPolicies.iterator();

        while(p2Iterator.hasNext()) {
            p2 = p2Iterator.next();
        }
   }

What I wanted to stop doing, is to compare for example: 1 and 2 & 2 and 1. Since it's the same for me. So since I compare the first with all the others, the second with all the others ... The second time for example, I would compare the second with all the front of it, the third with all the front of it, and so on. The problem is that I can not manipulate the positions with the iterator, what could I do?

    
asked by anonymous 15.09.2015 / 15:42

1 answer

4

Does it necessarily have to be iterators?

You could convert to a list and thus interact with the positions of the list.

You can use the library Guava : (best performance)

import com.google.common.collect.Lists;

List<Policie> policies = Lists.newArrayList(p1Iterator);

Or the java library

import org.apache.commons.collections.IteratorUtils;

List<Policie> policies = IteratorUtils.toList(p1Iterator);

Or should it be mandatory with iterators? Using iterators will give you a bit more work.

Do you need to post the comparison logic too?

    
15.09.2015 / 15:53