Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
|
becki:linux:sunkurs [2010-03-16 09:12] becki |
becki:linux:sunkurs [2010-10-19 12:39] (aktuell) becki |
||
|---|---|---|---|
| Zeile 6: | Zeile 6: | ||
| ===== Language Fundamentals ====== | ===== Language Fundamentals ====== | ||
| - | [[javatut>java/nutsandbolts/datatypes.html|Table of Primitive Data Types]] | + | [[javatut>java/nutsandbolts/datatypes.html|Table of Primitive Data Types]] | [[javatut>java/nutsandbolts/_keywords.html|Java Language Keywords]] |
| - | [[javatut>java/nutsandbolts/_keywords.html|Java Language Keywords]] | + | |
| ==== unsorted ===== | ==== unsorted ===== | ||
| Zeile 712: | Zeile 711: | ||
| ==== Collections ===== | ==== Collections ===== | ||
| - | FIXME Move this chapter to [[java]] | + | See [[java#Collections]] |
| - | + | ||
| - | [[javatut>reallybigindex.html#collections|Chapter in the Java tutorial]] [[http://www.developer.com/java/article.php/3829891|Selecting the Best Java Collection Class for Your Application]] | + | |
| - | + | ||
| - | | //[[javaref>api/java/util/Collection.html|Collection]]// |||| //[[javaref>api/java/util/Map.html|Map]]// || | + | |
| - | | //[[javaref>api/java/util/Set.html|Set]]// || //[[javaref>api/java/util/List.html|List]]// | //[[javaref>api/java/util/Queue.html|Queue]]// | || | + | |
| - | | | //[[javaref>api/java/util/SortedSet.html|SortedSet]]// | | | | //[[javaref>api/java/util/SortedMap.html|SortedMap]]// | | + | |
| - | | HashSet LinkedHashSet | TreeSet | LinkedList Vector ArrayList | LinkedList, PriorityQueue | HashTable LinkedHashMap HashMap | TreeMap | | + | |
| - | + | ||
| - | <code java> | + | |
| - | // important methods of List: | + | |
| - | boolean add(E e) | + | |
| - | void add(int index, E element) | + | |
| - | boolean contains(Object o) | + | |
| - | E get(int index) | + | |
| - | int indexOf(Object o) | + | |
| - | Iterator<E> iterator() | + | |
| - | E remove(int index) | + | |
| - | boolean remove(Object o) // Removes the first occurrence | + | |
| - | Object[] toArray() | + | |
| - | <T> T[] toArray(T[] a) | + | |
| - | + | ||
| - | // important methods of Map | + | |
| - | boolean containsKey(Object key) | + | |
| - | boolean containsValue(Object value) | + | |
| - | V get(Object key) | + | |
| - | Set<K> keySet() // Returns a set of the keys | + | |
| - | V put(K key, V value) // there is no add! | + | |
| - | V remove(Object key) | + | |
| - | + | ||
| - | // important methods of Queue: | + | |
| - | boolean offer(E o) // Inserts the specified element at the end; also add() possible | + | |
| - | E peek() // Retrieves, but does not remove, | + | |
| - | E poll() // Retrieves and removes from the beginning; remove() also possible | + | |
| - | </code> | + | |
| - | + | ||
| - | * Set: has no special order and no duplicates are permitted, not indexed; methods ar a subset of List (no index operations) | + | |
| - | * List: ordered, duplicates are permitted, indexed | + | |
| - | * Map does not implement the collection interface | + | |
| - | * 'Sorted' is a subset of 'Ordered' | + | |
| - | * Ordering: | + | |
| - | * //Lists// are always ordered by index | + | |
| - | * //Linked// collections are always ordered by insertion or last access | + | |
| - | * //Tree// collections are always sorted | + | |
| - | * List can made thread-save by Collections.synchronizedList() | + | |
| - | * Tutorial: Vector is a legacy class; API: Vector is a normal class; Hashtable is similar; Vector also implements List | + | |
| - | * HashSet & LinkedHashSet: Overwrite hashCode() so that equal objects provide the same hashcode | + | |
| - | * LinkedList implements //List// and //Queue// | + | |
| - | * //Queue// has Fifo (not stack) behaviour! | + | |
| - | * All Interfaces have a size() method | + | |
| - | * PriorityQueue sorts Elements by natural order using Comparable | + | |
| - | * Elements for SortedSet/TreeSet, SortedMap/TreeMap | + | |
| - | + | ||
| - | PriorityQueue must implement the Comparable interface! | + | |
| - | * String implements interface Comparable<String> and Integer implements Comparable<Integer> -> Trying to add both Types to any untyped (old style) Tree-Container results in a ClassCastException | + | |
| - | * Whereas adding only the same Type (eg Strings) works but gives a compiler warning. (I assume the type is detected at run time by reflection and the Object to compare is casted to String before compareTo() is called.) | + | |
| - | + | ||
| - | === Sorting and Searching == | + | |
| - | == Sorting with the Comparable Interface == | + | |
| - | + | ||
| - | To sort a //List// (eg ArrayList) or an array use: | + | |
| - | <code java> | + | |
| - | static void Collections.sort(List<T> list); | + | |
| - | static void Arrays.sort(allPrimitivesAndObject[] a); | + | |
| - | </code> | + | |
| - | The List interface has no sort method! All elements in the List or array must implement the Comparable interface: | + | |
| - | + | ||
| - | <code java> | + | |
| - | Interface Comparable<T> { | + | |
| - | int compareTo(T o); | + | |
| - | } | + | |
| - | </code> | + | |
| - | Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Keep in mind that compareTo accepts Type T (the generic of the List) wheras equals always only accepts type Object! | + | |
| - | + | ||
| - | == Sorting with the Comparator Interface == | + | |
| - | + | ||
| - | <code java> | + | |
| - | static void Collections.sort(List<T> list, Comparator<? super T> c); | + | |
| - | static void Arrays.sort(T[] a, Comparator<? super T> c); // T is an Object, no primitives! | + | |
| - | + | ||
| - | Interface Comparator<T> { | + | |
| - | int compare(T o1, T o2); | + | |
| - | } | + | |
| - | </code> | + | |
| - | + | ||
| - | == Searching == | + | |
| - | + | ||
| - | <code java> | + | |
| - | static int Collections.binarySearch(List<T> list, T key[, Comparator<? super T> c]); | + | |
| - | static int Arrays.binarySearch(allPrimitivesAndObject[] a, allPrimitivesAndObject key); | + | |
| - | static int Arrays.binarySearch(T[] a, T key, Comparator<? super T> c); | + | |
| - | </code> | + | |
| - | Call binarySearch() with the array or List as first param, the key as second, and, if not all items implement the Comparable interface, provide a Comparator as 3rd parameter. List or array has to be sorted before binarySearch can be called, otherwise the result is not predictable! If the element is not found, binarySearch returns -length-1 | + | |
| ==== Generics ===== | ==== Generics ===== | ||