## Permutation of a list with equals objects - Java

Question

```
List<String> original = Lists.newArrayList("A", "B", "C");
Collection<List<String>> permutations= Collections2.orderedPermutations(original)
```

gives

```
[A, B, C]
[A, C, B]
[B, A, C]
[B, C, A]
[C, A, B]
[C, B, A]
```

Great, now I have all possible permutations. But let's say A and C are equal, their order does not matter when they are next to each other.

Wanted result:

```
[A, C, B] or [C, A, B]
[B, A, C] or [B, C, A]
[A, B, C]
[C, B, A]
```

So we tried using a Comparator

```
Collection<List<String>> permutations= Collections2.orderedPermutations(original, new Comparator<String>() {
@Override
public int compare(final String o1, final String o2) {
if ((o1.equals("A") && o2.equals("C")) || (o2.equals("A") && o1.equals("C"))) {
return 0;
}
return o1.compareTo(o2);
}
});
```

But this gave us not the result we expected

```
[A, B, C]
[A, C, B]
```

Any suggestions how to get the wanted result? Although we used guava, any lib or custom solution is ok.

Show source

## Answers to Permutation of a list with equals objects - Java ( 3 )

I would expect this to work. I have not tested.

Edit: added missing parenthesis

If you want to remove permutations that are logically equivalent, then I think you should start by writing out the actual 3-permutations as they would really appear if

`A`

and`C`

were the same thing:With duplicates removed, you would be left with this:

By using a

`Set<List<String>>`

you can keep the duplicates out, i.e.My suggestion is in pair of ("A", "C") & ("C", "A") always put "A" first and transform it to ("A", "C")

Results are