Sprting a HashSet by propert

Hi all,I want to sort a Set by a property that is in the objects of the set. The property is a String. I searched around the net but could find anything that would relate to my problem.Greets
[212 byte] By [S1lv3ra] at [2007-11-27 8:06:00]
# 1
Turn it into a List then call http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)
ejpa at 2007-7-12 19:48:32 > top of Java-index,Core,Core APIs...
# 2

I turned it into a list...

But cant get the Collection.Sort good

this is the code:

package nl.wag.webarm.utils;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

import nl.wag.webarm.model.FactuurOnderdeel;

public class FactuurUtils {

public static void createFactuur (Set onderdelen){

if (onderdelen != null){

List onderdelenlijst = new ArrayList();

Iterator it = onderdelen.iterator();

while ( it.hasNext()){

FactuurOnderdeel fo = (FactuurOnderdeel) it.next();

onderdelenlijst.add(fo);

}

Collections.sort(onderdelenlijst,new OdComparator()); // this is where eclipse shows the warnings.. warnings are on the bottom of this post..

}

}

public class OdComparator implements Comparator{

public int compare(Object arg0, Object arg1) {

String s1 = ((FactuurOnderdeel) arg0).getId();

String s2 = ((FactuurOnderdeel) arg1).getId();

return s1.compareTo(s2);

}

}

}

And this is the warings eclipse gives:

Multiple markers at this line

- Type safety: The expression of type FactuurUtils.OdComparator needs unchecked

conversion to conform to Comparator<? super T>

- No enclosing instance of type FactuurUtils is accessible. Must qualify the allocation with

an enclosing instance of type FactuurUtils (e.g. x.new A() where x is an instance of FactuurUtils).

- Type safety: Unchecked invocation sort(List, Comparator) of the generic method sort

(List<T>, Comparator<? super T>) of type Collections

- Type safety: The expression of type List needs unchecked conversion to conform to

List<T>

S1lv3ra at 2007-7-12 19:48:32 > top of Java-index,Core,Core APIs...
# 3

> public static void createFactuur (Set<FactuurOnderdeel> onderdelen){

public static void createFactuur (Set onderdelen){

> if (onderdelen != null){

> List onderdelenlijst = new ArrayList();

List<FactuurOnderdeel> onderdelenlijst = new ArrayList<FactuurOnderdeel>(onderdelen);

and remove the iterator copy-loop following.

> public class OdComparator implements Comparator{

public static class OdComparator implements Comparator<FactuurOnderdeel>{

> public int compare(Object arg0, Object arg1) {

> String s1 = ((FactuurOnderdeel) arg0).getId();

> String s2 = ((FactuurOnderdeel) arg1).getId();

public int compare(FactuurOnderdeel arg0, FactuurOnderdeel arg1) {

String s1 = arg0.getId();

String s2 = arg1.getId();

ejpa at 2007-7-12 19:48:32 > top of Java-index,Core,Core APIs...
# 4
Ok then,This is very clear.. thank you..There is only one more problem.. when there is more than one object in the list i get a nullpointer at : Collections.sort(onderdelenlijst,new OdComparator()); any ideas?
S1lv3ra at 2007-7-12 19:48:32 > top of Java-index,Core,Core APIs...
# 5
You have a null element in the Set?
ejpa at 2007-7-12 19:48:32 > top of Java-index,Core,Core APIs...
# 6
Lol... i found it.. i use hibernate.. and the to be sorted elements are not yet saved. so the elements do not have an id. ill try to sort them on another property.. ill give an update when it works..
S1lv3ra at 2007-7-12 19:48:32 > top of Java-index,Core,Core APIs...
# 7
ok it works.Thanks for youre time.Grab some dukes..Later!
S1lv3ra at 2007-7-12 19:48:32 > top of Java-index,Core,Core APIs...