If you can impose an ordering on the objects, then you only need to check the pairs where a < b by the ordering. Could be anything: index in an array, pointer (yay for languages that do allow pointer value access).
for(Object a : list) {
for(Object b : list) {
if (a.compare(b) < 0) {
is very simple and will actually solve the problem.
If you have integer indexed storage, you can just do
for(int i = 0; i < list.length; i++) {
for(int j = i + 1; j < list.length; j++) {
and you won't get duplicates. This will work for ArrayList but probably not for arbitary types. You might be able to clone some iterators, but I wouldn't bet on that...
for(Iterator<Object> iter = list.iterator(); iter.hasNext(); ) {
Object a = iter.next();
Iterator<Object> iter2 = iter.clone();
for(;iter2.hasNext();) {
Object b = iter.next();
But seriously, that is a hack. I would be surprised if it works for all the java collections. A more reliable but just as hackish workaround with Java iterators:
for(Object a : list) {
Iterator<Object> biter = list.iter();
while(biter.next() != a) { };
for(; biter.hasNext(); ) {
Object b = biter.next();
In general, the java foreach syntax for(Clazz object : iterable) { is "cute", but much less powerful than Iterators. And in fact, the old integer for loop above works like a charm, too.