Ordering items in collection

Using this code I am able to order each item in collection:

import java.util.Iterator;

import java.util.TreeSet;

publicclass Itemimplements Comparable<Item>{

public Integer param1 = 0;

public Integer param2 = 0;

publicint compareTo(Item i){

return param1.compareTo(i.param1);

}

publicstaticvoid main(String[] args){

// collection

TreeSet<Item> mySet =new TreeSet<Item>();

// create items

Item i1 =new Item();

i1.param1 = 4;

i1.param2 = 4;

Item i2 =new Item();

i2.param1 = 3;

i2.param2 = 7;

Item i3 =new Item();

i3.param1 = 5;

i3.param2 = 7;

// add items

mySet.add(i1);

mySet.add(i2);

mySet.add(i3);

// output

for (Iterator<Item> iter = mySet.iterator(); iter.hasNext();){

Item element = iter.next();

System.out.println("ITEM: param1="+element.param1+" param2="+element.param2);

}

}

}

Please, could anyone give me some hint - I need to have for example this classes:

class TreeSetOrderByParam1{ ...}

and

class TreeSetOrderByParam2{ ...}

and I need inserted items will be ordered differently (according to name of its class).

Thanks in advance for any help.

Message was edited by:

radone

[2519 byte] By [radonea] at [2007-11-27 1:54:15]
# 1

Hello,

there appears to be some confusion here. You can't order the items in a set. There are basically three collection types in java.util: lists, sets and maps.

Lists are an ordered collection of objects. You have ArrayList, which is fast for access but slow for inserting and deleting objects. You have LinkedList, which is a bit slower for access, but much faster for inserting and deleting objects.

Sets are an unordered collection of objects. You have HashSet, which is fastest for access. You have TreeSet, which is a bit slower for access, but stores the objects in their natural order. However, you can't change the order!

Maps are for storing (key, value) pairs. You have HashMap, which is fastest for access and TreeMap, which stores the pairs according to the natural order of the keys.

If you want to order and reorder objects you have to use lists!

There are two ways for ordering objects. The first is that the objects implement the Comparable interface. The method

public int compareTo(Object o)

determines the natural order of the objects. The method returns a negative integer if this object is less than the object in the argument. The method returns 0 if this object is equal to the method in the argument. The method returns a positive integer if this object is greater than the object in the argument.

You can order the objects in a list according to their natural order with the method Collections.sort(List<T> list) in the package java.util.

If you want to define a custom order on your objects, you have to create an object that implements the Comparator<T> interface in the package java.util. This interface has two methods:

public int compare(T o1, T o2)

public boolean equals(Object obj)

The compare method returns a negative integer if o1 < o2, returns 0 if o1 == o2 and returns a positive integer if o1 > o2. The equals method returns true if and only if this comparator object is equal to the object in the argument. (The latter method makes only sense if you also include the comparator object in your list.)

You can order the objects in a list according to a custom order defined by a comparator with the method Collections.sort(List<T> list, Comparator<? super T> c) in the package java.util.

toon_macharisa at 2007-7-12 1:25:39 > top of Java-index,Core,Core APIs...
# 2
Just a thought, you may want to look at the TreeSet(Comparator<? super E> c) constructor and consider writing your own comparators, one for each relevant ordering.
OleVVa at 2007-7-12 1:25:40 > top of Java-index,Core,Core APIs...
# 3
There are ordered sets. TreeSet is one of them. Look into Comparators and Comparable.
_dnoyeBa at 2007-7-12 1:25:40 > top of Java-index,Core,Core APIs...
# 4

I am not absolutely sure about this but - I thought that each used Tree structure (Abstract Data Type) orders items when inserted according to Red-Black Trees (or AVL tree) algorithm.

Am I right?

What do I need is that items will be ordered not according to compareTo(...) but according to decision of choosen Tree structure.

Example:

radonea at 2007-7-12 1:25:40 > top of Java-index,Core,Core APIs...
# 5
Exactly, so see reply #2 again.
ejpa at 2007-7-12 1:25:40 > top of Java-index,Core,Core APIs...
# 6
Hi;I am having trouble coming up with a test code... to understand the use of TreeSet(Comparator<? super E> c). Could somebody please explain it with an example. Thanks for your help.
mjasa at 2007-7-12 1:25:40 > top of Java-index,Core,Core APIs...
# 7

Here is example of using comparator. Hope this help.

import java.util.Comparator;

import java.util.Iterator;

import java.util.TreeSet;

class Item {

public Integer param1 = new Integer(0);

public Integer param2 = new Integer(0);

}

public class Test {

public static void main(String[] args) {

Comparator<Item> cmp1 = new Comparator<Item>() {

@Override

public int compare(Item i1, Item i2) {

return i1.param1.compareTo(i2.param1);

}

};

TreeSet<Item> myTree1 = new TreeSet<Item>(cmp1);

Comparator<Item> cmp2 = new Comparator<Item>() {

@Override

public int compare(Item i1, Item i2) {

return i1.param2.compareTo(i2.param2);

}

};

TreeSet<Item> myTree2 = new TreeSet<Item>(cmp2);

Item item1 = new Item();

item1.param1 = 3;

item1.param2 = 6;

Item item2 = new Item();

item1.param1 = 4;

item1.param2 = 2;

myTree1.add(item1);

myTree1.add(item2);

myTree2.add(item1);

myTree2.add(item2);

for (Iterator<Item> it = myTree1.iterator(); it.hasNext();) {

Item item = it.next();

System.out.println("ITEM:"+item.param1+" "+item.param2);

}

for (Iterator<Item> it = myTree2.iterator(); it.hasNext();) {

Item item = it.next();

System.out.println("ITEM:"+item.param1+" "+item.param2);

}

}

}

radonea at 2007-7-12 1:25:40 > top of Java-index,Core,Core APIs...