Skip to main content
Second iteration. Explicit syntax highlighting hints (as a result, the diff looks more extensive than it really is - use view "Side-by-side Markdown" to compare).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = stringStream.toArray(String[]::new); 

It finds a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> stringStream = ...; String[] stringArray = stringStream.toArray(size -> new String[size]); 

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> stringStream = Stream.of("a", "b", "c"); String[] stringArray = stringStream.toArray(size -> new String[size]); Arrays.stream(stringArray).forEach(System.out::println); 

Prints:

a b c 
a b c 

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = stringStream.toArray(String[]::new); 

It finds a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> stringStream = ...; String[] stringArray = stringStream.toArray(size -> new String[size]); 

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> stringStream = Stream.of("a", "b", "c"); String[] stringArray = stringStream.toArray(size -> new String[size]); Arrays.stream(stringArray).forEach(System.out::println); 

Prints:

a b c 

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = stringStream.toArray(String[]::new); 

It finds a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> stringStream = ...; String[] stringArray = stringStream.toArray(size -> new String[size]); 

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> stringStream = Stream.of("a", "b", "c"); String[] stringArray = stringStream.toArray(size -> new String[size]); Arrays.stream(stringArray).forEach(System.out::println); 

Prints:

a b c 
Brevity.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = stringStream.toArray(String[]::new); 

What it does is findIt finds a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> stringStream = ...; String[] stringArray = stringStream.toArray(size -> new String[size]); 

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> stringStream = Stream.of("a", "b", "c"); String[] stringArray = stringStream.toArray(size -> new String[size]); Arrays.stream(stringArray).forEach(System.out::println); 

Prints:

a b c 

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = stringStream.toArray(String[]::new); 

What it does is find a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> stringStream = ...; String[] stringArray = stringStream.toArray(size -> new String[size]); 

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> stringStream = Stream.of("a", "b", "c"); String[] stringArray = stringStream.toArray(size -> new String[size]); Arrays.stream(stringArray).forEach(System.out::println); 

Prints:

a b c 

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = stringStream.toArray(String[]::new); 

It finds a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> stringStream = ...; String[] stringArray = stringStream.toArray(size -> new String[size]); 

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> stringStream = Stream.of("a", "b", "c"); String[] stringArray = stringStream.toArray(size -> new String[size]); Arrays.stream(stringArray).forEach(System.out::println); 

Prints:

a b c 
changed variable name to `stringStream` for consistency with `stringArray`
Source Link
Jens Bannmann
  • 5.1k
  • 7
  • 54
  • 81

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = streamStringstringStream.toArray(String[]::new); 

What it does, is find a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> streamstringStream = ...; String[] stringArray = streamstringStream.toArray(size -> new String[size]); 

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> streamStringstringStream = Stream.of("a", "b", "c"); String[] stringArray = streamStringstringStream.toArray(size -> new String[size]); Arrays.stream(stringArray).forEach(System.out::println); 

Prints:

a b c 

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = streamString.toArray(String[]::new); 

What it does, is find a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> stream = ...; String[] stringArray = stream.toArray(size -> new String[size]); 

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> streamString = Stream.of("a", "b", "c"); String[] stringArray = streamString.toArray(size -> new String[size]); Arrays.stream(stringArray).forEach(System.out::println); 

Prints:

a b c 

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = stringStream.toArray(String[]::new); 

What it does is find a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> stringStream = ...; String[] stringArray = stringStream.toArray(size -> new String[size]); 

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> stringStream = Stream.of("a", "b", "c"); String[] stringArray = stringStream.toArray(size -> new String[size]); Arrays.stream(stringArray).forEach(System.out::println); 

Prints:

a b c 
Show the recommended, easy method first, before the hand-constructed IntFunction.
Source Link
Andy Thomas
  • 86.8k
  • 12
  • 111
  • 160
Loading
Rollback to Revision 1
Source Link
skiwi
  • 69.8k
  • 32
  • 140
  • 225
Loading
added 21 characters in body
Source Link
user719662
user719662
Loading
Source Link
skiwi
  • 69.8k
  • 32
  • 140
  • 225
Loading