Java8 string[] arrays, integer[], and other types of wrapper arrays into a primitive array such as int[]

Source: Internet
Author: User

To convert a wrapper type array string[], integer[], etc. to a basic type array such as int[], is to use the Java8 stream for a for loop.

public class Tostreamintstring{public static void Main (string[] args) {Scanner in  = new Scanner (system.in); list<string> list = new arraylist<> (); for (int i = 0; i < 3; i++) {List.add (In.nextline ());} String n = list.get (0); System.out.println ("\ n---> string\n" +n); string[] xs = list.get (1). Split ("\\s+"); string[] ys = List.get (2). Split ("\\s+"); System.out.println ("\ n---> string[] to int[]");
You need to enter a pure digital int[] x = Arrays.stream (xs). Maptoint (integer::valueof). ToArray (); Arrays.stream (x). ForEach ((i)-System.out.print (i+ "")); System.out.println ("\ n---> string[] to list, list to Stream"); List<string> y = arrays.aslist (ys), Y.stream (). ForEach ((i) System.out.print (i+ "")); In.close ();}}
Java8 stream can be obtained in many ways, in which the stream () method can be used directly to the collection class, and the source code can be seen to know that the collection interface has a new default method Stream<e> stream (),
The Java Collection class is able to use the stream () method to get the stream.
Public interface Collection<e> extends iterable<e> {    /**     * @since 1.8 */    default Stream <E> Stream () {        return Streamsupport.stream (Spliterator (), false);}    }

 



Java8 string[] arrays, integer[], and other types of wrapper arrays into a primitive array such as int[]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.