Главная » soft, programming » Java collection usage

Java collection usage

Most Commonly Known Collections Usage Scenarios
ArrayList * Default choice of List implementation * To store a bunch of things * Repetitions matters * Insertion order matters * Best implementation in case of huge lists which are read intensive (elements are accessed more frequently than inserted deleted)
HashMap * Default choice of Map implementation * Majorly used for simple in-memory caching purpose.
Vector * Historical implementation of List * A good choice for thread-safe implementation
Hashtable * Similar to HashMap * Do not allow null values or keys * Entire map is locked for thread safety
HashSet * To store bunch of things * A very nice alternative for ArrayList if ** Do not want repetitions ** Ordering does not matter
TreeSet * To store bunch of things in sorted order * A very nice alternative for ArrayList if ** Do not want repetitions ** Sorted order
LinkedList * Sequential Access * Faster adding and deleting of elements * Slightly more memory than ArrayList * Add/Remove elements from both ends of the queue * Best alternative in case of huge lists which are more write intensive (elements added / deleted are more frequent than reading elements)
ArrayDeque * Random Access * Faster searching and retrieval of elements * Add/Remove elements from both ends of the queue * Best alternative in case of huge lists which are more read intensive
Stack * Similar to a Vector * Last-In-First-Out implementation
TreeMap * A very nice alternative for HashMap if sorted keys are important
WeakHashMap * The keys that are not referenced will automatically become eligible for garbage collection * Usually used for advanced caching techniques to store huge data and want to conserve memory
Arrays * A Utility class provided to manipulate arrays ** Searching ** Sorting ** Converting to other Collection types such as a List
Properties * Properties are exactly same as the Hashtable * Keys and Values are String * Can be loaded from a input stream * Usually used to store application properties and configurations
CopyOnWriteArrayList * A thread safe variant of ArrayList * Best use for ** Small lists which are read intensive ** requires thread-safety
ConcurrentHashMap * A thread safe variant of Hashtable * Best use for ** requires thread-safety ** Better performance at high load due to a better locking mechanism
ConcurrentSkipListMap * A thread safe variant of TreeMap * Best use for ** requires thread-safety
ConcurrentSkipListSet * A thread safe variant of TreeSet * Best use for ** Do not want repetitions ** Sorted order ** Requires thread-safety
CopyOnWriteArraySet * A thread-safe implementation of a Set * Best use for ** Small lists which are read intensive ** requires thread-safety ** Do not want repetitions
ConcurrentLinkedQueue * A thread-safe variant of PriorityQueue * Best use for ** Small lists ** No random access ** requires thread-safety
ConcurrentLinkedDeque «* A thread-safe variant of LinkedList * Best use for ** Small lists ** No random access ** Insertions, retrieval on both sides of the queue ** requires thread-safety»
ArrayBlockingQueue * Best use for Producer — Consumer type of scenarios with ** Lower capacity bound ** Predictable capacity * Has a bounded buffer. Space would be allocated during object creation
LinkedBlockingQueue * Best use for Producer — Consumer type of scenarios with ** Large capacity bound ** Unpredictable capacity * Upper bound is optional
LinkedTransferQueue * Can be used in situations where the producers should wait for consumer to receive elements. e.g. Message Passing
PriorityBlockingQueue «* Best use for Producer — Consumer type of scenarios with ** Large capacity bound ** Unpredictable capacity ** Consumer needs elements in sorted order
LinkedBlockingDeque * A Deque implementation of LinkedBlockingQueue ** Can add elements at both head and tail
SynchronousQueue * Both producer and consumer threads will have to wait for a handoff to occur. * If there is no consumer waiting. The element is not added to the collection.
DelayQueue * Similar to a normal LinkedBlockingQueue * Elements are implementations of Delayed interface * Consumer will be able to get the element only when it’s delay has expired

https://kdiv.wordpress.com/2013/01/22/java-collection-2/
https://kdiv.wordpress.com/2013/02/15/java-%D0%BA%D0%BE%D0%BD%D1%82%D0%B5%D0%B9%D0%BD%D0%B5%D1%80%D1%8B/
Java_Untitled Java__collections


import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;

public class Main {
public static void main(String[] args) {
List lnkLst = new LinkedList();
lnkLst.add("element1");
lnkLst.add("element2");
lnkLst.add("element3");
lnkLst.add("element4");
displayAll(lnkLst);
List aryLst = new ArrayList();
aryLst.add("x");
aryLst.add("y");
aryLst.add("z");
aryLst.add("w");
displayAll(aryLst);
Set hashSet = new HashSet();
hashSet.add("set1");
hashSet.add("set2");
hashSet.add("set3");
hashSet.add("set4");
displayAll(hashSet);
SortedSet treeSet = new TreeSet();
treeSet.add("1");
treeSet.add("2");
treeSet.add("3");
treeSet.add("4");
displayAll(treeSet);
LinkedHashSet lnkHashset = new LinkedHashSet();
lnkHashset.add("one");
lnkHashset.add("two");
lnkHashset.add("three");
lnkHashset.add("four");
displayAll(lnkHashset);
Map ma p1 = new HashMap();
map1.put("key1", "J");
map1.put("key2", "K");
map1.put("key3", "L");
map1.put("key4", "M");
displayAll(map1.keySet());
displayAll(map1.values());
SortedMap map2 = new TreeMap();
map2.put("key1", "JJ");
map2.put("key2", "KK");
map2.put("key3", "LL");
map2.put("key4", "MM");
displayAll(map2.keySet());
displayAll(map2.values());
LinkedHashMap map3 = new LinkedHashMap();
map3.put("key1", "JJJ");
map3.put("key2", "KKK");
map3.put("key3", "LLL");
map3.put("key4", "MMM");
displayAll(map3.keySet());
displayAll(map3.values());
}
static void displayAll(Collection col) {
Iterator itr = col.iterator();
while (itr.hasNext()) {
String str = (String) itr.next();
System.out.print(str + " ");
}
System.out.println();
}
}

Реклама

Добавить комментарий

Заполните поля или щелкните по значку, чтобы оставить свой комментарий:

Логотип WordPress.com

Для комментария используется ваша учётная запись WordPress.com. Выход / Изменить )

Фотография Twitter

Для комментария используется ваша учётная запись Twitter. Выход / Изменить )

Фотография Facebook

Для комментария используется ваша учётная запись Facebook. Выход / Изменить )

Google+ photo

Для комментария используется ваша учётная запись Google+. Выход / Изменить )

Connecting to %s