SlideShare a Scribd company logo
1 of 32
Trinity College



          Java Review Part II
                           Timothy Richards




    Trinity College, Hartford CT • Department of Computer Science • CPSC 225
Java API
    The entire Java 1.6 API documentation
           is available on the web
http://download.oracle.com/javase/6/docs/api/

                              Use It!




 Trinity College, Hartford CT • Department of Computer Science • CPSC 225   2
java.util
     Provides off-the-shelf data structures
      that you can use in your programs




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   3
java.util
     Provides off-the-shelf data structures
      that you can use in your programs

       List                      Map                     Set




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   4
java.util
     Provides off-the-shelf data structures
      that you can use in your programs

       List                      Map                     Set



                 These are not classes!




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   5
java.util
     Provides off-the-shelf data structures
      that you can use in your programs

       List                      Map                     Set



                    They are interfaces




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   6
java.util
     Provides off-the-shelf data structures
      that you can use in your programs

       List                      Map                     Set



                    They are interfaces
                   What’s an interface?




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   7
java.util
     Provides off-the-shelf data structures
      that you can use in your programs

       List                      Map                     Set

  LinkedList                  HashMap                 HashSet

   ArrayList                Hashtable             LinkedHashSet

      Stack                Properties                 TreeSet

     Vector                   TreeMap

      Queue

                      Class Implementations

Trinity College, Hartford CT • Department of Computer Science • CPSC 225   8
java.util
     Provides off-the-shelf data structures
      that you can use in your programs

       List                      Map                     Set

  LinkedList                  HashMap                 HashSet

   ArrayList                Hashtable             LinkedHashSet

      Stack                Properties                 TreeSet

     Vector                   TreeMap

      Queue

                      Class Implementations

Trinity College, Hartford CT • Department of Computer Science • CPSC 225   9
Generics
             How do I use a HashMap?
              HashMap m = new HashMap();
              m.put(“Tim”, 5);
              m.get(“Tim”);




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   10
Generics
             How do I use a HashMap?
              HashMap m = new HashMap();
              m.put(“Tim”, 5);
              m.get(“Tim”);




                         This is OK...




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   11
Generics
                   How do I use a HashMap?
                    Map m = new HashMap();
                    m.put(“Tim”, 5);
                    m.get(“Tim”);


Here we use
an interface                 This is better...




      Trinity College, Hartford CT • Department of Computer Science • CPSC 225   12
Generics
                   How do I use a HashMap?
                    Map m = new HashMap();
                    m.put(“Tim”, 5);
                    m.get(“Tim”);


Here we use
an interface                 This is better...

                            It separates the
                          interface from the
                            implementation

      Trinity College, Hartford CT • Department of Computer Science • CPSC 225   13
Generics
             How do I use a HashMap?
              Map m = new HashMap();
              m.put(“Tim”, 5);
              m.get(“Tim”);




                           Question?

                 What does the call to
                 this method return?


Trinity College, Hartford CT • Department of Computer Science • CPSC 225   14
Generics
             How do I use a HashMap?
              Map m = new HashMap();
              m.put(“Tim”, 5);

              Object r = m.get(“Tim”);



                           Question?

                  It returns an Object!



Trinity College, Hartford CT • Department of Computer Science • CPSC 225   15
Generics
             How do I use a HashMap?
              Map m = new HashMap();
              m.put(“Tim”, 5);

              Object r = m.get(“Tim”);
              int i = (Integer) r;


                           Question?

               That means we need to
               “cast” it to the correct
                         type...

Trinity College, Hartford CT • Department of Computer Science • CPSC 225   16
Generics
             How do I use a HashMap?
              Map m = new HashMap();
              m.put(“Tim”, 5);

              Object r = m.get(“Tim”);
              int i = (Integer) r;


                           Question?

                But, I thought it was an
                           int?


Trinity College, Hartford CT • Department of Computer Science • CPSC 225   17
Generics
             How do I use a HashMap?
              Map m = new HashMap();
              m.put(“Tim”, 5);

              Object r = m.get(“Tim”);
              int i = (Integer) r;


                           Question?

Yes, but Java is clever enough to know that if you
need an int, an Integer will be “converted” to an
                 int, and vice versa.

Trinity College, Hartford CT • Department of Computer Science • CPSC 225   18
Generics
             How do I use a HashMap?
              Map m = new HashMap();
              m.put(“Tim”, 5);

              Object r = m.get(“Tim”);
              int i = (Integer) r;                        This Works!
                                                   int x = new Integer(5);
                           Question?               Integer y = x;


Yes, but Java is clever enough to know that if you
need an int, an Integer will be “converted” to an
                 int, and vice versa.

Trinity College, Hartford CT • Department of Computer Science • CPSC 225   19
Generics
             How do I use a HashMap?
              Map m = new HashMap();
              m.put(“Tim”, 5);

              Object r = m.get(“Tim”);
              int i = (Integer) r;




So, does this mean I need to do this every time I
           get something from a Map?
       Yes... unless you use generic types!


Trinity College, Hartford CT • Department of Computer Science • CPSC 225   20
Generics
             How do I use a HashMap?
              Map m = new HashMap();
              m.put(“Tim”, 5);

              Object r = m.get(“Tim”);
              int i = (Integer) r;




                   What is a generic type?




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   21
Generics
                 How do I use a HashMap?
Map<String,Integer> m = new HashMap<String,Integer>();
m.put(“Tim”, 5);

Integer r = m.get(“Tim”);
int i = (Integer) r;                              No longer necessary


                       What is a generic type?




    Trinity College, Hartford CT • Department of Computer Science • CPSC 225   22
Generics
                 How do I use a HashMap?
Map<String,Integer> m = new HashMap<String,Integer>();
m.put(“Tim”, 5);

Integer r = m.get(“Tim”);
int i = (Integer) r;                              No longer necessary


    All the Java util classes can be used in this way.
                      For example...



    Trinity College, Hartford CT • Department of Computer Science • CPSC 225   23
ForEach Loop
                Now, a different topic...




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   24
ForEach Loop
                Now, a different topic...

     We know about one kind of for loop
             bounded iteration




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   25
ForEach Loop
                Now, a different topic...

     We know about one kind of for loop
             bounded iteration

        I mentioned there was another...




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   26
ForEach Loop
         Imagine we want to iterate over
              the elements in a list

        List<String> x = new ArrayList<String>();
        x.add(“Tom”);
        x.add(“Jerry”);




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   27
ForEach Loop
         Imagine we want to iterate over
              the elements in a list

        List<String> x = new ArrayList<String>();
        x.add(“Tom”);
        x.add(“Jerry”);

        for (int i = 0; i < s.size(); i++)
          s.get(i);



         Here is one way to achieve that


Trinity College, Hartford CT • Department of Computer Science • CPSC 225   28
ForEach Loop
         Imagine we want to iterate over
              the elements in a list

        List<String> x = new ArrayList<String>();
        x.add(“Tom”);
        x.add(“Jerry”);

        Iterator<String> i = x.iterator();
        while (i.hasNext())
          i.next()


     Here is another way to achieve that


Trinity College, Hartford CT • Department of Computer Science • CPSC 225   29
ForEach Loop
         Imagine we want to iterate over
              the elements in a list

        List<String> x = new ArrayList<String>();
        x.add(“Tom”);
        x.add(“Jerry”);

        for (String e : x)
          // do something with e...



  Here is the right way to achieve that


Trinity College, Hartford CT • Department of Computer Science • CPSC 225   30
ForEach Loop
         Imagine we want to iterate over
              the elements in a list

        List<String> x = new ArrayList<String>();
        x.add(“Tom”);
        x.add(“Jerry”);

        for (String e : x)
          // do something with e...



  Here is the right way to achieve that
           for each string e in x...

Trinity College, Hartford CT • Department of Computer Science • CPSC 225   31
Any Questions?


           We covered quite a bit of Java
            in the past two lectures...

      Does anyone have any questions?




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   32

More Related Content

What's hot

This Week in Machine Learning and AI Feb 2019
This Week in Machine Learning and AI Feb 2019This Week in Machine Learning and AI Feb 2019
This Week in Machine Learning and AI Feb 2019Charles Martin
 
Inventory theory presentation
Inventory theory presentationInventory theory presentation
Inventory theory presentationkun shin
 
用 Python 玩 LHC 公開數據
用 Python 玩 LHC 公開數據用 Python 玩 LHC 公開數據
用 Python 玩 LHC 公開數據Yuan CHAO
 
Asynchronous Stochastic Optimization, New Analysis and Algorithms
Asynchronous Stochastic Optimization, New Analysis and AlgorithmsAsynchronous Stochastic Optimization, New Analysis and Algorithms
Asynchronous Stochastic Optimization, New Analysis and AlgorithmsFabian Pedregosa
 
CHI 2014 talk by Antti Oulasvirta: Automated Nonlinear Regression Modeling fo...
CHI 2014 talk by Antti Oulasvirta: Automated Nonlinear Regression Modeling fo...CHI 2014 talk by Antti Oulasvirta: Automated Nonlinear Regression Modeling fo...
CHI 2014 talk by Antti Oulasvirta: Automated Nonlinear Regression Modeling fo...Aalto University
 
Hadoop for Data Science: Moving from BI dashboards to R models, using Hive st...
Hadoop for Data Science: Moving from BI dashboards to R models, using Hive st...Hadoop for Data Science: Moving from BI dashboards to R models, using Hive st...
Hadoop for Data Science: Moving from BI dashboards to R models, using Hive st...huguk
 
Snm Tauctv
Snm TauctvSnm Tauctv
Snm TauctvFNian
 
Scikit-Learn in Particle Physics
Scikit-Learn in Particle PhysicsScikit-Learn in Particle Physics
Scikit-Learn in Particle PhysicsGilles Louppe
 

What's hot (10)

An Evaluation of Models for Runtime Approximation in Link Discovery
An Evaluation of Models for Runtime Approximation in Link DiscoveryAn Evaluation of Models for Runtime Approximation in Link Discovery
An Evaluation of Models for Runtime Approximation in Link Discovery
 
This Week in Machine Learning and AI Feb 2019
This Week in Machine Learning and AI Feb 2019This Week in Machine Learning and AI Feb 2019
This Week in Machine Learning and AI Feb 2019
 
Inventory theory presentation
Inventory theory presentationInventory theory presentation
Inventory theory presentation
 
用 Python 玩 LHC 公開數據
用 Python 玩 LHC 公開數據用 Python 玩 LHC 公開數據
用 Python 玩 LHC 公開數據
 
Asynchronous Stochastic Optimization, New Analysis and Algorithms
Asynchronous Stochastic Optimization, New Analysis and AlgorithmsAsynchronous Stochastic Optimization, New Analysis and Algorithms
Asynchronous Stochastic Optimization, New Analysis and Algorithms
 
CHI 2014 talk by Antti Oulasvirta: Automated Nonlinear Regression Modeling fo...
CHI 2014 talk by Antti Oulasvirta: Automated Nonlinear Regression Modeling fo...CHI 2014 talk by Antti Oulasvirta: Automated Nonlinear Regression Modeling fo...
CHI 2014 talk by Antti Oulasvirta: Automated Nonlinear Regression Modeling fo...
 
Hadoop for Data Science: Moving from BI dashboards to R models, using Hive st...
Hadoop for Data Science: Moving from BI dashboards to R models, using Hive st...Hadoop for Data Science: Moving from BI dashboards to R models, using Hive st...
Hadoop for Data Science: Moving from BI dashboards to R models, using Hive st...
 
Thesis presentation
Thesis presentationThesis presentation
Thesis presentation
 
Snm Tauctv
Snm TauctvSnm Tauctv
Snm Tauctv
 
Scikit-Learn in Particle Physics
Scikit-Learn in Particle PhysicsScikit-Learn in Particle Physics
Scikit-Learn in Particle Physics
 

More from University of Massachusetts Amherst (7)

Community College Day Spring 2013
Community College Day Spring 2013Community College Day Spring 2013
Community College Day Spring 2013
 
Freshmen Advising Spring 2013
Freshmen Advising Spring 2013Freshmen Advising Spring 2013
Freshmen Advising Spring 2013
 
Basic SQL Part 2
Basic SQL Part 2Basic SQL Part 2
Basic SQL Part 2
 
Markup Languages
Markup LanguagesMarkup Languages
Markup Languages
 
Lecture 07 - Basic SQL
Lecture 07 - Basic SQLLecture 07 - Basic SQL
Lecture 07 - Basic SQL
 
Lecture 06
Lecture 06Lecture 06
Lecture 06
 
java review
java reviewjava review
java review
 

Java review-2

  • 1. Trinity College Java Review Part II Timothy Richards Trinity College, Hartford CT • Department of Computer Science • CPSC 225
  • 2. Java API The entire Java 1.6 API documentation is available on the web http://download.oracle.com/javase/6/docs/api/ Use It! Trinity College, Hartford CT • Department of Computer Science • CPSC 225 2
  • 3. java.util Provides off-the-shelf data structures that you can use in your programs Trinity College, Hartford CT • Department of Computer Science • CPSC 225 3
  • 4. java.util Provides off-the-shelf data structures that you can use in your programs List Map Set Trinity College, Hartford CT • Department of Computer Science • CPSC 225 4
  • 5. java.util Provides off-the-shelf data structures that you can use in your programs List Map Set These are not classes! Trinity College, Hartford CT • Department of Computer Science • CPSC 225 5
  • 6. java.util Provides off-the-shelf data structures that you can use in your programs List Map Set They are interfaces Trinity College, Hartford CT • Department of Computer Science • CPSC 225 6
  • 7. java.util Provides off-the-shelf data structures that you can use in your programs List Map Set They are interfaces What’s an interface? Trinity College, Hartford CT • Department of Computer Science • CPSC 225 7
  • 8. java.util Provides off-the-shelf data structures that you can use in your programs List Map Set LinkedList HashMap HashSet ArrayList Hashtable LinkedHashSet Stack Properties TreeSet Vector TreeMap Queue Class Implementations Trinity College, Hartford CT • Department of Computer Science • CPSC 225 8
  • 9. java.util Provides off-the-shelf data structures that you can use in your programs List Map Set LinkedList HashMap HashSet ArrayList Hashtable LinkedHashSet Stack Properties TreeSet Vector TreeMap Queue Class Implementations Trinity College, Hartford CT • Department of Computer Science • CPSC 225 9
  • 10. Generics How do I use a HashMap? HashMap m = new HashMap(); m.put(“Tim”, 5); m.get(“Tim”); Trinity College, Hartford CT • Department of Computer Science • CPSC 225 10
  • 11. Generics How do I use a HashMap? HashMap m = new HashMap(); m.put(“Tim”, 5); m.get(“Tim”); This is OK... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 11
  • 12. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); m.get(“Tim”); Here we use an interface This is better... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 12
  • 13. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); m.get(“Tim”); Here we use an interface This is better... It separates the interface from the implementation Trinity College, Hartford CT • Department of Computer Science • CPSC 225 13
  • 14. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); m.get(“Tim”); Question? What does the call to this method return? Trinity College, Hartford CT • Department of Computer Science • CPSC 225 14
  • 15. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); Object r = m.get(“Tim”); Question? It returns an Object! Trinity College, Hartford CT • Department of Computer Science • CPSC 225 15
  • 16. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); Object r = m.get(“Tim”); int i = (Integer) r; Question? That means we need to “cast” it to the correct type... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 16
  • 17. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); Object r = m.get(“Tim”); int i = (Integer) r; Question? But, I thought it was an int? Trinity College, Hartford CT • Department of Computer Science • CPSC 225 17
  • 18. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); Object r = m.get(“Tim”); int i = (Integer) r; Question? Yes, but Java is clever enough to know that if you need an int, an Integer will be “converted” to an int, and vice versa. Trinity College, Hartford CT • Department of Computer Science • CPSC 225 18
  • 19. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); Object r = m.get(“Tim”); int i = (Integer) r; This Works! int x = new Integer(5); Question? Integer y = x; Yes, but Java is clever enough to know that if you need an int, an Integer will be “converted” to an int, and vice versa. Trinity College, Hartford CT • Department of Computer Science • CPSC 225 19
  • 20. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); Object r = m.get(“Tim”); int i = (Integer) r; So, does this mean I need to do this every time I get something from a Map? Yes... unless you use generic types! Trinity College, Hartford CT • Department of Computer Science • CPSC 225 20
  • 21. Generics How do I use a HashMap? Map m = new HashMap(); m.put(“Tim”, 5); Object r = m.get(“Tim”); int i = (Integer) r; What is a generic type? Trinity College, Hartford CT • Department of Computer Science • CPSC 225 21
  • 22. Generics How do I use a HashMap? Map<String,Integer> m = new HashMap<String,Integer>(); m.put(“Tim”, 5); Integer r = m.get(“Tim”); int i = (Integer) r; No longer necessary What is a generic type? Trinity College, Hartford CT • Department of Computer Science • CPSC 225 22
  • 23. Generics How do I use a HashMap? Map<String,Integer> m = new HashMap<String,Integer>(); m.put(“Tim”, 5); Integer r = m.get(“Tim”); int i = (Integer) r; No longer necessary All the Java util classes can be used in this way. For example... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 23
  • 24. ForEach Loop Now, a different topic... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 24
  • 25. ForEach Loop Now, a different topic... We know about one kind of for loop bounded iteration Trinity College, Hartford CT • Department of Computer Science • CPSC 225 25
  • 26. ForEach Loop Now, a different topic... We know about one kind of for loop bounded iteration I mentioned there was another... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 26
  • 27. ForEach Loop Imagine we want to iterate over the elements in a list List<String> x = new ArrayList<String>(); x.add(“Tom”); x.add(“Jerry”); Trinity College, Hartford CT • Department of Computer Science • CPSC 225 27
  • 28. ForEach Loop Imagine we want to iterate over the elements in a list List<String> x = new ArrayList<String>(); x.add(“Tom”); x.add(“Jerry”); for (int i = 0; i < s.size(); i++) s.get(i); Here is one way to achieve that Trinity College, Hartford CT • Department of Computer Science • CPSC 225 28
  • 29. ForEach Loop Imagine we want to iterate over the elements in a list List<String> x = new ArrayList<String>(); x.add(“Tom”); x.add(“Jerry”); Iterator<String> i = x.iterator(); while (i.hasNext()) i.next() Here is another way to achieve that Trinity College, Hartford CT • Department of Computer Science • CPSC 225 29
  • 30. ForEach Loop Imagine we want to iterate over the elements in a list List<String> x = new ArrayList<String>(); x.add(“Tom”); x.add(“Jerry”); for (String e : x) // do something with e... Here is the right way to achieve that Trinity College, Hartford CT • Department of Computer Science • CPSC 225 30
  • 31. ForEach Loop Imagine we want to iterate over the elements in a list List<String> x = new ArrayList<String>(); x.add(“Tom”); x.add(“Jerry”); for (String e : x) // do something with e... Here is the right way to achieve that for each string e in x... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 31
  • 32. Any Questions? We covered quite a bit of Java in the past two lectures... Does anyone have any questions? Trinity College, Hartford CT • Department of Computer Science • CPSC 225 32

Editor's Notes