SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
Lecture 8
More on collections




           Object Oriented Programming
            Eastern 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: 4
                       0
                       1
                       2
                       3
                       size of list: 5
                       0
                       1
                       2
                       3
                       3
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 example
public 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
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{

@Override
public 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-cohesion
Provides convenience to the developer

Contenu connexe

Tendances

The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196Mahmoud Samir Fayed
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in SwiftSeongGyu Jo
 
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)진성 오
 
Python Dictionary
Python DictionaryPython Dictionary
Python DictionaryColin Su
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Rick Copeland
 
The Ring programming language version 1.3 book - Part 18 of 88
The Ring programming language version 1.3 book - Part 18 of 88The Ring programming language version 1.3 book - Part 18 of 88
The Ring programming language version 1.3 book - Part 18 of 88Mahmoud Samir Fayed
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonMarian Marinov
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1Giovanni Della Lunga
 
Grammatical Optimization
Grammatical OptimizationGrammatical Optimization
Grammatical Optimizationadil raja
 
Introduction to python programming 2
Introduction to python programming   2Introduction to python programming   2
Introduction to python programming 2Giovanni Della Lunga
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanWei-Yuan Chang
 
Elixir formatter Internals
Elixir formatter InternalsElixir formatter Internals
Elixir formatter InternalsPedro Medeiros
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...ssuserd6b1fd
 
Macrobrew: Clojure macros distilled
Macrobrew: Clojure macros distilledMacrobrew: Clojure macros distilled
Macrobrew: Clojure macros distilledabhinavomprakash10
 
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD  MAKING IN PAYTHON BY ROHIT MALAVNOTEPAD  MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAVRohit malav
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...ssuserd6b1fd
 

Tendances (20)

The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in Swift
 
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)
 
Python Dictionary
Python DictionaryPython Dictionary
Python Dictionary
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
ScalaFlavor4J
ScalaFlavor4JScalaFlavor4J
ScalaFlavor4J
 
The Ring programming language version 1.3 book - Part 18 of 88
The Ring programming language version 1.3 book - Part 18 of 88The Ring programming language version 1.3 book - Part 18 of 88
The Ring programming language version 1.3 book - Part 18 of 88
 
Objeck
ObjeckObjeck
Objeck
 
Begin with Python
Begin with PythonBegin with Python
Begin with Python
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
 
Grammatical Optimization
Grammatical OptimizationGrammatical Optimization
Grammatical Optimization
 
Introduction to python programming 2
Introduction to python programming   2Introduction to python programming   2
Introduction to python programming 2
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 
Elixir formatter Internals
Elixir formatter InternalsElixir formatter Internals
Elixir formatter Internals
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
Macrobrew: Clojure macros distilled
Macrobrew: Clojure macros distilledMacrobrew: Clojure macros distilled
Macrobrew: Clojure macros distilled
 
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD  MAKING IN PAYTHON BY ROHIT MALAVNOTEPAD  MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAV
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 

En vedette (9)

Oop lecture6
Oop lecture6Oop lecture6
Oop lecture6
 
Presentacion viernes 20 [compatibility mode]
Presentacion viernes 20 [compatibility mode]Presentacion viernes 20 [compatibility mode]
Presentacion viernes 20 [compatibility mode]
 
Calendario portada
Calendario portadaCalendario portada
Calendario portada
 
Cwgd
CwgdCwgd
Cwgd
 
Oop lecture2
Oop lecture2Oop lecture2
Oop lecture2
 
Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Oop lecture1
Oop lecture1Oop lecture1
Oop lecture1
 
Oop lecture9 12
Oop lecture9 12Oop lecture9 12
Oop lecture9 12
 

Similaire à Oop lecture8

I only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdfI only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdfarpitcomputronics
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Kuldeep Jain
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdffreddysarabia1
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1sotlsoc
 
Java programming Write a method- remove that takes three parameters- a.docx
Java programming Write a method- remove that takes three parameters- a.docxJava programming Write a method- remove that takes three parameters- a.docx
Java programming Write a method- remove that takes three parameters- a.docxolsenlinnea427
 
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdfalliedscorporation
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdfakkhan101
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdfarshin9
 
Program klik sederhana
Program klik sederhanaProgram klik sederhana
Program klik sederhanaHenfry Kai
 

Similaire à Oop lecture8 (20)

Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
I only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdfI only need help with four methods in the EmployeeManager class the .pdf
I only need help with four methods in the EmployeeManager class the .pdf
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
Java awt
Java awtJava awt
Java awt
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Java programming Write a method- remove that takes three parameters- a.docx
Java programming Write a method- remove that takes three parameters- a.docxJava programming Write a method- remove that takes three parameters- a.docx
Java programming Write a method- remove that takes three parameters- a.docx
 
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Java
JavaJava
Java
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
Awt
AwtAwt
Awt
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
 
T
TT
T
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
Program klik sederhana
Program klik sederhanaProgram klik sederhana
Program klik sederhana
 
Bar graph
Bar graphBar graph
Bar graph
 
54240326 copy
54240326   copy54240326   copy
54240326 copy
 
54240326 (1)
54240326 (1)54240326 (1)
54240326 (1)
 

Plus de Shahriar Robbani (7)

Group111
Group111Group111
Group111
 
SQL
SQLSQL
SQL
 
Oop lecture9 10
Oop lecture9 10Oop lecture9 10
Oop lecture9 10
 
Oop lecture4
Oop lecture4Oop lecture4
Oop lecture4
 
Oop lecture9
Oop lecture9Oop lecture9
Oop lecture9
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
 
Oop lecture3
Oop lecture3Oop lecture3
Oop lecture3
 

Dernier

How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfTechSoup
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationMJDuyan
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxDr. Asif Anas
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxSaurabhParmar42
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxKatherine Villaluna
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17Celine George
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphNetziValdelomar1
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapitolTechU
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfMohonDas
 

Dernier (20)

How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive Education
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptx
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptx
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a Paragraph
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptx
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdf
 

Oop lecture8

  • 1. Lecture 8 More on collections Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  • 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. Who implements List interface  ArrayList  LinkedList  Vector  Stack
  • 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. Output size of set: 4 0 1 2 3 size of list: 5 0 1 2 3 3 We can see set did not add duplicate element
  • 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. 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. HashMap example public 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. Inner classes Next slide: a demo without using inner class
  • 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{ @Override public void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(null, "Eggs are not supposed to be green."); } }
  • 11. After Run the Program After Clicking the Test Button
  • 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. After Run the Program After Clicking the Test Button for Once
  • 14. After Clicking the Test Button for Twice After Clicking the Test Button for Thrice
  • 15. Advantages of using Inner class Improves code-cohesion Provides convenience to the developer