Java streams 23. ToArray

Object[] res = Stream.of("a", "b", "c")
.map(String::toUpperCase)
.toArray();
System.out.print(Arrays.toString(res)); //prints: [A, B, C]
IntFunction<String[]> f = i -> new String[i];
System.out.print(Arrays.toString(f.apply(3)));
//prints: [null, null, null]
IntFunction<String[]> f = i -> new String[i];
String[] res = Stream.of("a", "b", "c")
.map(String::toUpperCase)
.toArray(f);
System.out.print(Arrays.toString(res)); //prints: [A, B, C]
String[] res = Stream.of("a", "b", "c")
.map(String::toUpperCase)
.toArray(i -> new String[i]);
System.out.print(Arrays.toString(res)); //prints: [A, B, C]
String[] res = Stream.of("a", "b", "c")
.map(String::toUpperCase)
.toArray(String[]::new);
System.out.print(Arrays.toString(res)); //prints: [A, B, C]
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() { return name; } @Override
public String toString() {
return "Person{" + name + "}";
}
}
Person createPerson(String s){
switch (s){
case "a":
return new Person("Alex");
case "b":
return new Person("Bernie");
case "c":
return new Person("Carol");
default:
return new Person("Zoi");
}
}
Person[] res = Stream.of("a", "b", "c")
.map(s -> createPerson(s))
.toArray(Person[]::new);
System.out.print(Arrays.toString(res));
//prints: [Person{Alex}, Person{Bernie}, Person{Carol}]
Integer[] res = Stream.of("a", "b", "c")
.map(s -> createPerson(s))
.map(p -> p.getName().length())
.toArray(Integer[]::new);
System.out.print(Arrays.toString(res)); //prints: [4, 6, 5]
Integer[] res = Stream.of("a", "b", "c")
.map(s -> createPerson(s).getName().length())
.toArray(Integer[]::new);
System.out.print(Arrays.toString(res)); //prints: [4, 6, 5]
int[] res = Stream.of("a", "b", "c")
.map(s -> createPerson(s).getName().length())
.mapToInt(i -> i)
.toArray();
System.out.print(Arrays.toString(res)); //prints: [4, 6, 5]
int[] res = Stream.of("a", "b", "c")
.mapToInt(s -> createPerson(s).getName().length())
.toArray();
System.out.print(Arrays.toString(res)); //prints: [4, 6, 5]

--

--

--

Born in Moscow, lived in Crimea, now lives in the US. Used to be physicist and rock climber, now programmer and writer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Upgrade heroku database

Introduce: CentralBoard.app

Build an Alexa Skill in 7 Minutes Flat with Node.js and StdLib

Adobe Acrobat Reader Dc Mac Download

Acrobat

Top 5 Reasons to Learn Flutter in 2021

Everything You Always Wanted to Know About IIIF* (*But Were Afraid to Ask)

PM Update: Clear and cold tonight. Pleasant weather again for Sunday.

Getting Started With Web Development — How To Proceed

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nick Samoylov

Nick Samoylov

Born in Moscow, lived in Crimea, now lives in the US. Used to be physicist and rock climber, now programmer and writer.

More from Medium

How to Build Java Applications Today #62

Java Memory Management

OOPs in Java

Java Packages & API-10 example