Benutzer-Werkzeuge

Webseiten-Werkzeuge


becki:linux:java

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
becki:linux:java [2010-04-30 14:18]
becki
becki:linux:java [2010-10-19 12:50] (aktuell)
becki
Zeile 1: Zeile 1:
 ====== Java == ====== Java ==
-===== Links ==+===== Related Pages ==
  
-[[sunkurs]] [[comparison table]]+<​pagelist>​ 
 +  * [[java plumbing]]  
 +  * [[java web programming]] 
 +  * [[apache click]] 
 +  * [[apache tomcat]] 
 +  * [[jrunscript]] 
 +</​pagelist>​
  
 ===== Java Versions and Compatibility == ===== Java Versions and Compatibility ==
Zeile 278: Zeile 284:
 String readLine() throws IOException String readLine() throws IOException
 </​code>​ </​code>​
 +
 +===== Collections ==
 +==== Overview ==
 +
 +[[javatut>​reallybigindex.html#​collections|Chapter in the Java tutorial]]
 +
 +|  //​[[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 |
 +
 +==== Implementations ==
 +
 +  * [[javatut>​collections/​implementations/​|Overview]]
 +  * [[http://​www.developer.com/​java/​article.php/​3829891|Selecting the Best Java Collection Class for Your Application]]
 +  * [[javatut>​collections/​implementations/​map.html|General purpose map implementations]]
 +
 +==== Methods ==
 +
 +<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
  
 ===== Threads == ===== Threads ==
Zeile 697: Zeile 806:
 ===== Further Reading / 2do == ===== Further Reading / 2do ==
  
-  * [[Java ​Web Programming]]+  * Intro to the [[ibm>​java/​library/​j-5things9.html|Java Scripting API]] with [[jrunscript]]
   * Doc general Java strategy: Use [[Apache Pivot]] for the GUI, [[JUnit]] for testing, [[Apache Ant]] for building, JWS for distribution and [[Groovy]] for coding   * Doc general Java strategy: Use [[Apache Pivot]] for the GUI, [[JUnit]] for testing, [[Apache Ant]] for building, JWS for distribution and [[Groovy]] for coding
   * [[http://​www.linux-magazin.de/​Heft-Abo/​Ausgaben/​2008/​08/​Reiches-Angebot|Webentwicklung mit Java]]: An Overview (german)   * [[http://​www.linux-magazin.de/​Heft-Abo/​Ausgaben/​2008/​08/​Reiches-Angebot|Webentwicklung mit Java]]: An Overview (german)
becki/linux/java.1272637099.txt.gz · Zuletzt geändert: 2010-04-30 14:18 von becki

Impressum - Datenschutzerklärung