Lecture 8More on collections           Object Oriented Programming            Eastern University, Dhaka                   ...
LinkedList   LinkedList is just like ArrayList; however, it    was designed to make add/remove faster    and efficient  ...
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...
Output                       size of set: 4                       0                       1                       2       ...
public class SetDemo { public static void main(String[] args) {   Set<Integer>set = new HashSet<Integer>();   List<Integer...
Map interface Object to a map are key,value pairs For example,<dhaka, 02><chittagong, 031><sylhet, 032> Two most common...
HashMap examplepublic class MapDemo {  public static void main(String[] args) {    Map<String, Integer>map = new HashMap<S...
Inner classesNext slide: a demo without using inner class
public class ButtonListenerDemo {    public static void main(String[] args) {      JFrame jframe = new JFrame();      jfra...
After Run the ProgramAfter Clicking the Test Button
public class ButtonListenerDemo2 {    public static void main(String[] args) {      JFrame jframe = new JFrame();      jfr...
After Run the ProgramAfter Clicking the Test Button for              Once
After Clicking the Test Button for              TwiceAfter Clicking the Test Button for              Thrice
Advantages of using Inner classImproves code-cohesionProvides convenience to the developer
Upcoming SlideShare
Loading in …5
×

Oop lecture8

361 views
314 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
361
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Oop lecture8

  1. 1. Lecture 8More on collections Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  2. 2. LinkedList LinkedList is just like ArrayList; however, it was designed to make add/remove faster and efficient Has these methods: addFirst AddLast removeFirst removeLast
  3. 3. Who implements List interface ArrayList LinkedList Vector Stack
  4. 4. 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
  5. 5. Output size of set: 4 0 1 2 3 size of list: 5 0 1 2 3 3We can see set did not add duplicate element
  6. 6. 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
  7. 7. 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
  8. 8. 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: 31 null
  9. 9. Inner classesNext slide: a demo without using inner class
  10. 10. 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."); }}
  11. 11. After Run the ProgramAfter Clicking the Test Button
  12. 12. 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()); }}
  13. 13. After Run the ProgramAfter Clicking the Test Button for Once
  14. 14. After Clicking the Test Button for TwiceAfter Clicking the Test Button for Thrice
  15. 15. Advantages of using Inner classImproves code-cohesionProvides convenience to the developer

×