L
Larry Coon
Is there a convenient & clever way to get a key
from a Map given a value (as opposed to the usual
getting a value given a key)?
I'm using a TreeMap, where the keys are Dates and the
values are Strings which represent the dates. I have
a JComboBox with the Strings. The JComboBox needs to
display the Strings in Date order, which is why I'm
using the Dates as the keys in the TreeMap -- the
TreeMap orders on the keys.
Here's a short example to illustrate what I'm doing.
Where the println() is printing "???" I want the key
associated with the selected item. Is there a better
way than iterating through the entire Map?
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
public class TreeMapTest extends JFrame
implements ActionListener {
private JComboBox comboBox;
public TreeMapTest() {
super("TreeMapTest");
Container container = getContentPane();
container.setLayout(new GridLayout(2, 1, 0, 10));
Map map = buildMap();
comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(map.values().toArray()));
container.add(comboBox);
JButton goButton = new JButton("Go");
goButton.addActionListener(this);
container.add(goButton);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String selectedValue = comboBox.getSelectedItem().toString();
System.out.println("Selected value = " + selectedValue);
// Here is where I want to get the key for the selected value.
System.out.println("Selected key = ???");
}
private Map buildMap() {
Map result = new TreeMap();
SimpleDateFormat df =
new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
try {
result.put(df.parse("10/1/2003"), "Fall '03");
result.put(df.parse("4/1/2004"), "Spring '04");
result.put(df.parse("10/1/2004"), "Fall '04");
result.put(df.parse("4/1/2005"), "Spring '05");
}
catch (ParseException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
new TreeMapTest();
}
}
from a Map given a value (as opposed to the usual
getting a value given a key)?
I'm using a TreeMap, where the keys are Dates and the
values are Strings which represent the dates. I have
a JComboBox with the Strings. The JComboBox needs to
display the Strings in Date order, which is why I'm
using the Dates as the keys in the TreeMap -- the
TreeMap orders on the keys.
Here's a short example to illustrate what I'm doing.
Where the println() is printing "???" I want the key
associated with the selected item. Is there a better
way than iterating through the entire Map?
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
public class TreeMapTest extends JFrame
implements ActionListener {
private JComboBox comboBox;
public TreeMapTest() {
super("TreeMapTest");
Container container = getContentPane();
container.setLayout(new GridLayout(2, 1, 0, 10));
Map map = buildMap();
comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(map.values().toArray()));
container.add(comboBox);
JButton goButton = new JButton("Go");
goButton.addActionListener(this);
container.add(goButton);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String selectedValue = comboBox.getSelectedItem().toString();
System.out.println("Selected value = " + selectedValue);
// Here is where I want to get the key for the selected value.
System.out.println("Selected key = ???");
}
private Map buildMap() {
Map result = new TreeMap();
SimpleDateFormat df =
new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
try {
result.put(df.parse("10/1/2003"), "Fall '03");
result.put(df.parse("4/1/2004"), "Spring '04");
result.put(df.parse("10/1/2004"), "Fall '04");
result.put(df.parse("4/1/2005"), "Spring '05");
}
catch (ParseException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
new TreeMapTest();
}
}