Java Hashmap

November 13, 2009 · Print This Article

Pet.java

JAVA:
  1. public class Pet {
  2.    private String name;
  3.    private int age;
  4.  
  5.    public String getName()
  6.    {
  7.       return name;
  8.    }
  9.    public void setName(String name)
  10.    {
  11.       this.name = name;
  12.    }
  13.    public int getAge()
  14.    {
  15.        return age;
  16.    }
  17.  
  18.    public void setAge(int age)
  19.    {
  20.       this.age = age;
  21.    }
  22. }

Apps.java

JAVA:
  1. import java.awt.Component;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import java.util.ListIterator;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTextArea;
  19. import javax.swing.JTextField;
  20.  
  21.  
  22. public class Apps extends JFrame
  23. {
  24.    private JButton jbsave;
  25.    private JButton jbdisplay;
  26.    private JTextField name;
  27.    private JTextField age;
  28.    private JTextArea ta;
  29.    private HashMap<String,Integer> hm;
  30.    private List <Pet> lm;
  31.    private JScrollPane jscroll;
  32.  
  33.    public Apps()
  34.    {
  35.       init();
  36.       this.setLayout(null);
  37.       this.setVisible(true);
  38.       this.setBounds(10,20,400,300);
  39.  
  40.       hm = new HashMap<String,Integer>();
  41.       lm = new ArrayList<Pet>();
  42. }
  43.  
  44. private void init()
  45. {
  46.    jbsave = new JButton();
  47.    jbdisplay = new JButton();
  48.    name = new JTextField();
  49.    age = new JTextField();
  50.    ta = new JTextArea();
  51.    jscroll = new JScrollPane();
  52.  
  53.    addComponents(name,40,40,100,25);
  54.    addComponents(age,40,70,50,25);
  55.    addComponents(jbsave,40,100,70,25);
  56.    addComponents(jbdisplay,120,100,100,25);
  57.    addComponents(jscroll,40,160,150,100);
  58.  
  59.    jscroll.getViewport().add(ta);
  60.    jbsave.setText("Save");
  61.    jbdisplay.setText("Display");
  62.    jbsave.addActionListener(new ActionListener() {
  63.       public void actionPerformed(ActionEvent e)
  64.      {
  65.         jbsave_actionPerformed(e);
  66.      }
  67.   });
  68.  
  69.    jbdisplay.addActionListener(new ActionListener()
  70.    {
  71.       public void actionPerformed(ActionEvent e)
  72.       {
  73.          try
  74.         {
  75.             jbdispaly_actionPerformed(e);
  76.         }
  77.         catch (IOException e1)
  78.         {
  79.            e1.printStackTrace();
  80.         }
  81.    }
  82.    });
  83. }
  84.  
  85. private void jbdispaly_actionPerformed(ActionEvent e) throws IOException
  86. {
  87.     String key;
  88.     ta.setText("HashMap size : " + hm.size());
  89.     Iterator<String> iterator = hm.keySet().iterator();
  90.  
  91.     while( iterator. hasNext() )
  92.     {
  93.        key = iterator.next();
  94.        ta.append("\n"+key + " , " + hm.get(key));
  95.     }
  96.  
  97.     Pet p;
  98.     ta.append("\nList size : " + lm.size());
  99.     ListIterator <Pet>li = lm.listIterator();
  100.  
  101.      while(li.hasNext())
  102.     {
  103.        p = (Pet) li.next();
  104.        ta.append("\n" + p.getName() + " , " + p.getAge());
  105.     }
  106. }
  107.    private void jbsave_actionPerformed(ActionEvent e)
  108.    {
  109.        Pet p = new Pet();
  110.        int i_age;
  111.        p.setName(name.getText());
  112.        i_age = Integer.parseInt(age.getText());
  113.        p.setAge(i_age);
  114.  
  115.        hm.put(name.getText(), i_age);
  116.        lm.add(p);
  117. }
  118.  
  119.    private void addComponents(Component comp, int x,int y,int width, int height)
  120.    {
  121.       this.add(comp);
  122.       comp.setBounds(x, y, width, height);
  123.    }
  124. }

Main.java

JAVA:
  1. public class Main
  2. {
  3.    public static void main(String[] args)
  4.    {
  5.       new Apps();
  6.     }
  7. }

Random Posts

Comments

Got something to say?