2

I need a "Container" for a few objects. Definition:

class DataSet implements Comparable {
    public int id;
    public String Date;
    public double Value

    public DataSetFSA (int id, String Date) {
        this.id=id;
        this.Date=Date;
    }
    @Override
    public int compareTo(Object o) {
    return  1;
    }
}

The objects have a certain order, which should not be destroyed! As you see my container class already has an "id" itself!

So my question is now should i insert the object as a key or a value into the TreeMap?

DataSet ds1 = new DataSetFSA(1,"Val1"); 
DataSet ds2 = new DataSetFSA(2,"Val2"); 
...

TreeMap DataTreeMap = new TreeMap();
        DataTreeMap.put(1,ds1); //as value
        DataTreeMap.put(2,ds2);

//or as key (then the class has to implement comparable)
        DataTreeMap.put(ds1,1); //as key but then the value doesnt has a function anymore
        DataTreeMap.put(ds2,2);
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Che Veyo
  • 375
  • 3
  • 14
  • Why are you using a `TreeMap` at all? What do you intend to do with it? – Oliver Charlesworth May 10 '15 at 17:03
  • Why does your `compareTo` method always return 1? Are you looking to preserve the insertion order? – Chetan Kinger May 10 '15 at 17:08
  • Well, i don't understand what you mean by: "as key but then the value doesnt has a function anymore". Also, your implementation of method 'compareTo' is 'a bit' invalid ;) – Rafal G. May 10 '15 at 17:09
  • I am reading some data from a json file (containing measurements from a scientific experiment) and for each line i create a new DataSet to gather them in a TreeMap. The plan is to run some calculations on it – Che Veyo May 10 '15 at 17:12
  • Exactly i am trying to preserve the insertion order! I know its a little bit invalid ;) but it was working^^ – Che Veyo May 10 '15 at 17:14

1 Answers1

0

If you're just looking for a sorted (unique) container, there's no reason to use a TreeMap - a TreeSet would seem to fit the bill perfectly.

Mureinik
  • 297,002
  • 52
  • 306
  • 350