Lists in Java Part of the Collections Framework
Kinds of Collections <ul><li>Collection --a group of objects, called  elements </li></ul><ul><ul><li>Set -- An unordered c...
Using Collections <ul><li>import java.util.* or  import java.util.Collection; </li></ul><ul><li>There is a sister class,  ...
Collections are interfaces <ul><li>Collection  is actually an interface </li></ul><ul><li>Each kind of Collection has one ...
Creating a  Collection <ul><li>All  Collection  implementations should have two constructors: </li></ul><ul><ul><li>A no-a...
Collection:  Basic operations <ul><li>int size( ); </li></ul><ul><li>boolean isEmpty( ); </li></ul><ul><li>boolean contain...
Collection: Iterator <ul><li>boolean hasNext( ); </li></ul><ul><ul><li>//  true  if there is another element </li></ul></u...
Using an  Iterator <ul><li>static void printAll (Collection coll) {   Iterator iter = coll.iterator( );    while (iter.has...
Collection:  Bulk operations <ul><li>boolean containsAll(Collection c); </li></ul><ul><li>boolean addAll(Collection c);  /...
Mixing Collection types <ul><li>Note that most methods, such as   boolean containsAll(Collection c); are defined for  any ...
singleton <ul><li>Collections.singleton(e)   returns an immutable set containing only the element  e </li></ul><ul><li>c.r...
Collection:  Array operations <ul><li>Object[ ] toArray( ); </li></ul><ul><ul><li>creates a new array of   Object s </li><...
The  List  interface <ul><li>A   List   is ordered and may have duplicates </li></ul><ul><li>Operations are exactly those ...
List  implementations <ul><li>List   is an interface; you can’t say  new List ( ) </li></ul><ul><li>There are two implemen...
Inherited  List  methods  <ul><li>list.remove(e)  removes the  first   e </li></ul><ul><li>add   and  addAll   add to the ...
List:  Positional access <ul><li>Object get(int index);  // Required --   // the rest are optional </li></ul><ul><li>Objec...
List:  Searching <ul><li>int indexOf(Object o); </li></ul><ul><li>int lastIndexOf(Object o); </li></ul><ul><li>equals   an...
Interface  List:  Iteration <ul><li>Iterators specific to Lists: </li></ul><ul><li>ListIterator listIterator( ); </li></ul...
List : Iterating backwards <ul><li>boolean hasPrevious( ); </li></ul><ul><li>Object previous( ); </li></ul><ul><li>int nex...
List : More operations <ul><li>void add(Object o);   </li></ul><ul><ul><li>Inserts an object at the cursor position </li><...
List:  Range-view <ul><li>List subList(int from, int to);   allows you to manipulate part of a list </li></ul><ul><li>A su...
<ul><li>Thank you </li></ul>
Upcoming SlideShare
Loading in …5
×

List in java

1,041 views

Published on

how to use list in java.

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,041
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
31
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

List in java

  1. 1. Lists in Java Part of the Collections Framework
  2. 2. Kinds of Collections <ul><li>Collection --a group of objects, called elements </li></ul><ul><ul><li>Set -- An unordered collection with no duplicates </li></ul></ul><ul><ul><ul><li>SortedSet -- An ordered collection with no duplicates </li></ul></ul></ul><ul><ul><li>List -- an ordered collection, duplicates are allowed </li></ul></ul><ul><li>Map -- a collection that maps keys to values </li></ul><ul><ul><li>SortedMap -- a collection ordered by the keys </li></ul></ul><ul><li>Note that there are two distinct hierarchies </li></ul>
  3. 3. Using Collections <ul><li>import java.util.* or import java.util.Collection; </li></ul><ul><li>There is a sister class, java.util.Collection s ; that provides a number of algorithms for use with collections: sort , binarySearch , copy , shuffle , reverse , max , min , etc. </li></ul>
  4. 4. Collections are interfaces <ul><li>Collection is actually an interface </li></ul><ul><li>Each kind of Collection has one or more implementations </li></ul><ul><li>You can create new kinds of Collections </li></ul><ul><li>When you implement an interface, you promise to supply the required methods </li></ul><ul><li>Some Collection methods are optional </li></ul><ul><ul><li>How can an interface declare an optional method? </li></ul></ul>
  5. 5. Creating a Collection <ul><li>All Collection implementations should have two constructors: </li></ul><ul><ul><li>A no-argument constructor to create an empty collection </li></ul></ul><ul><ul><li>A constructor with another Collection as argument </li></ul></ul><ul><li>All the Sun-supplied implementations obey this rule, but-- </li></ul><ul><li>If you implement your own Collection type, this rule cannot be enforced, because an Interface cannot specify constructors </li></ul>
  6. 6. Collection: Basic operations <ul><li>int size( ); </li></ul><ul><li>boolean isEmpty( ); </li></ul><ul><li>boolean contains(Object element); </li></ul><ul><li>boolean add(Object element); // Optional </li></ul><ul><li>boolean remove(Object element); // Optional </li></ul><ul><li>Iterator iterator( ); </li></ul>
  7. 7. Collection: Iterator <ul><li>boolean hasNext( ); </li></ul><ul><ul><li>// true if there is another element </li></ul></ul><ul><li>Object next( ); </li></ul><ul><ul><li>// returns the next element (advances the iterator) </li></ul></ul>void remove( ); // Optional // removes the element returned by next } public interface Iterator {
  8. 8. Using an Iterator <ul><li>static void printAll (Collection coll) { Iterator iter = coll.iterator( ); while (iter.hasNext( )) { System.out.println(iter.next( ) ); } } </li></ul><ul><li>Note that this code is polymorphic--it will work for any collection </li></ul>
  9. 9. Collection: Bulk operations <ul><li>boolean containsAll(Collection c); </li></ul><ul><li>boolean addAll(Collection c); // Optional </li></ul><ul><li>boolean removeAll(Collection c); // Optional </li></ul><ul><li>boolean retainAll(Collection c); // Optional </li></ul><ul><li>void clear( ); // Optional </li></ul><ul><li>addAll , removeAll , retainAll return true if the object receiving the message was modified </li></ul>
  10. 10. Mixing Collection types <ul><li>Note that most methods, such as boolean containsAll(Collection c); are defined for any type of Collection , and take any type of Collection as an argument </li></ul><ul><li>This makes it very easy to work with different types of Collections </li></ul>
  11. 11. singleton <ul><li>Collections.singleton(e) returns an immutable set containing only the element e </li></ul><ul><li>c.removeAll(Collections.singleton(e)); will remove all occurrences of e from the Collection c </li></ul>
  12. 12. Collection: Array operations <ul><li>Object[ ] toArray( ); </li></ul><ul><ul><li>creates a new array of Object s </li></ul></ul><ul><li>Object[ ] toArray(Object a[ ]); </li></ul><ul><ul><li>Allows the caller to provide the array </li></ul></ul><ul><li>Examples: </li></ul><ul><ul><li>Object[ ] a = c.toArray( ); </li></ul></ul><ul><ul><li>String[ ] a; a = (String[ ]) c.toArray(new String[0]); </li></ul></ul>
  13. 13. The List interface <ul><li>A List is ordered and may have duplicates </li></ul><ul><li>Operations are exactly those for Collections </li></ul>int size( ); boolean isEmpty( ); boolean contains(Object e); boolean add(Object e); boolean remove(Object e); Iterator iterator( ); boolean containsAll(Collection c); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear( ); Object[ ] toArray( ); Object[ ] toArray(Object a[ ]);
  14. 14. List implementations <ul><li>List is an interface; you can’t say new List ( ) </li></ul><ul><li>There are two implementations: </li></ul><ul><ul><li>LinkedList gives faster insertions and deletions </li></ul></ul><ul><ul><li>ArrayList gives faster random access </li></ul></ul><ul><li>It’s poor style to expose the implementation, so: </li></ul><ul><li>Good: List list = new LinkedList ( ); Bad: LinkedList list = new LinkedList ( ); </li></ul>
  15. 15. Inherited List methods <ul><li>list.remove(e) removes the first e </li></ul><ul><li>add and addAll add to the end of the list </li></ul><ul><li>To append one list to another: </li></ul><ul><ul><li>list1.addAll(list2); </li></ul></ul><ul><li>To append two lists into a new list: </li></ul><ul><ul><li>List list3 = new ArrayList(list1); list3.addAll(list2); </li></ul></ul><ul><li>Again, it's good style to hide the implementation </li></ul>
  16. 16. List: Positional access <ul><li>Object get(int index); // Required -- // the rest are optional </li></ul><ul><li>Object set(int index, Object element); </li></ul><ul><li>void add(int index, Object element); </li></ul><ul><li>Object remove(int index); </li></ul><ul><li>abstract boolean addAll(int index, Collection c); </li></ul><ul><li>These operations are more efficient with the ArrayList implementation </li></ul>
  17. 17. List: Searching <ul><li>int indexOf(Object o); </li></ul><ul><li>int lastIndexOf(Object o); </li></ul><ul><li>equals and hashCode work even if implementations are different </li></ul>
  18. 18. Interface List: Iteration <ul><li>Iterators specific to Lists: </li></ul><ul><li>ListIterator listIterator( ); </li></ul><ul><li>ListIterator listIterator(int index); </li></ul><ul><ul><li>starts at the position indicated (0 is first element) </li></ul></ul><ul><li>Inherited methods: </li></ul><ul><ul><li>boolean hasNext( ); </li></ul></ul><ul><ul><li>Object next( ); </li></ul></ul><ul><ul><li>void remove( ); </li></ul></ul><ul><li>Additional methods: </li></ul><ul><ul><li>boolean hasPrevious() </li></ul></ul><ul><ul><li>Object previous() </li></ul></ul>
  19. 19. List : Iterating backwards <ul><li>boolean hasPrevious( ); </li></ul><ul><li>Object previous( ); </li></ul><ul><li>int nextIndex( ); </li></ul><ul><li>int previousIndex( ); </li></ul><ul><li>Think of the iterator as “between” elements </li></ul><ul><li>Hence, next followed by previous gives you the same element each time </li></ul>
  20. 20. List : More operations <ul><li>void add(Object o); </li></ul><ul><ul><li>Inserts an object at the cursor position </li></ul></ul><ul><li>Object set(Object o); // Optional </li></ul><ul><ul><li>Replace the current element; return the old one </li></ul></ul><ul><li>Object remove(int index); // Optional </li></ul><ul><ul><li>Remove and return the element at that position </li></ul></ul>
  21. 21. List: Range-view <ul><li>List subList(int from, int to); allows you to manipulate part of a list </li></ul><ul><li>A sublist may be used just like any other list </li></ul>
  22. 22. <ul><li>Thank you </li></ul>

×