Java Login

About javalogin.com

Hello guys,
javalogin.com 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.

Example


CollectionApiImprovement.java

package java8;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
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 =
map.entrySet().stream().sorted(Map.Entry.comparingByKey())
.collect(Collectors.toList());
sortedByKey.forEach(System.out::println);

//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"));
System.out.println(map.get("P"));

// 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");
System.out.println(map.get("V"));

//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<>();
names.add("Smith");
names.add("Andrew");
names.add("John");

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

namesSpliterator.forEachRemaining(System.out::println);
}

}




     << Previous
Next >>