Question

I am using a Tree Set to store some Signals as objects in my Tree Set and also want to update an object If the same signal comes again. So far I tried something but the problem is I am not able to get the complete object when I try to print it and Secondly I don't know if there is any way to update an abject and save it back to the set... Here is my code Signal Class (Signal.java)

public class Signal implements Comparable<Signal>{

   String source;
   String name;
   int occurance;

   public void setSource(String source){
       this.source = source;
   }

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

   public void setOccurance(int occurance){
       this.occurance = occurance;
   }


   public String getSource(){
       return this.source;
   }

   public String getName(){
       return this.name;
   }

   public int getOccurnace(){
       return this.occurance;
   }

@Override
public int compareTo(Signal arg0) {
    // TODO Auto-generated method stub
    return 0;
}
}

My Main Class

  public class SortedSetTest {

   public static void main(String[] args) {

      // Create the sorted set
      SortedSet<Signal> set = new TreeSet<Signal>();

      //Create a Signal object for each new signal
      Signal sig = new Signal();

      sig.setSource("Source");
      sig.setName("Signal Name");
      sig.setOccurance(1);

      // Add elements to the set
      set.add(sig);
      System.out.println(set); 

The above print show me as object...How Can I see the set as String?

      // Iterating over the elements in the set
      Iterator<Signal> it = set.iterator();
      while (it.hasNext()){

Here I want to print each object For example take the first object and print the object (Signal) Source, Name and occurance and so on till the end of the set reaches.
} } }

Was it helpful?

Solution

The lines you're looking for:

    while (it.hasNext()){
        Signal sig = (Signal)it.next();
        System.out.println(sig.getName());
        System.out.println(sig.getOccurance());
        // do the same with source or whatever property 
    }

OTHER TIPS

You need to override toString in your Signal object to obtain a string that is more user-friendly than the default implementation in the Object class.

Also note that because all your Signals are equal based on your implementation of compareTo (always return 0) you won't be able to add more than one Signal to your set.

You are correctly printing the set. You just haven't implemented the Signal.toString() method to represent a Signal object the way you want it.

About updating objects in a TreeSet: the object must not be modified in such a way that the output of compareTo is changed. If it needs to be updated in this way, remove it (using Set.remove(object), update it and add it back.

That said, your compareTo() method always returns 0. It should return arg0.occurence - occurence if you want higher occurence values to come before lower ones. This is because returning a value less than 0 means this comes before the argument. 0 means equal ordering and > 1 means this comes after the argument.

Also, if you implement compareTo() you define the natural ordering of the class. It is strongly recommended to also override equals() and return true if and only if compareTo() returns 0. See http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html (search for 'consistent with equals'). With the above implementation of compareTo(), this would mean returning true only when they both have the same occurence value. If you don't want to implement equals this way (it is probably not correct) then you should write a class that implements Comparator<Signal> and pass that to the TreeSet constructor.

First of all you need to override toString method,insteadof using mutator methods try to use a constructor which implements all your instance variables and the third thing is TreeSet uses a Red-Black tree structure,and guarantees that the elements will be ascending order,according to the natural order.So in compareTo() method you can compare one of your variables to get the solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top