Title: Collection Considerations Data Structures, ADTs, class Object, Generic Collections
1Collection ConsiderationsData Structures, ADTs,
class Object, Generic Collections
2Some Definitions
- Abstract Data Type (ADT) A set of data values and
associated operations that are precisely
specified independent of any particular
implementation. - Bag, Set, List, Stack, Queue, Map
- Collection Class A Java language construct for
encapsulating the data and operations - ArrayList, LinkedList, Stack, TreeSet, HashMap
- Data Structure An organization of information
usually in memory - arrays, linked structure, binary trees, hash
tables
3Why Collection classes?
- Need data structures to store data to model real
world entities as a collection of objects - Things recognized by a jet fighter's radar system
- 35,267 students at UofA
- All courses taken by one student
- A set of Poker Hands
- An appointment book
- Phone numbers on your cell phone
4Common Methods
- Collection classes often have methods for
performing operations such as these - Adding an object to the collection of objects
- Removing an object from the collection
- Getting a reference to a particular object find
- then you can send messages to the object while it
is till in the collection. Program do this a lot - Retrieving certain objects such as the most
recently pushed (Stack) or least recently added
(Queue) - Arranging objects in a certain order sorting
5Storage Mechanisms
- We have used a 1-D array for storage
- elements in a set
- We have used a 2-D array for storage
- represent a society of cells in Artificial Life
- Can have linked structures to store elements
- Elements are not located in contiguous (next to
each other) memory as with arrays
6Structures to store elements
- Collection classes can store data in contiguous
memory (arrays) - or in a linked structure (linked lists, trees)
2 8 9 11 14 14 22 24 27 31
7- or in tree structures
50
75
25
12
35
66
90
28
41
81
95
54
91
100
8- or in hash tables
- Maps associate a key with a value
- Elements could be stored in a hash table
9Generic Collections
- Collection classes a big part of C Sc 227
- Next up Implement one class that can store a
collection of any element as a list - There are three approaches
- Use Java's Collection Framework (not yet)
- Use an array of Object (next)
- Use Java 5 Generics (soon)
- First learn about class Object
10The Object class
- Java has a class named Object
- It communicates with the operating system to
allocate memory at runtime - Object is the root of all other classes
- All classes extend Object or they extend a class
that extends object, or a class that extends a
class that extends Object
11EmptyClass inherits the methods of Object
- public class EmptyClass extends Object
- // This class inherits Object's 11 methods
-
- // Send some messages that Object has already
implemented - EmptyClass one new EmptyClass()
- EmptyClass two new EmptyClass()
- System.out.println(one.toString())
- System.out.println(one.hashCode())
- System.out.println(one.getClass())
- System.out.println(two.toString())
- System.out.println(two.hashCode())
- System.out.println(one.equals(two))
- one two
- System.out.println("\n"one.equals(two))
Output EmptyClass_at_ffb8f763 -4655261 class
EmptyClass EmptyClass_at_ffbcf763 -4393117 false tru
e
12 One way assignment
- Can assign any reference to an Object object
- Object obj1 new String("a string")
- Integer obj2 new Integer(123)
- System.out.println(obj1 " " obj2)
- Output
- a string 123
- But not the other way compiletime error
- String str obj1 // incompatible types
-
- 1 error
13Cast
- Sometimes an explicit cast is needed
- Consider retrieving an object with get
- Enclose the class name with what you know the
class to be in parentheses (String) and place it
before the reference to the Object object. - str (String)obj1
A reference to an Object object
14Cast
- Alternatively, you can write this more clearly in
two statements - // ArrayList's get methods returns a
- // reference to an Object object
- Object object stringList.get(0)
- // Now cast object from Object to String with
String str (String)object
15A collection of Anything
- Code Demo
- Implement a collection class that can store any
type - // Add any Object to this collection as the next
public void add(Object elementToAdd) -
- // Return the number of meaningful elements added
public int size() - // Return a reference to the element at the index
- // Precondition index gt 0 index lt size()
- public Object get(int index)
16Primitives as ObjectsObjects as Primitives
- To allow collections of primitives Java has
wrapper classes for each primitive type - Integer Double Character Boolean Long Float
- In Java 1.4 we need lots of extra code that some
consider - ListOfAnything list new ListOfAnything()
- list.add(new Integer(50))
- int firstVal ((Integer)list.get(0)).intValue()
- Switch Eclipse to 1.4 compliance and see the
above code work, but not this - list.add(50)
- int firstVal get(0)
17List of Primitives
- In Java 1.4, ListOfAnything could not store
primitives - ListOfAnything list new ListOfAnything()
- list.add(50) // Error in Java 1.4
- Since Java 5, objects can appear to be primitives
- Integer i 3 // assign an int to an Integer
- int j new Integer(4) // Integer to int
- int k j new Integer(7) 3 i // mix
- Is this legal in Java 5?
- Object obj 3
- int j (Integer)obj
18Boxing / Unboxing
- Autoboxing is the process of treating a primitive
as if it were an object. When the compiler sees
this - Integer anInt 3
- the code transforms into this
- Integer anInt new Integer(3)
- Switch Eclipse to 5.0 Compliance and see the code
on the previous slide works
19A Problem with Object Parameters and Returns
- Java does not check the type of element being
added - list.add(2)
- list.add(new Double(3.4))
- list.add(new BankAccount("Pat", 2.00))
- list.add(new Integer(2))
- So what do you cast list.get(0) to?
- So what do you cast list.get(1) to?
- So what do you cast list.get(2) to?
- Program could throw ClassCastException
20Generics
- Can use Java 5 Generics
- New class heading needed with ltEgt
- public class ListOfAnythingltEgt
- Then, in the class, the compiler sees E as the
same type as the type use to construct it - ListOfAnythingltStringgt strings
- new ListOfAnythingltStringgt()
- ListOfAnything ltIntegergt ints
- new ListOfAnythingltIntegergt()
- ListOfAnything ltBankAccountgt accounts
- new ListOfAnythingltBankAccountgt()
21Can't add the wrong type
- Java 5 generics checks the type being added at
compiletime. These are errors - strings.add(1)
- accounts.add("Strings")
- ints.add(new BankAccount("Pat", 12))
- Convert ListOfAnyType to a generic collection
with a type parameter ltEgt
22Code Demo Implement a Generic Collection with
ltEgt
- Here is a beginning
- public class ArrayBagltEgt implements BagltEgt
- private Object elements
- private int n
- public ArrayBag()
- elements new Object1000
- n 0
-