Lists in Java Part of the Collections Framework












![Collection: Array operations • Object[ ] to. Array( ); – creates a new array Collection: Array operations • Object[ ] to. Array( ); – creates a new array](https://slidetodoc.com/presentation_image/45a9412fc8e672ba68bf181864d8880a/image-13.jpg)










- Slides: 23

Lists in Java Part of the Collections Framework

Kinds of Collections • Collection—a group of objects, called elements – Set—An unordered collection with no duplicates • Sorted. Set—An ordered collection with no duplicates – List—an ordered collection, duplicates are allowed • Map—a collection that maps keys to values – Sorted. Map—a collection ordered by the keys • Note that there are two distinct hierarchies 2

Using Collections • import java. util. * or import java. util. Collection; • There is a sister class, java. util. Collections; that provides a number of algorithms for use with collections: sort, binary. Search, copy, shuffle, reverse, max, min, etc. 3

Collections Example import java. util. *; // importing Arrays, List, and Collections public class Test. Collections { public static void main(String args[]) { String[] array = {"Phil", "Mary", "Betty", "bob"}; List my. List = Arrays. as. List(array); Collections. sort(my. List); System. out. println("Sorted: " + my. List); int where = Collections. binary. Search(my. List, "bob"); System. out. println("bob is at " + where); Collections. shuffle(my. List); System. out. println("Shuffled: " + my. List); } } Sorted: [Betty, Mary, Phil, bob] bob is at 3 Shuffled: [Betty, bob, Phil, Mary] 4

Collections are interfaces • Collection is actually an interface • Each kind of Collection has one or more implementations • You can create new kinds of Collections • When you implement an interface, you promise to supply the required methods • Some Collection methods are optional – How can an interface declare an optional method? 5

Creating a Collection • All Collection implementations should have two constructors: – A no-argument constructor to create an empty collection – A constructor with another Collection as argument • All the Sun-supplied implementations obey this rule, but— • If you implement your own Collection type, this rule cannot be enforced, because an Interface cannot specify constructors 6

Collection: Basic operations int size( ); boolean is. Empty( ); boolean contains(Object element); boolean add(Object element); // Optional boolean remove(Object element); // Optional Iterator iterator( ); 7

Collection: Iterator public interface Iterator { boolean has. Next( ); // true if there is another element Object next( ); // returns the next element (advances the iterator) void remove( ); // Optional // removes the element returned by next } 8

Using an Iterator • static void print. All (Collection coll) { Iterator iter = coll. iterator( ); while (iter. has. Next( )) { System. out. println(iter. next( ) ); } } • Note that this code is polymorphic—it will work for any collection 9

Collection: Bulk operations boolean contains. All(Collection c); boolean add. All(Collection c); // Optional boolean remove. All(Collection c); // Optional boolean retain. All(Collection c); // Optional void clear( ); // Optional • add. All, remove. All, retain. All return true if the object receiving the message was modified 10

Mixing Collection types • Note that most methods, such as boolean contains. All(Collection c); are defined for any type of Collection, and take any type of Collection as an argument • This makes it very easy to work with different types of Collections 11

singleton • Collections. singleton(e) returns an immutable set containing only the element e • c. remove. All(Collections. singleton(e)); will remove all occurrences of e from the Collection c 12
![Collection Array operations Object to Array creates a new array Collection: Array operations • Object[ ] to. Array( ); – creates a new array](https://slidetodoc.com/presentation_image/45a9412fc8e672ba68bf181864d8880a/image-13.jpg)
Collection: Array operations • Object[ ] to. Array( ); – creates a new array of Objects • Object[ ] to. Array(Object a[ ]); – Allows the caller to provide the array • Examples: Object[ ] a = c. to. Array( ); String[ ] a; a = (String[ ]) c. to. Array(new String[0]); 13

The List interface • The order of elements in a List is important, and there may be duplicate elements • Operations are exactly those for Collection int size( ); boolean is. Empty( ); boolean contains(Object e); boolean add(Object e); boolean remove(Object e); Iterator iterator( ); boolean contains. All(Collection c); boolean add. All(Collection c); boolean remove. All(Collection c); boolean retain. All(Collection c); void clear( ); Object[ ] to. Array(Object a[ ]); 14

List implementations • List is an interface; you can’t say new List ( ) • There are two implementations: – Linked. List gives faster insertions and deletions – Array. List gives faster random access • It’s poor style to expose the implementation, so: • Good: List list = new Linked. List ( ); Bad: Linked. List list = new Linked. List ( ); 15

Inherited List methods • list. remove(e) removes the first e • add and add. All add to the end of the list • To append one list to another: list 1. add. All(list 2); • To append two lists into a new list: List list 3 = new Array. List(list 1); list 3. add. All(list 2); • Again, it's good style to hide the implementation 16

List: Positional access Object get(int index); // Required -// the rest are optional Object set(int index, Object element); void add(int index, Object element); Object remove(int index); abstract boolean add. All(int index, Collection c); • These operations are more efficient with the Array. List implementation 17

List: Searching int index. Of(Object o); int last. Index. Of(Object o); • equals and hash. Code work even if implementations are different 18

Interface List: Iteration • Iterators specific to Lists: 1. List. Iterator list. Iterator( ); 2. List. Iterator list. Iterator(int index); 1. starts at the position indicated (0 is first element) • Inherited methods: boolean has. Next( ); Object next( ); void remove( ); • Additional methods: 1. boolean has. Previous() 2. Object previous() 19

List: Iterating backwards boolean has. Previous( ); Object previous( ); int next. Index( ); int previous. Index( ); • Think of the iterator as “between” elements • Hence, next followed by previous gives you the same element each time 20

List: More operations • void add(Object o); – Inserts an object at the cursor position • Object set(Object o); // Optional – Replace the current element; return the old one • Object remove(int index); // Optional – Remove and return the element at that position 21

List: Range-view • List sub. List(int from, int to); allows you to manipulate part of a list • A sublist may be used just like any other list 22

The End http: //java. sun. com/docs/books/tutorial/collections /interfaces/collection. html http: //java. sun. com/docs/books/tutorial/collections /interfaces/list. html 23