object oriented programming ders 10: data structures mustafa emre İlal [email protected]
TRANSCRIPT
Collections
• We need to give a meaningful structure to the data
• Array is a basic structure
• We mentioned java.util.Vector
• Some common structes exist that programmers often make use of. Each is an analogy for a real world situation.
• Örnekler:– Bag
– Heap
– Set
– List
– Queue
– Table
– Tree
java.util.*
• Java 2 "Collections" – A "framework" (iskelet)– Interfaces (Kontratlar)
• Collection, Set, Map, etc.
– Implementations (Uygulamaları)• TreeMap, LinkedList, ArrayList, etc.
– Older classes that existed in Java 1.1• Vector, Hashtable
– Helper classes• Arrays, Iterator, etc.
– Algorithms• Sort, etc.
Interfaces (Kontratlar)
• Defines the type of data structure– 3 Main Types
• Set : No duplicate elements allowed
• List : Can have duplicates, order is important
• Map : Elements are recalled based on an index
Interfaces (Kontratlar)
• 6 interfaces in total– Collection (general groupings)
• Set (elements are important – can be ordered)
• List (order is the determining factor)
• SortedSet (a set that is kept in order based on a comparing method. [Comparator])
– Map (matching of an element to a unique ‘key’ for later retrieval)
• SortedMap (collection that is ordered by the key)
Classes
Implementation Type
HashTableVariable sized
ArrayBalanced
TreeLinked list
Interface
Set HashSet TreeSet
List ArrayList LinkedList
Map HashMap TreeMap
Set Classes
• HashSet– A structure that stores elements using a hashtable
algorithm. Fastest set.
• TreeSet– A sorted set implementation using a "Red-Black"
sorting algorithm.
Examples - Sets
import java.util.*;
HashSet toolBox = new HashSet();
toolBox.add(“hammer");
toolBox.add(“screwdriver");
toolBox.add(“pliers");
if ( !toolBox.isEmpty() )
println( toolBox.size() );
println ( toolBox );
List Classes
• ArrayList– Similar to a Vector. An automatically resizable Array
implementation.
• LinkedList– “Doubly linked list” implementation where given an
element you can access the previous and next elements. Useful in modelling queues.
Example - Lists
import java.util.*;
ArrayList attendance = new ArrayList();
attendance.add("Ali Durmaz");
attendance.add("Veli Kaymaz");
attendance.add("Ayşe Kaçmaz");
for (i=0; i< attendance.size(); i++)
println(attendance.get(i) );
Map Classes
• HashMap– An implementation where keys are stored and retrieved
using a hastable. Fast.
• TreeMap– A SortedMap implementation that keeps keys ordered
using a "Red-Black" sorting algorithm.
Example - Maps
import java.util.*;
HashMap team = new HashMap();
team.put(“defender", “Emre");
team.put(“goalie", “Kadir");
team.put(“striker", “Lale");
if ( ! team.containsKey(“midfielder") )
team.put(“midfielder", “Mustafa");
Helper classes
• Arrays– Static methods
• Searching, sorting, comparison, populating, etc.
• Basic usage– Iterator
– Comparator
– Exceptions
Iterator• Mechanism that allows us to examine the elements
stored in a data structure one by one.– The iterator() that exists in all collections returns an
iterator that we can use.– Iterator only has three methods
• Object next()• boolean has next()• void remove()
• ListIterator– Has additional methods that are suitable for lists.
• int nextIndex()• void set(Object o)• boolean hasPrevious()• …
Example - Iterator
import java.util.*;
Iterator i = attendance.iterator();
while ( i.hasNext() )
System.out.println( i.next() );
Generics – Java 1.5
• Previously a collection had no idea what Type (the class they belonged to) of elements it was storing.
• Java 1.5 provides “generics”. Through this mechanism, the compiler guarantees that the program uses the correct classes.
Generics – example
ArrayList<String> attendance = new ArrayList<String>();
attendance.add("Ali Durmaz");
attendance.add("Veli Kaymaz");
attendance.add(new Integer(25)); //error!!
for (i=0; i< attendance.size(); i++)
println(attendance.get(i) );
Iterator<String> i = attendance.iterator();
while ( i.hasNext() )
System.out.println( i.next() );
Assignment 10
• Rewrite the ShapeApplicaitons you have developed for assignment 07, using Collections.
– Choose a collection for Shapes
– Choose a group for colors
– etc.• Hint: Make use of Comparator to simplify sorting...