Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConcurrentSkipListMap Sorting Using Comparator

I am using 'ConcurrentSkipListMap' (Since my environment will be multythreaded) and a 'Comparator' to sort the inserting object according to its Id & date. in the 'TestObject' its Name will be unique. so i used it as my 'key' in the map. 'Id' will be a arbitrary value. i need to sort my map according to the 'Id' and 'date' values.(if Id s are equal will sort it by date) for here i just add current date and gave the focus on the ID field. But map didn't return me the sorting order i expect.

public class SortTest {

private static final Comparator TEST_COMPARATOR = new TestComparator();
private static Map<String, TestObject> map = new ConcurrentSkipListMap<String, TestObject>();

private static class TestComparator<T> implements Comparator<TestObject> {

    @Override
    public int compare(TestObject o1, TestObject o2) {
        Integer x1 = o1.getId();
        Integer x2 = o2.getId();

        int Comp = x1.compareTo(x2);

        if (Comp != 0) {
            return Comp;
        } else {

            Date d1 = o1.getDate();
            Date d2 = o2.getDate();

            return d1.compareTo(d2);
        }
    }

}

public static void construct() {

    for (int i = 1; i <= 10; i++) {
        TestObject t = new TestObject();
        t.setId(i%3);
        t.setDate(new Date());
        t.setName("Obj_"+i);
        System.out.println(t);
        map.put(t.getName(),t);


    }

}
public static void main(String[] args) {
    SortTest x = new SortTest();
    x.construct();
    System.out.println(map);
}

}

And the ObjectClass is -

public class TestObject {

private String name;

private int id;

Date date;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

/* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}



/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return "TestObject [name=" + name + ", id=" + id + "]";
}

/* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    TestObject other = (TestObject) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

}

Out snippet is -

{Obj_1=TestObject [name=Obj_1, id=1], Obj_10=TestObject [name=Obj_10, id=1], Obj_2=TestObject [name=Obj_2, id=2], Obj_3=TestObject [name=Obj_3, id=0],......

Expected Order is -

Obj_3(Since Id =0 ),Obj_1,Obj_10, (Ids are =1 ), Obj_2 (Id =2)

Can anyone please point me out what i am doing wrong here? Thanks in advance.

like image 342
Sam Avatar asked Jul 12 '26 16:07

Sam


1 Answers

The map compares keys, not values. I.e. it is ordered in alphabet string order for keys.


P.S. also you need to initialize the map with the comparator as a constructor parameter. But in this case it won't work, as comparator should compare Strings (keys).

like image 102
Eugene Retunsky Avatar answered Jul 14 '26 05:07

Eugene Retunsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!