collection enhancement in jdk8...

26
Collections Enhancements By Anurag

Upload: others

Post on 06-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Collections Enhancements

By Anurag

Page 2: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Agenda• Java 8 Collections Enhancements • Java 9 Collections Enhancements

Page 3: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Java 8 Collections Enhancements

• Lambda/Streams • Collections are most common stream source and destination

• Interfaces: Default Methods, Static Methods • Java 8 language feature • allows interfaces to be extended compatibly

• Collection interface enhancements • first changes in > 15 years! • Iterable, Collection, List got a few new methods • Map, Comparator got a lot of new methods

Page 4: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Iterable Interface

• Iterable.forEach // Java 7List<Stringti list = ... ;for (String str : list)

System.out.println(str);

// Java 8list.forEach(System.out::println);

• PS: Collection is a subinterface of Iterable

Page 5: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Collection Interface

• Collection.removeIf - bulk mutating operation // Java 7for (Iterator<String> it = coll.iterator() ; it.hasNext() ; ) {

String str = it.next();if (str.startsWith("A"))

it.remove();}

// Java 8coll.removeIf(str -> str.startsWith("A"));

• For Arraylist: O(n2) vs O(n)

Page 6: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

List Interface

• List.replaceAll - bulk mutation operation • Transforms each element in-place

// Java 7for (int i = 0; i < list.size(); i++) list.set(i, list.get(i).toUpperCase());

// Java 8list.replaceAll(String::toUpperCase);

• *Can not change Data Type

Page 7: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

List Interface

• List.sort - sorts a list in-place • Why is this better than Collections.sort? • old Collections.sort used three step process:

• copy into an temporary array • sort the array • copy back to the list

• List.sort • default does exactly the above • ArrayList.sort overrides and sorts in-place - no copying!

Page 8: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Map Interface: forEach

// Java 7

for (Map.Entry<String,String> entry : map.entrySet())

System.out.println(entry.getKey() + entry.getValue());

// Java 8

map.forEach((k, v) -> System.out.println(k + v));

Page 9: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Map Interface: replaceAll

// Java 7for (Map.Entry<String,String> entry : map.entrySet())

entry.setValue(entry.getValue().toUpperCase());

// Java 8map.replaceAll((k, v) -> v.toUpperCase());

Page 10: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Map Interface: “Multi-map” Example

• Multimap: like a map, with multiple values for each key • Example: simplified Multimap using Map<String, Set<Integer>>

Map<String, Set<Integer>> multimap = new HashMap<>();

Page 11: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Map Interface: “Multi-map” Example - Java 7

// put(str, i)

Set<Integer> set = multimap.get(str); if (set == null) {

set = new HashSet<>(); multimap.put(str, set);

}

set.add(i);

Page 12: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Map Interface: “Multi-map” Example - Java 7

// remove(str, i)

Set<Integer> set = multimap.get(str); if (set != null) {

if (set.remove(i) && set.isEmpty()) { multimap.remove(str);

}

}

Page 13: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Map Interface: “Multi-map” Example - Java 8

// put(str, i)

multimap.computeIfAbsent(str, x -> new HashSet<>()).add(i);

// remove(str, i)

multimap.computeIfPresent(str, (k, set) -> set.remove(i) && set.isEmpty() ? null : set);

// contains(str, i)

multimap.getOrDefault(str, Collections.emptySet()).contains(i);

// size()

multimap.values().stream().mapToInt(Set::size).sum();

// values()

multimap.values().stream().flatMap(Set::stream);

Page 14: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Comparator Example - Java 7

Page 15: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Comparator Example - Java 8 Comparator Methods

• *Static import of comparator

Page 16: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Java 9 Collections Enhancements

Page 17: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Convenience Factory Methods for Collections• Summary: Make it convenient to create instances of collections

and maps with small numbers of elements. • Main goals • convenience and brevity • space efficiency •  immutability

Page 18: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Example

Page 19: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

API Overview

Page 20: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Design and Implementation Issues

• Immutability • Iteration Order • Nulls Disallowed • Duplicate Handling • Space Efficiency

Page 21: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Immutability

• Collections returned by the new static factory methods are immutable • “Conventional” immutability, not “immutable persistent” • add, set, or remove throw UnsupportedOperationException

Page 22: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Randomized Iteration Order

• make iteration order predictably unpredictable for map and set.

Page 23: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Nulls Disallowed

• Nulls disallowed as List or Set members, Map keys or values

Page 24: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Throw Exceptions on Duplicates

Page 25: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Space Efficiency

• Consider an unmodifiable set containing two strings Set<String> set = new HashSet<>(3); // 3 is the number of buckets set.add("foo");set.add("bar");set = Collections.unmodifiableSet(set);

• How much space does this take? Count objects. • 1 unmodifiable wrapper • 1 HashSet • 1 HashMap • 1 Object[] table of length 3 • 2 Node objects, one for each element

Page 26: Collection Enhancement In JDK8 JDK9files.meetup.com/3189882/Collection_Enhancement_In_JDK8_JDK9.pdf · Java 8 Collections Enhancements • Lambda/Streams • Collections are most

Thank You