Java collections

1,461 views

Published on

Java 1.5 & Java 1.6 Collections New Features

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,461
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java collections

  1. 1. Java 1.5 & Java 1.6Collections New Features
  2. 2. Comments about changes• Easier to write. Reduces the chances for bugs.• Easier to read. Less code to accomplish the same result.• Minimize incompatibilities o Minimal changes to JVM.  Default compilation does not require new JVM.  If you use new Java 1.5 & Java 1.6 features:  Will not run on older JVMs.  Must tell javac compiler to run with prior version of compiler:  -source or –target keywords o New Java keywords kept to a minimum.
  3. 3. Generics (Raw Collection Example)List listOfStrings = getListOfStrings( );Iterator it = listOfStrings.iterator( );while (it.hasNext( ) ) {String item = (String)it.next( );// Work with that string} • If we remove the String cast, we will get a compiler error about incompatible types. • Generics let you finally get around this, by limiting the type that a particular List willaccept and we also need to assign it an instance that only accepts the sametype:List<String> listOfStrings = new LinkedList<String>( );
  4. 4. Generics (Generic Collection Example)List<String> onlyStrings = new LinkedList<String>( );onlyStrings.add("Legal addition");onlyStrings.add(new StringBuilder("Illegal Addition"));onlyStrings.add(25);• We will get two compiler errors about type checking.• Because this list can’t accept another object except String type. • public interface List<E> extends Collection, Iterable{public boolean add(E o);…}
  5. 5. Generics (Generic Collection Example)public interface List<E> extends Collection,Iterable{public boolean add(E o);…} • Compiler replaces every occurrence of E with the type we supplied, in our example,String. • These approach dont work with primitive values.List<int> list = new LinkedList<int>( ); // Compilation errorList<Integer> list = new LinkedList<Integer>( );
  6. 6. Generics (Generic Collection Example#2)List onlyStrings = new LinkedList<StringBuilder>( );onlyStrings.add(new StringBuilder("Illegal Addition"));onlyStrings.add(“Illegal addition"); // Not a compile error. But runtime error
  7. 7. “Generic” Collection Example #3 • Is List the same as List<Object>? List<StringBuilder> onlyStrings = new LinkedList<StringBuilder>(); List<Object> onlyStrings2 = onlyStrings; // Compile error(Incompatible types)To prevent later errors such as:onlyStrings2.add(“String”);StringBuilder pol = onlyStrings.get(0); • List is a raw, List<Object> is a generic Collection.
  8. 8. “Generic” Collection Example #4• Assume Homeowners is a subclass of Policy. List<Policy> policies = new ArrayList<Policy>(); policies.add(new Homeowners()); Homeowners home = policies.get(0); // Compile error!!!• policies is a generic Collection for Policy, not Homeowners. o Need a cast to Homeowners.
  9. 9. “Generic” Collection Example #5• Assume Auto is also a subclass of Policy. List<Policy> policies = new ArrayList<Policy>(); policies.add(new Auto()); Homeowners home = (Homeowners)policies.get(0); // Not a Compile error. // But, a ClassCastException at runtime!• policies is a generic Collection for Policy, an Auto policy is added, but a Homeowners policy is extracted.
  10. 10. “Generic” Collection Example #6• Assume Auto is a subclass of Policy. List<Auto> autoPolicies = new ArrayList<Auto>(); autoPolicies.add(new Auto()); List<Policy> policies = new ArrayList<Policy>(); policies = autoPolicies; // Compile error!• policies is an “incompatible type” for autopolicies! o A List of Policy is not compatible with a List of Auto.
  11. 11. “Generic” Collection Example #6 Fix• Assume Auto is a subclass of Policy. List<Auto> autoPolicies = new ArrayList<Auto>(); autoPolicies.add(new Auto()); List<? extends Policy> policies = new ArrayList<Policy>(); policies = autoPolicies;• policies is now “compatible” with autopolicies! o The “? extends Policy” is a wildcard specification.

×