Java Login


Hello guys, is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.

     << Previous
Next >>     

Collection API Improvement

Java8 collection framework support lambda expression and streams. There is a performance improvement in HashMap because there was lots of collision in keys. To improve performance in hash bin in java 8 is using balanced trees instead of linked list for containing large number of colliding keys. This change in jdk8 applies only to HashMap, LinkedHashMap, and ConcurrentHashMap. Here are some new methods introduced in Collection -
Spliterator : A Spliterator is a special type of Iterator to traverse and partition the elements of a source in Java. A source can be a collection, an IO channel or a generator function. Some important points about Java Spliterator are:

  • Java Spliterator is an interface in Java Collection API which is introduced in Java 8 release in java.util package.
  • Spliterator supports both Sequential and Parallel processing of data.
  • We can use it for both Collection API and Stream API classes.
  • It provides characteristics about Collection or API objects.
  • It uses tryAdvance() method to iterate elements individually in multiple Threads to support Parallel Processing.
  • It uses forEachRemaining() method to iterate elements sequentially in a single Thread.
  • It uses trySplit() method to divide itself into Sub-Spliterators to support Parallel Processing.


package java8;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Spliterator;

public class CollectionApiImprovement {

public static void main(String[] args) {

//Sorting Map directly with Comparators
Map<String, String> map = new HashMap<>();
map.put("J", "j");
map.put("A", "a");
map.put("V", "v");
map.put("P", "p");

List<Map.Entry<String, String>> sortedByKey =

//Iterate over map easily with forEach
map.forEach((k, v) -> System.out.println("Key : " + k + " Value : " + v));

//operation on directly value
map.compute("P", (k, v) -> v.concat("-new"));

// if-else condition, use getOrDefault method
String val = map.getOrDefault("B", "NAN");
System.out.println(val); // prints Nah!

//override key using putIfAbsent
map.putIfAbsent("V", "x");

//Replace and Remove utilities
map.replaceAll((k, v) -> "v"); // values is "x" for all keys.
map.forEach((k, v) -> System.out.println("Key : " + k + " Value : " + v));

//Spliterator example
List<String> names = new ArrayList<>();

Spliterator<String> namesSpliterator = names.spliterator();



     << Previous
Next >>