java collections and generics
play

Java Collections and Generics Object-oriented programming Inf1 :: - PowerPoint PPT Presentation

Java Collections Generics Java Collections and Generics Object-oriented programming Inf1 :: 2008 Object-oriented programming Java Collections and Generics Java Collections Generics Collections Data structures like the ones we discussed in


  1. Java Collections Generics Java Collections and Generics Object-oriented programming Inf1 :: 2008 Object-oriented programming Java Collections and Generics

  2. Java Collections Generics Collections Data structures like the ones we discussed in the previous lectures are so important that the Java library provided a generic implementation Package java.util The collections framework is a perfect example of polymorphism , interfaces and object-oriented design There is a basic interface ( Collection ) that all classes in java.util implement Behaviours are implemented in a variety of ways We will deal with lists , sets , iterators and trees Object-oriented programming Java Collections and Generics

  3. Java Collections Generics The class/interface hierarchy Iterator Collection boolean hasNext () boolean isEmpty () Object next () boolean add/remove (Object o) void remove () boolean add/removeAll (Collection c) void clear () ListIterator boolean contains (Object o) boolean containsAll (Collection c) boolean hasPrevious () Comparator Iterator iterator () Object previous () int compare (Object o1, Object o2) int size () int nextIndex () Object [] toArray () int previousIndex () Set List Object get (int index) Object set (int index, Object o) void add (int index, Object o) SortedSet void addAll (int index, Collection c) Object remove (int index) AbstractSet Comparator comparator () int indexOf (Object o) Object first () int lastIndexOf (Object o) Object last () ListIterator listIterator () SortedSet headSet (Object to) ListIterator listIterator (int index) SortedSet tailSet (Object from) List subList (int start, int end) SortedSet subset (Object from, Object to) AbstractList TreeSet HashSet void removeRange (int start, int end) AbstractSequentialList LinkedList ArrayList Object getFirst () void ensureCapacity (int min) Object getLast () void trimToSize () void addFirst (Object o) void addLast (Object o) Object removeFirst () Object removeLast () Object-oriented programming Java Collections and Generics

  4. Java Collections Generics In more detail Collection s are either List s or Set s List s are Sequential Possibly with duplicate values Unordered with respect to the values they store Set s are Unordered with respect to the values Without any duplicate values SortedSet s are Ordered (with respect to their values ) Set s Iterator s are A way of traversing all values in a Collection ListIterator s are Special Iterator s that allow one to traverse sequential Collection s like List s Object-oriented programming Java Collections and Generics

  5. Java Collections Generics Using a List import everything from the import java.util.*; java.util package (the collections framework) public class ListTest { private static final String colors[] = { "red", "white", "blue", "green", "gray", "orange", "tan", "white", "cyan", "peach", "gray", "orange" }; List interface public ListTest () { make the reference aim at a List list = new ArrayList(); class that implements the for (int i = 0; i < colors.length; i++) interface ( ArrayList ) list.add(colors[i]); System.out.print("\nList: "); polymorphism printList(list); ArrayList ‘s implementation System.out.print("\nReversed List: "); of add() will be called ; printReversedList(list); add() is a Collection System.out.print("\nList from 5, forward: "); method and List extends printListFromIndex(5, list); Collection System.out.print("\nList from 5, backward: "); printReversedListFromIndex(5, list); } Object-oriented programming Java Collections and Generics

  6. Java Collections Generics Traversing a List with an Iterator iterator declare an Iterator over the List , i.e. , obtain a sequential handle over the List ‘s data; iterator() returns a ListIterator , which is an Iterator public void printList (List l) { Iterator iter = l.iterator(); forward scan while (iter.hasNext()) keep scanning forward , as list iterator System.out.print(iter.next() + " "); long as the Iterator declare a ListIterator over System.out.println(); says there are more the List , since we want the } elements extra functionality of scanning public void printReversedList (List l) { from a specific point ListIterator iter = l.listIterator(l.size()); while (iter.hasPrevious()) backward scan System.out.print(iter.previous() + " "); keep scanning backwards , as System.out.println(); long as the ListIterator } says there are more elements Object-oriented programming Java Collections and Generics

  7. Java Collections Generics Using a List — results import java.util.*; public class ListTest { private static final String colors[] = { "red", "white", "blue", "green", "gray", "orange", "tan", "white", "cyan", "peach", "gray", "orange" }; public ListTest () { List list = new ArrayList(); for (int i = 0; i < colors.length; i++) list.add(colors[i]); System.out.print("\nList: "); printList(list); System.out.print("\nReversed List: "); printReversedList(list); System.out.print("\nList from 5, forward: "); printListFromIndex(5, list); System.out.print("\nList from 5, backward: "); printReversedListFromIndex(5, list); } List: red white blue green gray orange tan white cyan peach gray orange Reversed List: orange gray peach cyan white tan orange gray green blue white red List from 5, forward: orange tan white cyan peach gray orange List from 5, backward: gray green blue white red Object-oriented programming Java Collections and Generics

  8. Java Collections Generics Using a Set import java.util.* imports the Collections framework import java.util.*; public class SetTest { private static final String colors[] = { "red", "white", "blue", "green", "gray", "orange", "tan", "white", "cyan", "peach", "gray", "orange" }; reference to concrete class assign a List ( interface ) reference public SetTest () { List list = new ArrayList(); to an ArrayList concrete class (that implements the interface ) for (int i = 0; i < colors.length; i++) list.add(colors[i]); System.out.print("\nArrayList: "); create a set from a list printCollection(list); use a Set ( interface ) reference to a Set hashSet = new HashSet(list); HashSet concrete class (that implements the interface ); use a list System.out.print("\nHashSet: "); printCollection(hashSet); as the input collection } Object-oriented programming Java Collections and Generics

  9. Java Collections Generics Traversing a Set Collection as parameter this will work for both List s and Set s since they both extend Collection public void printCollection (Collection c) { Iterator iter = c.iterator(); while (iter.hasNext()) System.out.print(iter.next() + " "); System.out.println(); } Object-oriented programming Java Collections and Generics

  10. Java Collections Generics Using s Set — results import java.util.*; public class SetTest { private static final String colors[] = { "red", "white", "blue", "green", "gray", "orange", "tan", "white", "cyan", "peach", "gray", "orange" }; public SetTest () { List list = new ArrayList(); for (int i = 0; i < colors.length; i++) list.add(colors[i]); System.out.print("\nArrayList: "); printCollection(list); Set hashSet = new HashSet(list); System.out.print("\nHashSet: "); printCollection(hashSet); } ArrayList: red white blue green gray orange tan white cyan peach gray orange HashSet: red cyan white tan gray green orange blue peach Object-oriented programming Java Collections and Generics

  11. Java Collections Generics Using a SortedSet import java.util.*; public class SortedSetTest { private static final String names[] = { "yellow", "green", "black", "tan", "grey", "white", "orange", "red", "green" }; polymorphism public SortedSetTest () { TreeSet implements SortedSet set = new TreeSet (); SortedSet for (int i = 0; i < names.length; i++) set.add(names[i]); System.out.print("\nset: "); printSet(set); System.out.print("\nheadSet(\"orange\"): "); printSet(set.headSet("orange")); SortedSet behaviour System.out.print("\ntailSet(\"orange\"): "); the methods called are printSet(set.tailSet("orange")); SortedSet methods , but System.out.print("\nheadSet(\"foo\"): "); TreeSet ‘s implementations printSet(set.headSet("foo")); will be called System.out.print("\ntailSet(\"foo\"): "); printSet(set.tailSet("foo")); System.out.print("\nfirst: "); System.out.println(set.first()); System.out.print("\nlast: "); System.out.println(set.last()); } Object-oriented programming Java Collections and Generics

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend