Java, Utilities

Collection-Klassen


Collections

Interfaces:

Klassen:

Collection

Collection, Set, List

Collection:

public interface Collection

  public boolean add(Object o)
  public boolean remove(Object o)
  public boolean contains(Object o)
  public Iterator iterator()
public interface Collection< E >

  public boolean add(E o)
  public boolean remove(E o)
  public boolean contains(E o)
  public Iterator<E> iterator()

Iterator:

public interface Iterator

  public boolean hasNext()
  public Object next()
public interface Iterator< E >

  public boolean hasNext()
  public E next()

List: Duplikate sind zugelassen

public interface List
extends Collection

  public boolean add(Object o)
  public Object get()
  public boolean remove(Object o)
  public boolean contains(Object o)
  public Iterator iterator()
public interface List< E >
extends Collection< E >

  public boolean add(E o)
  public E get()
  public boolean remove(E o)
  public boolean contains(E o)
  public Iterator<E> iterator()

Set: keine Duplikate enthalten

public interface Set
extends Collection

  public boolean add(Object o)
  public boolean remove(Object o)
  public boolean contains(Object o)
  public Iterator iterator()
public interface Set< E >
extends Collection< E >

  public boolean add(E o)
  public boolean remove(E o)
  public boolean contains(E o)
  public Iterator<E> iterator()

Comparator: Vergleichsmethode bei Sorted*

public interface Comparator

int compare(Object o1, Object o2) 

boolean equals(Object obj) 
public interface Comparator< T >

int compare(T o1, T o2) 

boolean equals(Object obj) 
Map

Map, SortedMap

Map: (Schlüssel,Wert)-Paare

public interface Map

  public boolean put(Object key, Object value)
  public Object get(Object key)
  public boolean remove(Object key)
  public boolean containsKey(Object key)
  public boolean containsValue(Object value)
  public Set keySet()
  public Collection values()
  public Set entrySet()
public interface Map< K, V >

  public boolean put(K key, V value)
  public V get(K key)
  public boolean remove(K key)
  public boolean containsKey(K key)
  public boolean containsValue(V value)
  public Set<K> keySet()
  public Collection<V> values()
  public Set< Map.Entry<K,V> > entrySet()

Objekte, die als Schlüssel (key) verwendet werden, müssen folgende Bedingungen erfüllen:

Beispiel: Konten als HashMap

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

public class Konten {

    private HashMap konten = null;

    public Konten() {
	konten = new HashMap();
    }

    public void buche(int nummer, double betrag) {
        Integer n = new Integer(nummer);
        Object v = konten.get(n);
        double s = 0.0;
        if (v instanceof Double) {
	    s = ((Double)v).doubleValue();
	} 
        konten.put(n, new Double(s+betrag));
    }

    public double stand(int nummer) {
        Integer n = new Integer(nummer);
        Object v = konten.get(n);
        Double s = null;
        if (v instanceof Double) {
	    s = (Double) v;
	} else {
	    s = new Double(0.0);
	}
	return s.doubleValue();
    }

    public String toString(int nummer) {
	String s = "";
        s += "<Konto"; 
	s += " nummer='" + nummer + "'";
	s += " stand='" + stand(nummer) + "'";
	s += " />";
	return s;
    }

    public String toString() {
	String s = "";
        s += "<Konten >\n"; 
        for (Iterator i = (konten.keySet()).iterator(); i.hasNext(); ) {
            Object e = i.next();
            Integer n = (Integer)e;
	    int nummer = n.intValue();
	   s += toString(nummer) + "\n";
	}
	s += "</Konten>";
	return s;
    }
}

Beispiel: Konten als HashMap mit Generics von JDK 1.5

import java.util.HashMap;
import java.util.Map;

public class Konten {

    private Map<Integer,Double> konten = null;

    public Konten() {
	konten = new HashMap<Integer,Double>();
    }

    public void buche(int nummer, double betrag) {
        double s = 0.0;
        if ( konten.get(nummer) != null ) {
           s = konten.get(nummer); 
        }
        konten.put(nummer, (s+betrag) );
    }

    public double stand(int nummer) {
        double s = konten.get(nummer);
	return s; 
    }

    public String toString(int nummer) {
	String s = "";
        s += "<Konto"; 
	s += " nummer='" + nummer + "'";
	s += " stand='" + stand(nummer) + "'";
	s += " />";
	return s;
    }

    public String toString() {
	String s = "";
        s += "<Konten >\n"; 
        for ( Integer nummer :  konten.keySet() ) {
	    s += toString(nummer) + "\n";
	}
	s += "</Konten>";
	return s;
    }

}

Collections: statische Hilfsmethoden

public class Collections
extends Object

static int binarySearch(List list, Object key) 
static int binarySearch(List list, Object key, Comparator c) 

static void copy(List dest, List src) 
static void fill(List list, Object obj) 

static int indexOfSubList(List source, List target) 
static int lastIndexOfSubList(List source, List target) 

static Object max(Collection coll) 
static Object max(Collection coll, Comparator comp) 

static Object min(Collection coll) 
static Object min(Collection coll, Comparator comp) 

static List nCopies(int n, Object o) 
static boolean replaceAll(List list, Object oldVal, Object newVal) 

static void reverse(List list) 
static Comparator reverseOrder() 

static void rotate(List list, int distance) 
static void shuffle(List list) 
static void shuffle(List list, Random rnd) 

static void sort(List list) 
static void sort(List list, Comparator c) 

static void swap(List list, int i, int j) 
static Set singleton(Object o) 
static List singletonList(Object o) 
static Map singletonMap(Object key, Object value) 

static Collection synchronizedCollection(Collection c) 
static List synchronizedList(List list) 
static Map synchronizedMap(Map m) 
static Set synchronizedSet(Set s) 
static SortedMap synchronizedSortedMap(SortedMap m) 
static SortedSet synchronizedSortedSet(SortedSet s) 

static Collection unmodifiableCollection(Collection c) 
static List unmodifiableList(List list) 
static Map unmodifiableMap(Map m) 
static Set unmodifiableSet(Set s) 
static SortedMap unmodifiableSortedMap(SortedMap m) 
static SortedSet unmodifiableSortedSet(SortedSet s) 

© Universität Mannheim, Rechenzentrum, 1998-2005.

Heinz Kredel

Last modified: Sun Feb 5 18:44:30 CET 2006