How to get all pairs of a set efficiently?

java, set

Solution

List<Integer> list = new ArrayList<Integer>(mySet);
for (int i = 0; i < list .size(); i++)
    for (int j = i + 1; j < list .size(); j++)
        // Do something with vector.get(i) and vector.get(j)

Problem

Given an object of a `Set`, I want to walk through all (unordered) pairs of it. Example: Set = {1, 2, 3}, Pairs: (1, 2), (1, 3), (2, 3). When dealing with a `Vector<Integer>`, one can achieve this with help of the index of each element: ``` for (int i = 0; i < vector.size(); i++) for (int j = i + 1; j < vector.size(); j++) // Do something with vector.get(i) and vector.get(j) ``` But elements in a `Set<Integer>` have no indices. The best solution, I found so far, is to convert the `Set` to a `Vector` and use the solution above. Is there a more efficient / direct solution ?

Original source

Related problems