Java - Streams: Difference between revisions

From My Limbic Wiki
Line 89: Line 89:


===LinkedBlockingQueue===
===LinkedBlockingQueue===
This is a blocking queue that uses a linked list to maintain its elements, which are returned in FIFO order
===ConcurrentLinkedQueue===
===ConcurrentLinkedQueue===
===SynchronousQueue===
===SynchronousQueue===

Revision as of 18:06, 18 October 2019

Collections

Main Methods

<source lang="Java"> - add(Object o) //Adds the specified object to the collection. - remove(Object o) //Removes the specified object from the collection. - clear() //Removes all elements from the collection. - size() //Returns an integer that indicates how many elements are currently in the collection. - iterator() //Returns an object that can be used to retrieve references to the elements in the collection. </source>

Samples

Print all Students name

<source lang="Java"> Student student; Iterator iterator = collection.iterator(); while (iterator.hasNext()) {

   student = (Student)(iterator.next());
   System.out.println(student.getFullName());

} </source>

Get a Student

<source lang="Java"> Student student = getNextStudent(); while (student != null) {

   collection.add(student);

} </source>

Iteration Without Generics Often Requires Casting

<source lang="Java"> Student student; Iterator iterator = collection.iterator(); while (iterator.hasNext()) {

   student = (Student)(iterator.next());
   System.out.println(student.getFullName());

} </source>

Autoboxing

is the process of performing the encapsulation before a primitive is stored in a collection, and the following is an example of how this can improve your code: <source lang="Java"> Random random = new Random(); Collection<Integer> collection = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) {

   collection.add(random.nextInt());

} </source>

Unboxing

is the process of extracting the primitive value from its corresponding wrapper object when retrieving data from a collection: <source lang="Java"> int total = 0; Iterator<Integer> iterator = collection.iterator(); while (iterator.hasNext()) {

   total += iterator.next();

} </source>

Remove by Index

A Naive Approach to Removing Elements Corresponding to a Set of Index Values <source lang="java"> //as soon as you remove the first entry, the other indices in the array effectively become invalid because they no longer refer to the same elements for (int i = 0; i < deleteIndices.length; i++) {

  myList.remove(deleteIndices[i]);

}

//An easy way to address this problem is to simply traverse the index list in reverse order for (int i = deleteIndices.length - 1; i >= 0; i--) {

   myList.remove(deletedIndices[i]);

} </source>

Queue

Main Methods

<source lang="Java"> - element() Returns a reference to the head element without removing it, throwing an exception if the queue is empty. - peek() //Returns a reference to the head element without removing it, returning null if the queue is empty. - offer() //Adds an element to the queue. Some implementations may reject the addition, in which case a value of false is returned. - remove() //Retrieves a reference to the head element and removes it from the queue, throwing an exception if the queue is empty. - poll() //Retrieves a reference to the head element and removes it from the queue, returning null if the queue is empty. </source>

PriorityQueue

Like a TreeSet or a TreeMap in that its elements are ordered based upon their “priority,” which is really just either their natural order or their sequence as determined by an instance of Comparator

PriorityBlockingQueue

isn’t a subclass of PriorityQueue one important difference: as the name implies, this class represents a blocking queue. A blocking queue is one that causes the calling thread to be blocked if the queue is empty, and the thread will remain blocked until at least one element is added to the queue.

ArrayBlockingQueue

This class represents a blocking queue that uses an array to maintain its elements, and those elements are returned in FIFO manne You might use this class when threads are creating tasks that need to be processed and all the tasks are considered to be of equal priority.

LinkedBlockingQueue

This is a blocking queue that uses a linked list to maintain its elements, which are returned in FIFO order

ConcurrentLinkedQueue

SynchronousQueue

DelayQueue

A voir

  • Parallel stream