Java Collections: Types, Interfaces, Implementations, and Iterators, Study notes of Computer Science

An outline and explanation of java collections, including major types (sets, bags, lists, maps), interfaces (collection, set, list, map), implementation classes (hashset, linkedhashset, treeset, arraylist, linkedlist, hashmap, identityhashmap, linkedhashmap, treemap, hashtable), iterators, input/output classes, and ordering. It covers the methods and descriptions of each interface and implementation class, as well as examples and use cases.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-gja
koofers-user-gja 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1/14/2009
1
Collection and
Input/Output Classes
CS 3331
Outline
Collection classes
Collection interfaces
Implementation classes
Iterators
Ordering
Input/output classes
Collection Classes in Java
Major types of collections
Sets, bags, lists, maps
Defined in the java.util package
Interfaces of collections
Collection Interface
Method Description
add(o) Add a new element
addAll(c) Add all elements of c
remove(o) Remove an element
removeAll(c) Remove all elements found in c
retainAll(c) Retain only elements found in c
clear() Remove all elements
contains(o) Membership testing
containsAll(c) Membership testing
isEmpty() Whether it is empty
size() The number of elements
iterator() Return an iterator
Set Interface
Method Description
add(o) Add an element if not already present
addAll(c) Add each elements of c if not present
List Interface
Method Description
add(i, o) Insert o at the i-th position
add(o) Append o at the end
addAll(i, c) Insert all elements of c starting at the i-th position
addAll(c) Append all elements of c at the end
remove(i) Remove i-th element
remove(o) Remove the first occurrence of o
set(i, o) Replace i-th element with o
get(i) Return i-th element
indexOf(o) Return the index of the first occurrence of o
lastIndexOf(o) Return the index of the last occurrence of o
listIteratpr() Return a list iterator
listIterator(i) Return a list iterator for the sublist starting from i
subList(i, j) Retrun a sublist between index iand j
pf3
pf4
pf5

Partial preview of the text

Download Java Collections: Types, Interfaces, Implementations, and Iterators and more Study notes Computer Science in PDF only on Docsity!

Collection and

Input/Output Classes

CS 3331

Outline

• Collection classes

  • Collection interfaces
  • Implementation classes
  • Iterators
  • Ordering

• Input/output classes

Collection Classes in Java

• Major types of collections

  • Sets, bags, lists, maps
  • Defined in the java.util package

• Interfaces of collections

Collection Interface

Method Description add(o) Add a new element addAll(c) Add all elements of c remove(o) Remove an element removeAll(c) Remove all elements found in c retainAll(c) Retain only elements found in c clear() Remove all elements contains(o) Membership testing containsAll(c) Membership testing isEmpty() Whether it is empty size() The number of elements iterator() Return an iterator

Set Interface

Method Description add(o) Add an element if not already present addAll(c) Add each elements of c if not present

List Interface

Method Description add(i, o) Insert o at the i-th position add(o) Append o at the end addAll(i, c) Insert all elements of c starting at the i-th position addAll(c) Append all elements of c at the end remove(i) Remove i-th element remove(o) Remove the first occurrence of o set(i, o) Replace i-th element with o get(i) Return i-th element indexOf(o) Return the index of the first occurrence of o lastIndexOf(o) Return the index of the last occurrence of o listIteratpr() Return a list iterator listIterator(i) Return a list iterator for the sublist starting from i subList(i, j) Retrun a sublist between index iand j

Map Interface

Method Description put(k,v) Associate v with k remove(k) Remove the mapping for k clear() Remove all mappings get(k) The value associated with k containsKey(k) Whether contains a mapping for k containsValue(v) Whether contains a mapping to v size() The number of pairs isEmpty() Whether it is empty ….

Map Interface (Cont.)

Method Description entrySet() Set of key-value pairs keySet() Set of keys values() The collection of values k k k . . . kn v v v . . . vn keySet() values() entrySet()

Outline

 Collection classes

 Collection interfaces

  • Implementation classes
  • Iterators
  • Ordering

• Input/output classes

Implementation of Collections

• Why different implementations?

Class Interface Description HashSet Set Hash table LinkedHashSet Set Hash table & DLL TreeSet SortedSet Balanced binary tree

Implementation (Cont.)

• Lists

Class Interface Description ArrayList List Resizable array LinkedList List Doubly linked list Vector List Legacy of JDK 1.

Implementation (Cont.)

• Maps

Class Interface Description HashMap Map Hash table IdentityHashMap Map Hash table with identity comparison LinkedHashMap Map Hash table and DLL TreeMap SortedMap Balanced binary tree Hashtable Map Legacy of JDK 1.

Sorted Collections

• Interfaces

– SortedSet and SortedMap

• Implementations

– TreeSet and TreeMap

• Example

SortedSet s1 = new SortedSet(); SortedSet s2 = new SortedSet(new PersonComparator()); SortedMap m1 = new SortedMap(); SortedMap m2 = new SortedMap(new PersonComparator());

SortedSet Interface

Method Description comparator() Return the comparator first() Return the first (lowest) element last() Return the last (highest) element headSet(o) Return elements less than o tailSet(o) Return elements greater than or equal to o subSet(o1,o2) Return elements between o1 and o

SortedMap Interface

Method Description comparator() Return the comparator firstKey() Return the first (lowest) key lastKey() Return the last (highest) key headMap(k) Return maplets less than k tailMap(k) Return maplets greater than or equal to k subMap(k1,k2) Return maplets between k1 and k

Outline

 Collection classes

 Collection interfaces

 Implementation classes

 Iterators

 Ordering

• Input/output classes

Input/Output Classes

• Two types of input/output

– Stream I/O

  • Sequential reading and writing
  • Opened for reading or writing, but not both
  • Byte streams vs. character streams

– Random access I/O

  • Non-sequential reading and writing
  • Can be opened for both reading and writing

Byte Streams

Character Streams Example Usage

• Reading lines from file

String fileName = …; BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(fileName))); String line; while ((line = reader.readLine()) != null) { // do something with line … }

Decorator Pattern

  • To add additional responsibility or capability to an object

dynamically