====== Collections ======
===== List =====
List myList = new ArrayList ();
String s1 = "Hello1";
String s2 = "Hello2";
myList.add(100);
myList.add(s1);
myList.add(s2);
myList.add(s1);
myList.add(1);
myList.add(2, "World");
myList.set(3, "Yes");
myList.add(null);
System.out.println("Size:" + myList.size() );
int pos = myList.indexOf(100);
int pos2 = myList.lastIndexOf(s2); // return −1 if no match was found.
for (Object object : myList)
{
System.out.println(object);
}
Iterator iterator = myList.iterator();
while (iterator.hasNext () )
{
String element = (String) iterator.next ();
System.out.println(element);
}
This is identical to:
for (Iterator iterator = myList.iterator(); iterator.hasNext (); )
{
String element = (String) iterator.next();
System.out.println(element);
}
The for statement in JDK 5 has been enhanced. It can iterate over a Collection without the need to call the iterator method. The syntax is
for (Type identifier : expression)
{
statement (s)
}
===== Set =====
List without duplicates
The add method of Set returns false if you try to add a duplicate element.
Set set = new HashSet ();
set.add ("Hello");
if (set.add ("Hello")) {
System.out.println ("addition successful");
} else {
System.out.println ("addition failed");
}
===== Queue =====
Queue queue = new LinkedList ();
queue.add("one");
queue.add("two");
queue.add ("three");
System.out.println(queue.remove () );
System.out.println(queue.remove () );
System.out.println(queue.remove () );
The code produces this result:
one
two
three
===== Map =====
Map map = new HashMap();
map.put ("1", "one");
map.put ("2", "two");
System.out.println(map.size() ); //print 2
System.out.println(map.get("1") ); //print "one"
Set keys = map.keySet();
// print the keys
for (Object object : keys) {
System.out.println(object);
}
===== Ma collection =====
public class Elephant implements comparable
{
public float weight;
public int age;
public float tuskLength;
**public int compareTo(Object obi)**
{
Elephant anotherElephant = (Elephant) obj;
if (this.weight > anotherElephant.weight)
{
return 1;
}
else if (this.weight < anotherElephant.weight)
{
return -1;
}
else
{
// both elephants have the same weight, now
// compare their age
return (this.age - anotherElephant.age);
}
}
}
public class ElephantTest
{
public static void main(String[] args)
{
Elephant elephant1 = new Elephant ();
elephant1.weight = 100.12F;
elephantl.age = 20;
Elephant elephant2 = new Elephant ();
elephant2.weight = 120.12F;
elephant2.age = 20;
Elephant elephant3 = new Elephant ();
elephant3.weight = 100.12F;
elephant3.age = 25;
Elephant[] elephants = new Elephant[3];
elephants[0] = elephant1;
elephants[1] = elephant2;
elephants[2] = elephant3;
System.out.println("Before sorting");
for (Elephant elephant : elephants) {
System.out.println(elephant.weight + ":" +
elephant.age);
}
Arrays.sort(elephants);
System.out.println("After sorting");
for (Elephant elephant : elephants) {
System.out.println(elephant.weight + ":" +
elephant.age);
}
}
}
===== Conversion =====
As an example, the following code converts a Queue to a List.
Queue queue = new LinkedList ();
queue.add("Hello");
queue.add("World");
List list = new ArrayList(queue);
And this converts a List to a Set.
List myList = new ArrayList ();
myList.add("Hello");
myList.add("World");
myList.add("World");
Set set = new HashSet(myList);
===== enum =====
public enum CustomerType
{
INDIVIDUAL,
ORGANIZATION
}
===== Collections synchronisées =====
Collections.synchronizedCollection(Collection c)
===== Bonnes pratiques =====
http://javatropbien.free.fr/index.php/2011/08/27/8-bonnes-pratiques-de-base-pour-un-developpeur-javaj2ee/\\
http://bokc-fr.blogspot.fr/2010/04/java-bonnes-pratiques.html\\
http://www.patate-chaude.fr/bonne-pratique-java-constructeurs-appels-methodes/\\
Sonar:
findbugs, checkstyle, pmd et Metrics
StringBuilder (non synchronisé) au lieu de StringBuffer.