Java 8 - Part VII [Collections]
Let's start the seventh post of the Java 8 series about the changes that you can find from version 6 to 8, the Collections.
You will see some subject already mentioned in the stream post, but I consider important to have a separate post to this subject.
Summary
Introduction
A collection is a group of objects contained in a single object.
The main interfaces in the Collections Framework are:
- List: an ordered collection of elements; it allows duplicate entries. It does not limit the null elements
- ArrayList: dynamic arrays. It's not synchronized, allow null values and maintenance the insertion order. It can not be used by primitives.
- LinkedList: the elements are not stored side-by-side. They are linked by pointers. Each element specifies previews and next elements.
- Set: it's not ordered collection and it does not allow duplicate entries. It allows only one null element.
- The HashSet works like HashMap but not allow duplicate elements.
- Queue: ordered for a process (FIFO - first-in, first-out)
- Map: it maps keys to values; it does not allow duplicate key.
- HashMap implements Map interface. You can see the internal working in this post on GeeksforGeeks. You will see that Java 8 change the implementation to solve collision. Now Java 8 began using linked-list and as soon as the threshold is reached the hash will change to use a balanced tree.
- LinkedHashMap: similar to the HashMap but now you can access the elements by their insertion order.
- HashTable also implements Map interface. The main differences between the HashTable and the HashMap are that the first one does not allow to use null value to the key or to the value and it is synchronized, which is an important reason to make the performance worst comparing with HashMap. The ConcurrentHashMap (from Executor framework) is similar to HashTable, but the ConcurrentHashMap has a better performance. ConcurrentHashMap read data using get() without locks (the locking is applied only for updates), by another hand, the HashTable is synchronized for all operation (single lock for whole data). More detail about the differences you can see on this other GeeksforGeeks post. And an implementation you can see here.
- TreeMap implements a child of Map interface, the SortedMap. It's useful to use with unique elements and where is necessary to have sorted elements (natural order or using Comparable interface).
- HashMap implements Map interface. You can see the internal working in this post on GeeksforGeeks. You will see that Java 8 change the implementation to solve collision. Now Java 8 began using linked-list and as soon as the threshold is reached the hash will change to use a balanced tree.
HowToDoInJava - Java Collection
Another post also addresses the topic Java collection. If you want a little more about this go to the Marcus Biel Java Collection post in DZone.
Using the Diamond Operator
From Java 7, you can omit the type of the generic class from the right side, but the diamond operator ('<>') is required yet.
The compiler will fill the type with the type declared on the right side. The diamond is limited to be used only on right side.
Iterates, filters and sorts (using lambda)
To iterate through the collection you can do this using forEach loop, the index, while loop (see this HowToDoInJava post to examples). But you don't need to navigate each element in every case. If you want to search some elements it's possible to use the binarySearch method to do this. But the list needs to be sorted.
To sort a collection you need to know how to compare the elements. So, you can use the Comparator interface with inner class or using lambda expression. It's possible to say that Comparator is a functional interface because it has a single abstract method. If it's necessary you can use a not natural order with reverseOrderMethod.
Also you can use filters in the collections. The Streams accept the Predicate interface to apply in the collection. Example you can see here, compare before and after Java 8.
Java SE 8 collection improvements
Java 8 introduced new methods to help us to manipulate the collections. One of them was the removeIf method to remove an element using a condition. Another one was the replaceAll, where you pass a lambda expression and to apply to each element. You can use the forEach loop to navigate by the list and do something.
The Map also added new methods such merge that add a new element with a logic.
The computeIfPresent is another method to map and runs only when the key isn’t present or is null. And computeIfAbsent do the opposite.
Streams
The stream in java is a sequence of data which you can execute operation and get a result. It's not a collection. The Stream API was introduced to process elements in sequence. The stream wraps an existing collection to support operations expressed with lambdas, so you specify what you want to do, not how to do it.
Some differences of the stream from the collection are:
- No storage. A stream gets each element from a source (data structure, an array, a generator function) and processes through a pipeline of computational operations.
- Functional in nature. An operation on a stream produces a result but does not modify its source.
- Laziness-seeking. Many stream operations can be implemented lazily.
- Possibly unbounded. It's possible to work with infinite size using streams throughout short-circuiting operations such as limit(n) or findFirst().
- Consumable. The elements of a stream are only visited once during the life of a stream.
The Stream pipeline is the set of operations chained, which can be the intermediate operations and terminal operations.
- Intermediate Stream: execute operation where is possible to manipulate the stream and the result is another stream.
- Terminal operator: execute the operator to get a result different of the stream.
Intermediate Stream Operation
- flatMap: "It takes each element in the stream and makes any elements it contains top-level elements in a single stream". It is usual to remove empty elements of when you are using a list with a list.
- map: "It creates a one-to-one mapping from the elements in the stream to the elements of the next step in the stream"
Terminal Stream Operation
Search for data:
- findAny/findFirst: It return a Optional< T > object. To an empty stream, the return will be an empty Optional return. It works with an infinite stream. These methods do not need to process all the elements. The findAny method is useful to work with a parallel stream.
- allMatch/anyMatch/noneMatch : the return is a boolean type related to the predicate passed. The allMatch and the noneMatch methods cannot be working in an infinite stream.
Reduction operator
- collect: it does not terminate an infinite execution. It is a reduction operator.
- min/max: It allows find smallest or largest value. It returns an Optional< T > object to be able to represent no value. In an infinite stream, it cannot terminate the process.
- count: it returns a long number the represents the number of elements in a stream. It does not terminate execution in case of the infinite stream.
Group the results
- averagingDouble/averagingInt/averagingLong: return the average to the collect
- joining: it creates a single string.
- groupingBy: it will create a map using a Function.
- partitioningBy: it will create a map using a Predicate.
Optional With Primitive Streams
- average: it returns a optional type to primitive (ex. OptionalDouble)
-
IntStream.rangeClosed(1,10).average();
-
- sum: It does not return optional type
- min:/max:
-
IntStream.rangeClosed(1,10).min();
-
Related Posts
- Java 8 - Part VII [Collections]
- Java 8 – Part VI [File IO NIO.2]
- Java 8 – Part V [Concurreny]
- Java 8 – Part IV [Streams]
- Java 8 – Parte III [Lambda]
- Java 8 - Part II [Localization, Date, Time]
- Java 8 - Language Enhancements
- JVM