Version: 2015-05-27 Algoritmer och datastrukturer
Datastrukturer och standardalgoritmer i Java
Dokumentet listar ett urval av konstruktorer och metoder för några vanliga Javaklasser.
Class ArrayList<E>
Constructors
ArrayList()
Constructs an empty list.
Methods
void add(int index, E element)
Inserts the specified element at the specified position in this list.
boolean add(E o)
Appends the specified element to the end of this list.
void clear()
Removes all of the elements from this list.
boolean contains(E elem)
Returns true if this list contains the specified element.
E get(int index)
Returns the element at the specified position in this list.
int indexOf(E elem)
Searches for the first occurence of the given argument, testing for equality using the equals method.
boolean isEmpty()
Tests if this list has no elements.
Iterator iterator()
Returns an iterator over the elements in this list in proper sequence.
E remove(int index)
Removes the element at the specified position in this list.
E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
int size()
Returns the number of elements in this list.
E[] toArray()
Returns an array containing all of the elements in this list in the correct order.
E[] toArray(E[] a)
Returns an array containing all of the elements in this list in the correct order;
the runtime type of the returned array is that of the specified array.
Version: 2015-05-27 Algoritmer och datastrukturer
Class LinkedList<E>
Constructors
LinkedList()
Constructs an empty list.
Methods
boolean addFirst(E o)
Inserts the specified element at the beginning of this list..
void add(int index, E element)
Inserts the specified element at the specified position in this list.
boolean addLast(E o)
Appends the specified element to the end of this list.
E peek()
Returns, but does not remove, the head (first element) of this list, returning null if this queue is empty.
E poll()
Returns and removes the head (first element) of this list, or returns null if it is empty.
E getFirst()
Returns the first element in this list.
E getLast()
Returns the last element in this list.
E removeFirst()
Removes and returns the first element from this list.
E removeLast()
Removes and returns the last element from this list.
void clear()
Removes all of the elements from this list.
boolean contains(E elem)
Returns true if this list contains the specified element.
boolean isEmpty()
Tests if this list has no elements.
Iterator iterator()
Returns an iterator over the elements in this list in proper sequence.
int size()
Returns the number of elements in this list.
Version: 2015-05-27 Algoritmer och datastrukturer
Interface Iterator<E>
public boolean hasNext()
Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)
public E next()
Returns the next element in the iteration.
Throws: NoSuchElementException - iteration has no more elements.
public void remove()
Removes from the underlying collection the last element returned by the iterator. This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.
Classes HashSet<E> and TreeSet<E>
Constructors HashSet()
Constructs a new, empty HashSet.
TreeSet()
Constructs a new, empty TreeSet.
Methods (both classes) boolean add(E o)
Adds the specified element to this set if it is not already present.
void clear()
Removes all of the elements from this set.
boolean contains(E o)
Returns true if this set contains the specified element.
boolean isEmpty()
Returns true if this set contains no elements.
Iterator<E> iterator()
Returns an iterator over the elements in this set.
boolean remove(E o)
Removes the specified element from this set if it is present.
int size()
Returns the number of elements in this set (its cardinality).
Version: 2015-05-27 Algoritmer och datastrukturer
interface java.lang.Collection<E>
boolean add(E e)
Ensures that this collection contains the specified element (optional operation).
boolean addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this collection (optional operation).
void clear()
Removes all of the elements from this collection (optional operation).
boolean contains(Object o)
Returns true if this collection contains the specified element.
boolean equals(Object o)
Compares the specified object with this collection for equality.
int hashCode()
Returns the hash code value for this collection.
boolean isEmpty()
Returns true if this collection contains no elements.
Iterator<E> iterator()
Returns an iterator over the elements in this collection.
boolean remove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation).
int size()
Version: 2015-05-27 Algoritmer och datastrukturer
Classes HashMap<K,V> and TreeMap<K,V>
Constructors HashMap()
Constructs an empty HashMap.
TreeMap()
Constructs an empty TreeMap.
Methods (both classes) void clear()
Removes all mappings from this map.
boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key.
boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
Set<Map.Entry<K,V>> entrySet()
Returns a collection view of the mappings contained in this map.
V get(Object key)
Returns the value to which the specified key is mapped in this hash map, or null if the map contains no mapping for this key.
boolean isEmpty()
Returns true if this map contains no key-value mappings.
Set<K> keySet()
Returns a set view of the keys contained in this map.
V put(K key, V value)
Associates the specified value with the specified key in this map.
V remove(Object key)
Removes the mapping for this key from this map if present.
int size()
Returns the number of key-value mappings in this map.
Collection<V> values()
Returns a collection view of the values contained in this map.
Version: 2015-05-27 Algoritmer och datastrukturer
Class Stack<E>
Constructors Stack()
Constructs an empty Stack.
Methods
boolean isEmpty()
Tests if this stack is empty.
E peek()
Looks at the object at the top of this stack without removing it from the stack.
E pop()
Removes the object at the top of this stack and returns that object as the value of this function.
E push(E item)
Pushes an item onto the top of this stack.
Interface Queue<E>
This interface is implemented by LinkedList and PriorityQueue .
Methods
boolean add(E o)
Adds the specified element to this queue.
boolean isEmpty()
Tests if this queue has no elements.
E peek()
Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
E poll()
Retrieves and removes the head of this queue, or returns null if it is empty.
int size()
Returns the number of elements in this queue.
Version: 2015-05-27 Algoritmer och datastrukturer
Class PriorityQueue<E>
Constructors
PriorityQueue()
Creates a PriorityQueue that orders its elements according to their natural ordering (using Comparable).
PriorityQueue(Collection<E> c)
Creates a PriorityQueue containing the elements in the specified collection.
Methods
boolean add(E o)
Adds the specified element to this queue.
boolean isEmpty()
Tests if this queue has no elements.
E peek()
Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
E poll()
Retrieves and removes the head of this queue, or null if this queue is empty.
int size()
Returns the number of elements in this collection.
Class Collections
Methods
static <T> boolean
addAll(Collection<? super T> c, T... elements) Adds all of the specified elements to the specified collection.
static <T> void
copy(List<? super T> dest, List<? extends T> src) Copies all of the elements from one list into another.
static boolean disjoint(Collection<?> c1, Collection<?> c2) Returns true if the two specified collections have no elements in common.
static <T> T
max(Collection<? extends T> coll, Comparator<? super T> comp) Returns the maximum element of the given collection, according to the order induced by the specified comparator.
static <T> T
min(Collection<? extends T> coll, Comparator<? super T> comp) Returns the minimum element of the given collection, according to the order induced by the specified comparator.
static void reverse(List<?> list)
Reverses the order of the elements in the specified list.
Version: 2015-05-27 Algoritmer och datastrukturer
static void rotate(List<?> list, int distance) Rotates the elements in the specified list by the specified distance.
static void shuffle(List<?> list)
Randomly permutes the specified list using a default source of randomness.
static <T> void
sort(List<T> list, Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator.
static void swap(List<?> list, int i, int j) Swaps the elements at the specified positions in the specified list.
Class DisjointSets Constructors
DisjointSets(int numElements) Creates numElements disjoint sets.
Methods
int find( int x ) Return the set containing x.
void union( int root1, int root2 ) Union two disjoint sets.
root1 and root2 are distinct and represent set names.
Class Pair<E,F>
Constructors
Pair(E x,F y) Creates a pair of x and y.
Components E first
Retrieves the first part of this pair.
F second
Retrieves the second part of this pair.
Class Random Constructors Random()
Creates a new random number generator.
Methods
int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive)
and the specified value (exclusive), drawn from this random number generator's sequence.