Reputation: 2481
I have a row of buttons some of them are visible some not. So I want to sort these buttons in order they were added to a panel starting with visible ones.
So I created LinkedHashMap where I add buttons with true if it is visible and false otherwise. Then I created my comparator that sorts this Map by values (Boolean) and only after it I add my buttons to a panel from a TreeMap. But TreeMap is empty at the end.
Here is what I do:
private Map<Widget, Boolean> panelButtons = new HashMap<Widget, Boolean>();
private Map<Widget, Boolean> panelSortedButtons = new TreeMap<Widget, Boolean>(comparator);
private ButtonsComparator comparator = new ButtonsComparator(panelButtons);
//UI initialization: buttons are added using addToPanel() method - described below
for(Widget widget : panelSortedButtons.keySet()) { //panelSortedButtons is empty
panel.add(widget);
}
//----------------
//All buttons are added via this method
public void addToPanel(Widget widget, boolean isVisible) {
panelButtons.put(widget, isVisible);
}
//----------------
private class ButtonsComparator implements Comparator<Widget>{
Map<Widget, Boolean> buttons;
public ButtonsComparator(Map<Widget, Boolean> buttons) {
this.buttons = buttons;
}
@Override
public int compare(Widget widget1, Widget widget2) {
return buttons.get(widget1).compareTo(buttons.get(widget2));
}
}
What is missed if anything?
Updated
I understood my fault, I had to fill my TreeMap with data from HashMap. The same way as John B. showed.
panelSortedButtons.putAll(panelButtons);
Upvotes: 0
Views: 164
Reputation: 31368
You need to use the constructor TreeMap(Comparator<? super K> comparator)
, previously having defined the Comparator
. You are setting the comparator before even declaring it. This cannot compile.
Upvotes: 0
Reputation: 32969
You never added the Widgets
to the TreeMap
. You created an empty TreeMap
with a Comparator
but I don't see where you added any elements to that map.
EDIT:
Try this
//UI initialization
panelSortedButtons.putAll(panelButtons);
for(Widget widget : panelSortedButtons.keySet()) { //panelSortedButtons is empty
panel.add(widget);
}
Upvotes: 2
Reputation: 71019
The method compare
will never be used in the above code. Also when you implement Comparable
you should override the method compareTo
instead of just compare
.
Also I never see you actually add anything to the collection you have.
Upvotes: 1
Reputation: 533880
When you return 0 you say that two keys are equal. As Maps don't allow duplicates, returning 0 results in that duplicate being dropped. In your case you only have two options true
and false
so you will never have more than two keys.
However, you say the collection is "empty" which suggests it's not your comparator but you are not adding anything to the collection.
What it appear you want to do is
List<Widget> widgets = new ArrayList<Widget>(panelButtons.keySet());
Collections.sort(widget, comparator);
You can add the elements, with duplicates to a list like this, and then sort them without losing elements even if they are the "same"
Upvotes: 4