15
More on collections Lecture 8 Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria

Oop lecture8

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Oop lecture8

More on collections

Lecture 8

Object Oriented ProgrammingEastern University, Dhaka

Md. Raihan Kibria

Page 2: Oop lecture8

LinkedList

LinkedList is just like ArrayList; however, it was designed to make add/remove faster and efficient

Has these methods: addFirst AddLast removeFirst removeLast

Page 3: Oop lecture8

Who implements List interface

ArrayList LinkedList Vector Stack

Page 4: Oop lecture8

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

Page 5: Oop lecture8

Output

size of set: 40123size of list: 501233

We can see set did not add duplicate element

Page 6: Oop lecture8

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

Page 7: Oop lecture8

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

Page 8: Oop lecture8

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

Page 9: Oop lecture8

Inner classes

Next slide: a demo without using inner class

Page 10: Oop lecture8

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."); }}

Page 11: Oop lecture8

After Run the Program

After Clicking the Test Button

Page 12: Oop lecture8

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()); }}

Page 13: Oop lecture8

After Run the Program

After Clicking the Test Button for Once

Page 14: Oop lecture8

After Clicking the Test Button for Twice

After Clicking the Test Button for Thrice

Page 15: Oop lecture8

Advantages of using Inner class

Improves code-cohesionProvides convenience to the developer