module 04 - generics and collections

16
Generics and Collections Oracle® Certified Professional, Java® SE 7 Programmer Module 4 © 20092012 JohnYeary

Upload: john-yeary

Post on 31-Jul-2015

3.526 views

Category:

Documents


0 download

DESCRIPTION

This presentation is the fourth module of 12 modules covering the Oracle Certified Java SE 7 Programmer exam. This module covers:Create a generic classUse the diamond syntax to create a collectionAnalyze the interoperability of collections that use raw type and generic typesUse wrapper classes and auto-boxingCreate and use a List, a Set and a Deque.Create and use a MapUse java.util.Comparator and java.lang.Comparable.Sort and search arrays and lists

TRANSCRIPT

Page 1: Module 04 - Generics and Collections

Generics  and  Collections  Oracle®  Certified  Professional,  Java®  SE  7  Programmer  

Module  4  

©  200

9-­‐20

12  Jo

hn  Yea

ry  

Page 2: Module 04 - Generics and Collections

Module  4  -­‐  Objectives  

Ê  Create  a  generic  class  

Ê  Use  the  diamond  syntax  to  create  a  collection  

Ê  Analyze  the  interoperability  of  collections  that  use  raw  type  and  generic  types  

Ê  Use  wrapper  classes  and  auto-­‐boxing  

Ê  Create  and  use  a  List,  a  Set  and  a  Deque.  

Ê  Create  and  use  a  Map  

Ê  Use  java.util.Comparator  and  java.lang.Comparable.  

Ê  Sort  and  search  arrays  and  lists  

Page 3: Module 04 - Generics and Collections

Generic  Class  

Ê  Generics  were  introduced  in  Java  5.  

Ê  Generics  were  added  to  allow  compile  time  checking  of  Java  types.  This  allows  the  javac  compiler  to  find  errors  during  compilation,  and  helps  to  avoid  exceptions  at  runtime.  

Ê  Generics  remove  the  requirement  to  cast  objects.  

Ê  A  generic  class  requires  a  Type.  Typically  you  will  see  arguments  like  <T>  (type),  <K>  (key),  <V>  (value),  <E>  (element).  

Page 4: Module 04 - Generics and Collections

Generic  Class  (cont.)  

public  interface  Producer<T>  {  

       T  produce();  

}  

public  class  WidgetProducer  implements  Producer<Widget>  {  

       @Override  

       public  Widget  produce()  {  

               return  new  Widget();  

       }  

Page 5: Module 04 - Generics and Collections

Raw  and  Generic  Type  Collections  

Ê  A  “raw”  collection  is  a  name  for  the  version  of  collections  prior  to  the  introduction  of  generics  in  Java  5.  

Ê  A  “raw”  collection  is  not  type-­‐safe,  and  requires  casting  when  an  element  is  extracted  from  the  collection.  

Ê  A  “raw”  collection  contains  Object(s).  

Ê  A  typed  collection  declares  a  type  which  the  collection  contains,  e.g.  ArrayList<Widget>  is  a  list  of  Widget  objects.  

Ê  No  casting  required  when  elements  are  are  extracted.  

Page 6: Module 04 - Generics and Collections

Raw  and  Generic  Type  Collections  (cont.)  

Ê  A  typed  collection  can  also  contain  subclasses  of  a  typed  object  using  a  syntax  like,  e.g.  ArrayList<?  extends  Widget>();.  

Ê  A  wildcard  (?)  can  be  used  to  declare  a  collection,  e.g.  Collection<?>  c  =  new  ArrayList<>();  

Page 7: Module 04 - Generics and Collections

Diamond  Operator  <>  

Ê  Introduced  in  Java  7  as  a  part  of  Project  Coin.  

Ê  Also  called  “Type  Inference”  because  the  type  is  inferred  from  the  LHS  (Left-­‐Hand  Side)  of  the  declaration.  

Ê  Simplifies  the  declaration  of  typed  collections.    

List<List<Map<String,String>>>  list  =    new  ArrayList<List<Map<String,String>>>();  //pre-­‐Java  7  

List<List<Map<String,String>>>  list    =  new  ArrayList<>();//Java  7+  

Page 8: Module 04 - Generics and Collections

Wrapper  Classes  (primitive  wrappers)  

Ê  Wrapper  classes  are  used  to  represent  primitive  values  when  an  Object  is  required.  

Ê  Wrapper  classes  are  used  extensively  to  wrap  primitive  classes  for  use  in  Collections  since  a  Collection  can  only  store  objects.  It  is  also  used  with  a  number  of  reflection  API  classes.  

Ê  The  8  primitive  wrapper  classes  are:  Byte  (byte),  Short  (short),  Character  (char),  Integer  (int),  Long  (long),  Float  (float),  Double  (double),  and  Boolean  (boolean).  

Page 9: Module 04 - Generics and Collections

Auto-­‐Boxing  and  Auto-­‐Unboxing  

Ê  Java  5  SE  introduced  auto-­‐boxing.  This  is  the  automatic  conversion  of  a  primitive  to  an  Object  wrapper  if  required.  

Ê  Auto-­‐unboxing  is  the  automatic  conversion  of  a  primitive  wrapper  class  to  a  primitive.  

Ê  An  Exception  can  occur  when  auto-­‐unboxing  if  the  primitive  wrapper  is  null.  

Page 10: Module 04 - Generics and Collections

List<T>,  Set<T>,  and  Deque<T>  

Ê  List  –  An  ordered  collection  also  known  as  a  sequence.  Values  are  stored  in  the  order  in  which  they  are  added  unless  manipulated.  

Ê  Set  –  A  collection  which  contains  no  duplicate  entries,  and  at  most  one  null  value.  Objects  being  added  to  a  set  should  implement  equals,  and  hashcode  methods  otherwise  it  is  possible  to  add  multiple  objects  to  a  Set  which  may  be  intrinsically  equivalent.  

Ê  Deque  –  A  linear  collection  which  supports  adding  and  removing  elements  from  either  end  (“double-­‐ended  queue”).  

Page 11: Module 04 - Generics and Collections

Map<K,V>  

Ê  A  Map  is  not  part  of  the  Collections  API!  

Ê  A  map  stores  values  by  key.  

Ê  The  keys  must  be  unique,  but  the  same  value  can  be  assigned  to  multiple  keys.  

Ê  Warning:  do  not  use  mutable  objects  for  keys!  

Page 12: Module 04 - Generics and Collections

Comparable<T>  

Ê  The  Comparable<T>  interface  has  one  method:  int  compareTo(T  o);  which  is  used  determine  order.  

Ê  “Returns  a  negative  integer,  zero,  or  a  positive  integer  as  this  object  is  less  than,  equal  to,  or  greater  than  the  specified  object.”  

Page 13: Module 04 - Generics and Collections

Comparator<T>  

Ê  The  Comparator<T>  interface  has  two  methods:  int  compare(T  01,  T  02);  and  boolean  equals(Object  0);  which  are  used  to  determine  order,  and  if  two  comparators  are  the  same.  

Ê  “Compares  its  two  arguments  for  order.    Returns  a  negative  integer,  zero,  or  a  positive  integer  as  the  first  argument  is  less  than,  equal  to,  or  greater  than  the  second.”  

Ê  The  equals  method  DOES  not  determine  equality  between  compared  objects.  It  only  determines  if  the  Object  provided  to  the  method  is  equal  to  the  current  Comparator.  This  includes  ordering.  

Page 14: Module 04 - Generics and Collections

Sorting  and  Searching  Arrays  and  Lists  

Ê  The  Collections  class  is  YOUR  friend.  

Ê  The  Collections  class  has  a  significant  number  of  methods  to  make  using  the  Java  Collections  Framework  easier  to  use.  

Ê  Sorting  can  be  done  via  “natural”  sorting  using  the  Collections.sort(Collection<T>)  method.  

Ê  Sorting  can  also  be  accomplished  by  providing  a  Comparator<T>  to  the  sort  method.  

Page 15: Module 04 - Generics and Collections

Sorting  and  Searching  Arrays  and  Lists  (cont.)  

Ê  The  Arrays  class  is  YOUR  friend.  

Ê  The  Arrays  class  has  a  number  of  convenience  methods  to  sort,  copy,  compare  (equals),  and  search  an  array.    

Ê  The  Arrays  class  has  a  convenience  method  to  convert  an  array  to  a  List  object.  

Page 16: Module 04 - Generics and Collections

 Attribution-­‐NonCommercial-­‐ShareAlike  3.0  Unported  

This  work  is  licensed  under  the  Creative  Commons  Attribution-­‐NonCommercial-­‐ShareAlike  3.0  Unported  License.  To  view  a  copy  of  this  license,  visit  http://creativecommons.org/licenses/by-­‐nc-­‐sa/3.0/.