[JAVA] How to convert an array of Strings to an array of objects with the Stream API

Image of what you want to implement

Before conversion:

String[] strArray = {"Apple", "Mandarin orange", "banana"};

** After conversion: **

Fruit ringo = new Fruit("Apple");
Fruit mikan = new Fruit("Mandarin orange");
Fruit banana = new Fruit("banana");
Fruit[] fruitArray = {ringo, mikan, banana};

environment

OS:Windows 10 Java:Oracle JDK 11 (It is interpreted that it can be used free of charge for applications such as development and testing.)

(Reference) For ordinary for statements

String[] strArray = {"Apple", "Mandarin orange", "banana"};
Fruit[] fruitArray = new Fruit[strArray.length];
for(int i = 0; i < strArray.length; i++) {
    fruitArray[i] = new Fruit(strArray[i]);
}

When using Stream API

Fruit[] fruitArray = Arrays.stream(strArray)  // (1)
    .map(Fruit::new)                          // (2)
    .toArray(Fruit[]::new);                   // (3)

Lightly explain

(1) Convert from String array to String stream. (String [] ⇒ Stream \ <String >)

(2) Generate Fruit class for each Stream element with map method. Since there is only one String name argument in the constructor, this description is OK. (Of course, lambda expression s-> new Fruit (s) is also OK.)

(3) Convert to an array with the toArray method.

Summary

If you're not used to it, the for statement is easier to understand and write, but Once you get used to it, the code will be shorter and easier to write using the Stream API.

I'm still a beginner in Stream, so I want to force myself to get used to it quickly.

Recommended Posts

How to convert an array of Strings to an array of objects with the Stream API
Convert an array of strings to numbers
Convert 2D array to csv format with Java 8 Stream API
[Ruby] How to batch convert strings in an array to numbers
How to request by passing an array to the query with HTTP Client of Ruby
[Swift5] How to get an array and the complement of arrays
The story of toString () starting with passing an array to System.out.println
[Java] How to convert one element of a String type array to an Int type
Convert the array of errors.full_messages to characters and output
How to output standard from an array with forEach
How to use git with the power of jgit in an environment without git commands
How to add elements without specifying the length of the array
Now is the time to get started with the Stream API
Convert an array that may be null to a stream
[java8] To understand the Stream API
How to specify an array in the return value / argument of the method in the CORBA IDL file
Try to imitate the idea of a two-dimensional array with a one-dimensional array
How to output the value when there is an array in the array
Cast an array of Strings to a List of Integers in Java
How to get the length of an audio file in java
[Rails] How to convert the URI of the image sent by http to https when using Twitter API
[Java] How to search for a value in an array (or list) with the contains method
How to crop an image with libGDX
Compare the elements of an array (Java)
How to determine the number of parallels
How to sort the List of SelectItem
I tried to summarize the Stream API
How to decorate the radio button of rails6 form_with (helper) with CSS
[Swift] Summary of how to remove elements from an array (personal note)
[Java] How to turn a two-dimensional array with an extended for statement
How to access Socket directly with the TCP function of Spring Integration
Get the type of an array element to determine if it is an array
[swift5] How to change the color of TabBar or the color of item of TabBar with code
Java8 / 9 Beginners: Stream API addiction points and how to deal with them
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
How to find the cause of the Ruby error
[java] Summary of how to handle character strings
Customize how to divide the contents of Recyclerview
If the parameter is an array, how to include it in Stopara's params.permit
How to get the ID of a user authenticated with Firebase in Swift
How to use an array for HashMap keys
Convert a string to a character-by-character array with swift
[Swift] How to link the app with Firebase
How to get today's day of the week
Output of how to use the slice method
[Java] How to easily get the longest character string of ArrayList using stream
Calculate the similarity score of strings with JAVA
Rails6 I want to make an array of values with a check box
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
How to build API with GraphQL and Rails
How to display the result of form input
Getting Started with Doma-Introduction to the Criteria API
[Rails] How to build an environment with Docker
[Java] How to get the authority of the folder
[Java] How to operate List using Stream API
If you use SQLite with VSCode, use the extension (how to see the binary file of sqlite3)
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
I tried to check the operation of http request (Put) with Talented API Tester
[Docker] How to see the contents of Volumes. Start a container with root privileges.
How to set the retry limit of sidekiq and notify dead queues with slack