Java 8 - New methods in Collections
Below is short simple sample code to show use of some of the new methods introduced in collections including default method in Java8 -
package javaFeatures;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CollectionsJava8 implements Defaulting {
public static void main(String[] args) {
CollectionsJava8 cl = new CollectionsJava8();
List<String> al = new ArrayList<>();
al.add("Red");
al.add("Blue");
al.add("White");
al.add("Black");
// Below is the way using method reference
// al.forEach(CollectionsJava8::show);
// Below is the way using lambda expression
// al.forEach(str -> CollectionsJava8.show(str));
// Below is the way using method reference
al.forEach(cl::inserts);
// Below is the way using lambda expression
al.forEach(str -> cl.inserts(str));
System.out.println(Defaulting.colors.size());
// Below way we get the ConcurrentModificationException because in this we are not removing the
// required element using iterator during iteration
// Defaulting.colors.removeIf(cl::removals);
// Below is the right way to remove the element while iterating
Defaulting.colors.removeIf(str -> str.length() > 4);
System.out.println("After removal : " + Defaulting.colors.size());
// Below we are removing the element & also taking some other action like printing the message.
// Note we have to return some boolean value in removeIf as it expects some boolean value.
Defaulting.colors.removeIf(str -> {if(str.length() > 2){ System.out.println("Orange"); return true; } return false;});
System.out.println("After removal : " + Defaulting.colors.size());
// Below is the way to replace all the values matching to the required condition
String repColor = "Orange";
al.replaceAll(str -> {if(str == "Red") return repColor; else return str; });
System.out.println(al.get(1));
// Below is the way to sort the elements of the list.
al.sort((a, b) -> a.length()>b.length() ? 1 : -1);
System.out.println(al.get(2));
Map<String, Integer> persons = new HashMap<>();
persons.put("Sid", 25);
persons.put("Deep", null);
persons.compute("Sid", (a, b) -> 26);
System.out.println("After Update " + persons.get("Sid"));
System.out.println("Size of Collection after compute on Sid : " + persons.size());
persons.compute("Deep", (a, b) -> null);
System.out.println("Size of Collection after compute on Deep : " + persons.size());
// If the value provide below is null then given entry will be ignored.
persons.computeIfAbsent("Deep", b -> 25);
System.out.println("Size of Collection after computeIfAbsent on Deep : " + persons.size());
// Returns the value mapped to the key, or if the key is not present, returns the default value.
// Here value will be 30
String person = "Sudeep";
System.out.println(persons.getOrDefault(person, 30));
/*If the key is not present or if the value for the key is null,
then adds the key-value pair to the map. If the key is present
then replaces the value with the value from the remapping function.
If the remapping function return null then the key is removed from the map.*/
// a is existing value against the given key & b is the value passed below.
persons.merge(person, 30, (a, b) -> a+b);
System.out.println(persons.get(person));
persons.merge(person, 31, (a, b) -> a+1);
System.out.println(persons.get(person));
}
static void show(String str) {
switch(str) {
case "Red" : System.out.println(str + " is good color on some ocassions.");
break;
case "Blue" : System.out.println(str + " is color of sky due to atmosphere.");
break;
case "White" : System.out.println(str + " is color of peace.");
break;
case "Black" : System.out.println(str + " is good color for bikes, cars & mobiles.");
break;
default : System.out.println();
}
}
}
// Below is the usage of the default method introduced in Java8
// It can't be static like methods are in interface because those are abstract in nature
// & default methods are not linked to inteface rather those are linked to instances &
// therefore can be overriden.
interface Defaulting {
Set<String> colors = new HashSet<>();
default public void inserts(String str) {
colors.add(str);
}
default boolean removals(String str) {
if(str.length() > 4)
return colors.remove(str);
return false;
}
}
package javaFeatures;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CollectionsJava8 implements Defaulting {
public static void main(String[] args) {
CollectionsJava8 cl = new CollectionsJava8();
List<String> al = new ArrayList<>();
al.add("Red");
al.add("Blue");
al.add("White");
al.add("Black");
// Below is the way using method reference
// al.forEach(CollectionsJava8::show);
// Below is the way using lambda expression
// al.forEach(str -> CollectionsJava8.show(str));
// Below is the way using method reference
al.forEach(cl::inserts);
// Below is the way using lambda expression
al.forEach(str -> cl.inserts(str));
System.out.println(Defaulting.colors.size());
// Below way we get the ConcurrentModificationException because in this we are not removing the
// required element using iterator during iteration
// Defaulting.colors.removeIf(cl::removals);
// Below is the right way to remove the element while iterating
Defaulting.colors.removeIf(str -> str.length() > 4);
System.out.println("After removal : " + Defaulting.colors.size());
// Below we are removing the element & also taking some other action like printing the message.
// Note we have to return some boolean value in removeIf as it expects some boolean value.
Defaulting.colors.removeIf(str -> {if(str.length() > 2){ System.out.println("Orange"); return true; } return false;});
System.out.println("After removal : " + Defaulting.colors.size());
// Below is the way to replace all the values matching to the required condition
String repColor = "Orange";
al.replaceAll(str -> {if(str == "Red") return repColor; else return str; });
System.out.println(al.get(1));
// Below is the way to sort the elements of the list.
al.sort((a, b) -> a.length()>b.length() ? 1 : -1);
System.out.println(al.get(2));
Map<String, Integer> persons = new HashMap<>();
persons.put("Sid", 25);
persons.put("Deep", null);
persons.compute("Sid", (a, b) -> 26);
System.out.println("After Update " + persons.get("Sid"));
System.out.println("Size of Collection after compute on Sid : " + persons.size());
persons.compute("Deep", (a, b) -> null);
System.out.println("Size of Collection after compute on Deep : " + persons.size());
// If the value provide below is null then given entry will be ignored.
persons.computeIfAbsent("Deep", b -> 25);
System.out.println("Size of Collection after computeIfAbsent on Deep : " + persons.size());
// Returns the value mapped to the key, or if the key is not present, returns the default value.
// Here value will be 30
String person = "Sudeep";
System.out.println(persons.getOrDefault(person, 30));
/*If the key is not present or if the value for the key is null,
then adds the key-value pair to the map. If the key is present
then replaces the value with the value from the remapping function.
If the remapping function return null then the key is removed from the map.*/
// a is existing value against the given key & b is the value passed below.
persons.merge(person, 30, (a, b) -> a+b);
System.out.println(persons.get(person));
persons.merge(person, 31, (a, b) -> a+1);
System.out.println(persons.get(person));
}
static void show(String str) {
switch(str) {
case "Red" : System.out.println(str + " is good color on some ocassions.");
break;
case "Blue" : System.out.println(str + " is color of sky due to atmosphere.");
break;
case "White" : System.out.println(str + " is color of peace.");
break;
case "Black" : System.out.println(str + " is good color for bikes, cars & mobiles.");
break;
default : System.out.println();
}
}
}
// Below is the usage of the default method introduced in Java8
// It can't be static like methods are in interface because those are abstract in nature
// & default methods are not linked to inteface rather those are linked to instances &
// therefore can be overriden.
interface Defaulting {
Set<String> colors = new HashSet<>();
default public void inserts(String str) {
colors.add(str);
}
default boolean removals(String str) {
if(str.length() > 4)
return colors.remove(str);
return false;
}
}