Reputation: 2455
I am working with Tree Set in java to add some objects in the set . Each object is a signal with different properties. Everytime a new signal comes I set some attributes of the object (Signal) and add it to the set, A signal can come more then one time. For signals I mantain a list in which I store the signal name and when I signal comes I check if the signal is in the list or not If it is not in the list I simply add a new object to my set but if the signal is in the list I take that signal from the set and modify some of its attributes , remove the signal and and add a new one.
The problem is that when I signal comes for the 2nd time or 3rd time and so on it does not remove the old signal and keep all the occurance of the same signal with modified attributes.
Here is my code
SortedSet<Signal> set = new TreeSet<Signal>();
ArrayList< String > messages = new ArrayList< String >();
//If the Message is new one
if(!messages.contains(messageName)){
//System.out.println("I am new signal");
//Add the new Message in Message List
messages.add(messageName);
//Create Object of Message to be stored in Set
signal = new Signal();
signal.setSource(messageSource);
signal.setName(messageName);
if(messageSource.equals("egdci") || messageSource.equals("ugdci"))
signal.setComponent(egdci_ugdci_msgComponent);
else
signal.setComponent(messageComponent);
signal.setOccurance(messageCounter);
signal.setSize(messageContent.length);
signal.setBandwidth(0F);
//Add the new Message Object in the Set
set.add(signal);
}
//If Message already exists in the list
else{
//System.out.println("I am old one");
Iterator<Signal> iterator = set.iterator();
while(iterator.hasNext()) {
signal = (Signal)iterator.next();
if(signal.getName().equalsIgnoreCase(messageName)){
System.out.println("I am here");
int occurance = signal.getOccurance() + 1;
int size = occurance * messageContent.length;
float bandwidth = 0F;
set.remove(signal);
signal = new Signal();
signal.setSource(messageSource);
signal.setName(messageName);
if(messageSource.equals("egdci") || messageSource.equals("ugdci"))
signal.setComponent(egdci_ugdci_msgComponent);
else
signal.setComponent(messageComponent);
signal.setOccurance(occurance);
signal.setSize(size);
signal.setBandwidth(bandwidth);
//Add the new Message Object in the Set
set.add(signal);
}
}
}
writeToCSV(signal , writer);
More over I try to sort my signals in the file on the basis of occurance of a signal, the higher the value of occurance, on the top it should be in the file.
Here is my code of compareTo in Signal.java (I am using Tree Set for the first time so Not sure how to implement my comapreTo)
@Override
public int compareTo(Signal signal) {
int thisOccurance = this.getOccurance();
return thisOccurance.compareTo(signal.getOccurance());
}
Any Help will be highly appreciated...
Thanks in advance
Upvotes: 0
Views: 655
Reputation: 3271
If the problem is only remove the old signal, you can use remove it with:
iterator.remove();
Once the while ends you can create and then add your new Signal to the set.
You else condition should look like:
else {
// System.out.println("I am old one");
final Iterator<Signal> iterator = set.iterator();
boolean deleted = false;
while (iterator.hasNext()) {
signal = (Signal)iterator.next();
if(signal.getName().equalsIgnoreCase(messageName)){
iterator.remove();
deleted = true;
break;
}
}
if(delted){
int occurance = signal.getOccurance() + 1;
int size = occurance * messageContent.length;
float bandwidth = 0F;
signal = new Signal();
signal.setSource(messageSource);
signal.setName(messageName);
if(messageSource.equals("egdci") || messageSource.equals("ugdci"))
signal.setComponent(egdci_ugdci_msgComponent);
else
signal.setComponent(messageComponent);
signal.setOccurance(occurance);
signal.setSize(size);
signal.setBandwidth(bandwidth);
//Add the new Message Object in the Set
set.add(signal);
}
}
Upvotes: 1