Collections

Collections - Java Collections There are three basic types...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Java Collections
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
There are three basic types of Java collections 1.List (Array-like, single-column, index access) 2.Map (two-column, key access) 3.Set (single-column, no dupes, no access to individual elements!) Each of these types of collections is defined by an interface of method names that shows what kind of function that collection should have. The names above (List, Map, Set) are the names of the interface files that define that type of collection. List Collections A List object is simply a list of 4-byte pointers to other objects. You can specify (limit) the type of the objects that can be added to the list when you create the List object. Or, you can leave it a general list, and put different kinds of objects in the same List. The List Object Objects In The List ___________ ____ / ____ \ / \ / 0 |____|--------------------------->|Cat | / 1 |____| \ \____/ | 2 |____| | ____ | 3 |____| | / \ Children | 4 |____|--------------------------->|Dog | of \ 5 |____| / \____/ Pets \ 6 |____|---------------------------->____ \___________/ / \ |Bird| \____/ The list type of collection is defined by the List interface. The interface (and therefore each implementing class) has methods like: size() // how many things in the list? isEmpty(); // returns a boolean
Background image of page 2
add(Object o) // adds object ref to the bottom of the list add(int index, Object o) // an insert function get(int index) // returns obj ref at that index contains(Object o) // returns a boolean set(int index, Object o) // replaces the object ref at that index remove(int index); // removes (and returns) the object ref remove(Object o); // removes this object ref no matter where it is clear(); // removes all entries A List is zero-based. A List can have duplicate entries. A List can have another List as an entry. To see all the methods, see documentation for the List interface and its parent, the Collection interface. Classes That Implement the List Interface Several class are provided in the class library that implement the List interface (provide the List function). More than one class is provided because the implementation is different to make the performance better when the list is used one way vs. another. For example, the Vector class provides thread protection when the list will be shared by multiple application threads. Whereas ArrayList does NOT provide thread protection, and therefore should be faster in a single-thread environment where the multi-thread protection is not required. The following three classes all implement the List interface: ArrayList - data saved in an array - fast look up LinkedList - data saved as a linked list - fast insert/remove
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Vector - list methods are synchronized to allow the list to be shared by multiple applications (multiple threads) without exposing the data integrity of the list. What does a "Linked List" look like? (And when should I use one?)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

Collections - Java Collections There are three basic types...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online