230 likes | 244 Views
Explore foundational data structures such as arrays, lists, sets, maps, and their implementations in Java programming. Learn about key interfaces, algorithms, and common classes in the context of thinking in Java.
E N D
Object Oriented Programming Lecture 10: Data Structures Mustafa Emre İlal emreilal@iyte.edu.tr
Recap • Assignment 08 • File input/output
Today • Assignment 9 • Data structures • Thinking in Java – Chapter 11
We need to give a meaningful structure to the data Array is a basic structure We mentioned java.util.Vector Some common structres exist that programmers often make use of. Each is an analogy for a real world situation. Examples: Bag Heap Set List Queue Table Tree Collections
java.util.* • Java 2 "Collections" – A "framework" (iskelet) • Interfaces (Kontratlar) • Collection, Set, Map, etc. • Implementations (Uygulayan Sınıflar) • 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 • 2 Main Groups • Subclasses of the Collectioninterface • Set : No duplicate elements allowed • List : Can have duplicates, order is important • Queue: FIFO (or LIFO w/ DEque) • Map : Elements are recalled based on an index (key – value pairing – “mapping”)
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]) • Queue (only add, remove and inspect element) • Deque (double-ended queue) • Map (matching of an element to a unique ‘key’ for later retrieval) • SortedMap (collection that is ordered by the key)
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. • LinkedHashSet • Items are ordered and iterated in predictable order
Examples - Sets import java.util.*; SettoolBox = 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. Also implements the Deque interface.
Example - Lists import java.util.*; // use of Generics below ArrayList<String> attendance = new ArrayList<String>(); attendance.add("Ali Durmaz"); attendance.add("VeliKaymaz"); attendance.add("Ayşe Kaçmaz"); for (i=0; i< attendance.size(); i++) println(attendance.get(i) );
Dequeue Classes • ArrayDeque • An automatically resizable Array implementation. • LinkedList • “Doubly linked list” implementation where given an element you can access the previous and next elements. Also implements the List interface.
Example - Deque import java.util.*; ArrayDequeue<String> line = new ArrayDequeue <String>(); line.add("Ali Önde"); line.add("Veli Arkasında"); line.add("Ayşe Sonuncu"); for (String s: line) // foreach syntax! println(s);
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<String, String> team = new HashMap<>(); team.put(“defender", “Emre"); team.put(“goalie", “Kadir"); team.put(“striker", “Lale"); if ( ! team.containsKey(“midfielder") ) team.put(“midfielder", “Mustafa"); print(team);
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 ShapeApplications you have developed for assignment 07, using Collections. • Choose a collection for Shapes • Choose a collection for colors • etc. • Hint: You may define and use a Comparator to simplify sorting...
Next week • Interfaces and beyond