Rabu, 30 Maret 2011

Java Collection Framework

Java Collection adalah framework yang merupakan kumpulan-kumpulan kelas struktur data seperti array linked list, stack, tree dan lain-lain seluruh kelas java merupakan turunan dari java.lang.itterible sehingga dapat digunakan perulangan foreach.
langsung aja gan kita belajar dengan memahami sourcenya disini :
package src.sigma.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

public class ListExample {
public static void main(String[] arg){
// List : order, duplikasi

List listDay=new ArrayList();
listDay.add("Rabu");
listDay.add("Kamis");
listDay.add("Senin");
listDay.add("Selasa");
listDay.add("Jumat");
listDay.add("Sabtu");
listDay.add("Minggu");
listDay.add("Rabu");


System.out.println(listDay);
// ArrayList : order, un-sorted , allow duplikasi
// loading collection fast

List listDay1=new LinkedList();
listDay1.add("Rabu");
listDay1.add("Kamis");
listDay1.add("Senin");
listDay1.add("Selasa");
listDay1.add("Jumat");
listDay1.add("Sabtu");
listDay1.add("Minggu");
listDay1.add("Rabu");

System.out.println(listDay1);
// ArrayList : order, un-sorted , allow duplikasi
// insert&deletion fast

String[] days={"Senin","Selasa","Rabu",
"Kamis","Jumat","Sabtu","Minggu","Selasa"};
Vector vector=new Vector(Arrays.asList(days));
System.out.println(vector);
// Vector : no thread safe
}
}



package src.sigma.collection;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class MapExample {
public static void main(String arg[]){
// Map : penggunaan key & value
// key --> harus unik but not for value

Map listDay=new HashMap();
listDay.put(4,"Kamis");
listDay.put(2,"Selasa");
listDay.put(1,"Senin");
listDay.put(3,"Rabu");
listDay.put(5,"Jumat");
listDay.put(7,"Minggu");
listDay.put(6,"Sabtu");
listDay.put(null,null);
listDay.put(null,null);
System.out.println(listDay);
//HashMap : sorted , null key at least 1,no duplikasi
// no thread safe ---> ConcurrentHashMap

Map listDay2=new Hashtable();
listDay2.put(4,"Kamis");
listDay2.put(2,"Selasa");
listDay2.put(1,"Senin");
listDay2.put(3,"Rabu");
listDay2.put(5,"Jumat");
listDay2.put(7,"Minggu");
listDay2.put(6,"Sabtu");
listDay2.put(6,"Sabtu");
listDay.put(null,null);
listDay.put(null,null);

System.out.println(listDay2);
//Hashtable : un-sorted, un-order , null key not allowed,
// no duplikasi


Map listDay3=new LinkedHashMap();
listDay3.put(4,"Kamis");
listDay3.put(2,"Selasa");
listDay3.put(1,"Senin");
listDay3.put(3,"Rabu");
listDay3.put(5,"Jumat");
listDay3.put(7,"Minggu");
listDay3.put(6,"Sabtu");
listDay3.put(6,"Sabtu");
listDay.put(null,null);
listDay.put(null,null);

System.out.println(listDay3);
//LinkedHashMap : un-sorted,Order
// no-duplikasi,no null key

Map listDay4=new TreeMap();
listDay4.put(4,"Kamis");
listDay4.put(2,"Selasa");
listDay4.put(1,"Senin");
listDay4.put(3,"Rabu");
listDay4.put(5,"Jumat");
listDay4.put(7,"Minggu");
listDay4.put(6,"Sabtu");
listDay4.put(6,"Sabtu");
listDay.put(null,null);
listDay.put(null,null);

System.out.println(listDay4);
//TreeMap : sorted ,no null key, no duplikasi

}
}


package src.sigma.collection;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;

public class QueueExample {
public static void main(String arg[]){
String[] days={"Selasa","Jumat","Rabu",
"Kamis","Sabtu","Senin","Minggu","Selasa"};

Queue listDays=new PriorityQueue(Arrays.asList(days));
System.out.println(listDays);

listDays.peek(); // only retrieve head , no remove
System.out.println(listDays);
listDays.poll(); // retrieve head & remove
System.out.println(listDays);
//PriorityQueue : un-sorted, un-order
}
}




package src.sigma.collection;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class SetExample {
public static void main(String arg[]){
String[] days={"Senin","Selasa","Rabu",
"Kamis","Jumat","Sabtu","Minggu","Selasa"};

// Set : tidak menerima duplikasi data
Set listDays=new HashSet(Arrays.asList(days));
// HashSet : un-sorted,un-order,insert deleting fast
System.out.println(listDays);

Set listDays2=new LinkedHashSet(Arrays.asList(days));
// LinkedHashSet : un-sorted, Ordered,Loading fast
System.out.println(listDays2);

Set listDays3=new TreeSet(Arrays.asList(days));
// TreeSet : sorted , un-order,insert deleting fast
System.out.println(listDays3);
}
}




selamat mencoba gan..

created by Rizki Agung Nugraha

1 komentar:

Posting Komentar