Posts

Showing posts with the label java 10

Java List copyOf method

Java List copyOf method Example copyOf method added in jdk10. Returns an unmodifiable List containing the elements of the given Collection, in its iteration order. The given Collection must not be null, and it must not contain any null elements. If the given Collection is subsequently modified, the returned List will not reflect such modifications. Implementation Note: If the given Collection is an unmodifiable List, calling copyOf will generally not create a copy. Method: static <E> List<E> copyOf​(Collection<? extends E> coll) Implementation Note: If the given Collection is an unmodifiable List, calling copyOf will generally not create a copy. Type Parameters: E - the List's element type Parameters: coll - a Collection from which elements are drawn, must be non-null Returns: a List containing the elements of the given Collection Throws: NullPointerException - if coll is null, or if it contains any nulls Example: class copyOfExample{ p...

Java Data Type Default Values

Image
When a field is declared, It's not always necessary to assign a value. Fields that are declared but not initialized will be set to a reasonable default by the compiler. This default will be zero or null, depending on the data type. The following are the default values for the above data types: Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

Java - APIs for Creating Unmodifiable Collections

Several new APIs have been added that facilitate the creation of unmodifiable collections. The List.copyOf Set.copyOf Map.copyOf  methods create new collection instances from existing instances. New methods have been added to the Collectors class in the Stream package. toUnmodifiableList toUnmodifiableSet toUnmodifiableMap   These allow the elements of a Stream to be collected into an unmodifiable collection.

Lambda expressions - implicitly typed parameters

For formal parameters of implicitly typed lambda expressions, allow the reserved type name `var` to be used, so that:     (var x, var y) -> x.process(y) is equivalent to:     (x, y) -> x.process(y) An implicitly typed lambda expression must use `var` for all its formal parameters or for none of them. In addition, `var` is permitted only for the formal parameters of implicitly typed lambda expressions --- explicitly typed lambda expressions continue to specify manifest types for _all_ their formal parameters, so it is not permitted for some formal parameters to have manifest types while others use `var`. The following examples are illegal:     (var x, y) -> x.process(y)         // Cannot mix 'var' and 'no var' in implicitly typed lambda expression     (var x, int y) -> x.process(y)     // Cannot mix 'var' and manifest types in explicitly typed lambda expression In theory, it would be...