Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HashMap Problem

Status
Not open for further replies.

ALongpwneage

Programmer
May 17, 2007
6
Trying to Return the Current and previous values in the HashMap, but is only returning the current keys. Any advise will be much appreciated. Thanks

The code:

private void saveJButtonActionPerformed( ActionEvent event )
{
int x = 0;
//Creates the tree map and the values that will be placed in the map
TreeMap hm = new TreeMap();
hm.put("Average Attendance ", subtotalJTextField.getText());
hm.put("1)",Name1JTextFeild.getText());
hm.put("Attendance", Members1JTextFeild.getText());
hm.put("2)",Name2JLabel.getText());
hm.put("Attendance", Members2JTextFeild.getText());
hm.put("3)",Name3JLabel.getText());
hm.put("Attendance", Members3JTextFeild.getText());
Set set = hm.entrySet();

Iterator i = set.iterator();


//displays the values in the map
while( i.hasNext())
{

Map.Entry me = (Map.Entry)i.next();
DisplayAttendance.setText(me.getKey() + " : " + me.getValue());
System.out.println(me.getKey() + " " + me.getValue());


}
 
Code:
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TestMap {
	   public static void main(String args[]) {
		    int x = 0;
		    //Creates the tree map and the values that will be placed in the map
		    TreeMap hm = new TreeMap();
		    hm.put("Average Attendance ", "zero");
		    hm.put("1)","one");
		    hm.put("Attendance", "two");
		    hm.put("2)","three");
		    hm.put("Attendance", "four");
		    hm.put("3)","five");
		    hm.put("Attendance", "six");
		    Set set = hm.entrySet();
		    
		    Iterator i = set.iterator();
		    
		    
		    //displays the values in the map
		    while( i.hasNext())
		        {		        
		        Map.Entry me = (Map.Entry)i.next();
		        System.out.println(me.getKey() + "  " + me.getValue());
		        }
	   }
}
The key "Attendance" has been used for triple times, so the value is replaced a new value at each time. "four" replaces "two", then "six" replaces "four".
 
This forum is for J2EE posts. In future please post pure Java questions to forum269.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top