https://dzone.com/articles/the-best-of-java-collections-tutorials
http://falkhausen.de/Java-10/java.util/Collection-Hierarchy.html hyerarchy
https://dzone.com/articles/a-deep-dive-into-collections
https://dzone.com/articles/the-developers-guide-to-collections-lists
https://dzone.com/articles/the-developers-guide-to-collections-sets
https://dzone.com/articles/the-developers-guide-to-collections-queues
Showing posts with label collections. Show all posts
Showing posts with label collections. Show all posts
Tuesday, December 31, 2019
Friday, December 27, 2019
LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class MyLinkedBlockingQueue {
public static void main(String[] args) throws InterruptedException {
LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue();
linkedBlockingQueue.put("PIPPO");
linkedBlockingQueue.put("PLUTO");
System.out.println(linkedBlockingQueue);
Object bla1 = linkedBlockingQueue.peek();
System.out.println(bla1);
Object bla2 = linkedBlockingQueue.take();
System.out.println(bla2);
Object bla3 = linkedBlockingQueue.take();
System.out.println(bla3);
Object bla4 = linkedBlockingQueue.take();
System.out.println(bla4);
System.out.println(linkedBlockingQueue);
}
}
the output is:
[PIPPO, PLUTO]
PIPPO // this is the peek
PIPPO // this is the first take
PLUTO // this is the second take
and then at bla4 it blocks, because the Queue is empty...waiting for someone to put something into it...
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/LinkedBlockingQueue.html
Labels:
collections
Sunday, December 15, 2019
Collections
https://dzone.com/articles/the-best-of-java-collections-tutorials
http://falkhausen.de/Java-10/java.util/Collection-Hierarchy.html hyerarchy
https://dzone.com/articles/a-deep-dive-into-collections
https://dzone.com/articles/the-developers-guide-to-collections-lists
https://dzone.com/articles/the-developers-guide-to-collections-sets
https://dzone.com/articles/the-developers-guide-to-collections-queues
http://falkhausen.de/Java-10/java.util/Collection-Hierarchy.html hyerarchy
https://dzone.com/articles/a-deep-dive-into-collections
https://dzone.com/articles/the-developers-guide-to-collections-lists
https://dzone.com/articles/the-developers-guide-to-collections-sets
https://dzone.com/articles/the-developers-guide-to-collections-queues
Labels:
collections
Saturday, July 14, 2012
ConcurrentHashMap and MultiValueMap - a Map with multiple values per key
a ConcurrentHashMap can only associate 1 value to 1 key:
map.put("a", "bla");
map.put("a", "blu");
map.get("a") will return only "blu".
If I want map.get("a") to return ("bla", "blu") I must use a MultiValueMap to decorate a ConcurrentHashMap
or you can use a Google Collections (Guava) Multimap such as a ArrayListMultimap
Otherwise, if like me you like brewing your own framework, you can decorate yourself a ConcurrentHashmap:
map.put("a", "bla");
map.put("a", "blu");
map.get("a") will return only "blu".
If I want map.get("a") to return ("bla", "blu") I must use a MultiValueMap to decorate a ConcurrentHashMap
or you can use a Google Collections (Guava) Multimap such as a ArrayListMultimap
Otherwise, if like me you like brewing your own framework, you can decorate yourself a ConcurrentHashmap:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.xmlbeans.XmlObject;
public class ArrayListMultimap {
private ConcurrentHashMap<String, List> theMap = new ConcurrentHashMap<String, List<XmlObject>>();
public static ArrayListMultimap create(int i, int j) {
return new ArrayListMultimap();
}
public List<XmlObject> put(String key, XmlObject value) {
List<XmlObject> previousvalue = (List<XmlObject>)theMap.get(key);
if (previousvalue == null) {
previousvalue = new ArrayList<XmlObject>();
previousvalue.add(value);
theMap.put(key, previousvalue);
}
return previousvalue;
}
public int size() {
return theMap.size();
}
}
Labels:
collections
Monday, July 18, 2011
synchronizedList
I have created an attribute
private List measurements = new ArrayList();
and I keep getting an exception:
java.lang.ArrayIndexOutOfBoundsException: 10
at java.util.ArrayList.add(ArrayList.java:352)
whenever I do
public void addEntry(long elapsed) {
measurements.add(elapsed);
}
In fact, ArrayList constructor does:
public ArrayList() {
this(10);
}
and its "add" method verifies that there is enough capacity:
public boolean add(E e) {
ensureCapacity(size + 1);
elementData[size++] = e;
return true;
}
it is self evident that this code is not thread safe.... so if you plan to use an ArrayList in a concurrent scenario, you better synchronize here and there...
otherwise use http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#synchronizedList%28java.util.List%29
List list = Collections.synchronizedList(new ArrayList());
private List
and I keep getting an exception:
java.lang.ArrayIndexOutOfBoundsException: 10
at java.util.ArrayList.add(ArrayList.java:352)
whenever I do
public void addEntry(long elapsed) {
measurements.add(elapsed);
}
In fact, ArrayList constructor does:
public ArrayList() {
this(10);
}
and its "add" method verifies that there is enough capacity:
public boolean add(E e) {
ensureCapacity(size + 1);
elementData[size++] = e;
return true;
}
it is self evident that this code is not thread safe.... so if you plan to use an ArrayList in a concurrent scenario, you better synchronize here and there...
otherwise use http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#synchronizedList%28java.util.List%29
List list = Collections.synchronizedList(new ArrayList());
Labels:
collections
Thursday, January 13, 2011
java.util.ConcurrentModificationException
I get this while iterating on a collection (a regular java.lang.Vector) and at the same time changing it:
for (PreactivationCallbackTask task : tasks) {
....
tasks.remove(task); // changing the collection while you iterate is not allowed
}
I get this:
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
here the javadoc for java.util.ConcurrentModificationException
The only way out is to change the collection OUTSIDE the for loop.
This post provides an excellent explanation.
for (PreactivationCallbackTask task : tasks) {
....
tasks.remove(task); // changing the collection while you iterate is not allowed
}
I get this:
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
here the javadoc for java.util.ConcurrentModificationException
The only way out is to change the collection OUTSIDE the for loop.
This post provides an excellent explanation.
Labels:
collections
Monday, October 4, 2010
Java Collections, some important facts
Here the main entry point, with an excellent tutorial by Joshua Bloch.
Collections can be: Set (TreeSet, HashSet), List (Vector, LinkedList), Queue (LinkedList), Map (HashMap, TreeMap, LinkedHashMap)
Some implementations are Synchronized (Vector), other no (ArrayList)
Collections provide Enumeration or Iterator to scan them.
Use Iterator when you might want to make changes to the collection, use Enumeration when R/O. You have also ListIterator
java.util.concurrent package provide thread-safe optimized implementation of most collections.
ArrayList and Vector use a dynamic array as an implementation,
LinkedList uses a doubly linked list, and it's faster for insert/delete
Queue: FIFO
Stack: LIFO
Interview questions here:
http://www.interview-questions-java.com/java-collections.htm
Collections can be: Set (TreeSet, HashSet), List (Vector, LinkedList), Queue (LinkedList), Map (HashMap, TreeMap, LinkedHashMap)
Some implementations are Synchronized (Vector), other no (ArrayList)
Collections provide Enumeration or Iterator to scan them.
Use Iterator when you might want to make changes to the collection, use Enumeration when R/O. You have also ListIterator
java.util.concurrent package provide thread-safe optimized implementation of most collections.
ArrayList and Vector use a dynamic array as an implementation,
LinkedList uses a doubly linked list, and it's faster for insert/delete
Queue: FIFO
Stack: LIFO
Interview questions here:
http://www.interview-questions-java.com/java-collections.htm
Labels:
collections
Subscribe to:
Posts (Atom)