tags:

views:

43

answers:

1

Who can tell me how to use Distinct operation on db4o in Java code. I couldn't find any example in Java.

Thanks!

+3  A: 

Afaik, there's no build in distinct-operator. You need to use a work-around. For example you can use a Set for the distinct-operation:

Distinct by the equality of the objects. When your class has a proper equals()- and hashCode()-Implementation, this works wonderful:

ObjectSet<TestClass> result = container.query(TestClass.class);
// will use the equals-method of TestClass.
Set<TestClass> distinctResult = new HashSet<TestClass>(result);

Distinct by a certain field. Useful when your want to make it distinct by a certain field.

ObjectSet<TestClass> result = container.query(TestClass.class);
Set<TestClass> distinctResultByField = new TreeSet<TestClass>(new Comparator<TestClass>() {
    public int compare(TestClass o1, TestClass o2) {
       return o1.getTestField().compareTo(o2.getTestField());
    }
});
distinctResultByField.addAll(result);
Gamlor