Appendix B. Miscellaneous library updates

 

This appendix reviews the main additions to the Java library.

B.1. Collections

The biggest update to the Collections API is the introduction of streams, which we discussed in chapters 46. There are also other updates, which we briefly review in this appendix.

B.1.1. Additional methods

The Java API designers made the most out of default methods and added several new methods to collection interfaces and classes. The new methods are listed in table B.1.

Table B.1. New methods added to collection classes and interfaces

Class/interface

New methods

Map getOrDefault, forEach, compute, computeIfAbsent, computeIfPresent, merge, putIfAbsent, remove(key, value), replace, replaceAll
Iterable forEach, spliterator
Iterator forEachRemaining
Collection removeIf, stream, parallelStream
List replaceAll, sort
BitSet stream
Map

The Map interface is the most updated interface, with support for several new convenient methods. For example, the method getOrDefault can be used to replace an existing idiom that checks whether a Map contains a mapping for a given key. If not, you can provide a default value to return instead. Previously you would do this:

Map<String, Integer> carInventory = new HashMap<>();
Integer count = 0;
if(map.containsKey("Aston Martin")){
  count = map.get("Aston Martin");
}

You can now more simply do the following:

Integer count = map.getOrDefault("Aston Martin", 0);

B.2. Concurrency

B.3. Arrays

B.4. Number and Math

B.5. Files

B.6. Reflection

B.7. String