Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a sorted object list in java? [duplicate]

Tags:

java

I have an object with the following memebers:

class XNode {
    // key
    long magicNumber;   

    // metadata:
    int x;
    iny y;
    int z;
    double angle;
    double tempreture;
}

I want to use order list (the key is magicNumber).

Is there a list in java which already implements the add/remove/update in a order manner ? (I dont want to use Collections.sort for every operation).

like image 446
Azil Avatar asked Mar 16 '23 21:03

Azil


1 Answers

no duplicates are allowed

First, in XNode there is a typo. I believe

iny y;

should be

int y;

Then you might use a SortedSet (for example TreeSet),

Comparator<XNode> comp = new Comparator<XNode>() {
    @Override
    public int compare(XNode o1, XNode o2) {
        return Long.valueOf(o1.magicNumber).compareTo(o2.magicNumber);
    }
};
SortedSet<XNode> set = new TreeSet<>(comp);
like image 197
Elliott Frisch Avatar answered Apr 01 '23 14:04

Elliott Frisch