oop lecture8
DESCRIPTION
TRANSCRIPT
More on collections
Lecture 8
Object Oriented ProgrammingEastern University, Dhaka
Md. Raihan Kibria
LinkedList
LinkedList is just like ArrayList; however, it was designed to make add/remove faster and efficient
Has these methods: addFirst AddLast removeFirst removeLast
Who implements List interface
ArrayList LinkedList Vector Stack
Sets
Examples are HashSet, TreeSet
Difference between Set and List A list can have duplicate objects Set cannot have duplicate objects See example in the next slide
Output
size of set: 40123size of list: 501233
We can see set did not add duplicate element
public class SetDemo {
public static void main(String[] args) { Set<Integer>set = new HashSet<Integer>(); List<Integer>list = new ArrayList<Integer>();
for (int i=0; i<4; i++){ set.add(new Integer(i)); list.add(new Integer(i)); }
set.add(3); list.add(3);
System.out.println("size of set: " + set.size()); for (int i=0; i<set.size(); i++) System.out.println(set.toArray()[i]);
System.out.println("size of list: " + list.size()); for (int i=0; i<list.size(); i++) System.out.println(list.get(i)); }}
Output in the following slide
Map interface
Object to a map are key,value pairs For example, <dhaka, 02><chittagong, 031><sylhet, 032> Two most common map implementations
are HashMap and TreeMap
HashMap examplepublic class MapDemo { public static void main(String[] args) { Map<String, Integer>map = new HashMap<String, Integer>(); map.put("Dhaka", 2); map.put("Chittagong", 31); map.put("Sylhet", 32);
System.out.println(map.get("Chittagong")); System.out.println(map.get("Comilla")); }}
Output is:31null
Inner classes
Next slide: a demo without using inner class
public class ButtonListenerDemo {
public static void main(String[] args) { JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setBounds(0, 0, 300, 200); jframe.setLayout(new FlowLayout());
JButton jbutton = new JButton("Test button"); jframe.add(jbutton); jbutton.addActionListener(new ButtonListener());
JTextField jtext = new JTextField(); jframe.getContentPane().add(jtext); jtext.setText("Hello"); jframe.setVisible(true); }}
class ButtonListener implements ActionListener{
@Overridepublic void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(null, "Eggs are not supposed to be green."); }}
After Run the Program
After Clicking the Test Button
public class ButtonListenerDemo2 {
public static void main(String[] args) { JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setBounds(0, 0, 300, 200); jframe.setLayout(new FlowLayout());
final JTextField jtext = new JTextField(); jframe.getContentPane().add(jtext); jtext.setText("Hello"); jframe.setVisible(true);
final int i=0; class MyButtonListener implements ActionListener{
int j = i; @Override public void actionPerformed(ActionEvent arg0) { jtext.setText(String.valueOf(j++)); } }
JButton jbutton = new JButton("Test button"); jframe.add(jbutton); jbutton.addActionListener(new MyButtonListener()); }}
After Run the Program
After Clicking the Test Button for Once
After Clicking the Test Button for Twice
After Clicking the Test Button for Thrice
Advantages of using Inner class
Improves code-cohesionProvides convenience to the developer