You can do this with a partition operation. First transform the array to a stream, and then use the Collectors.partitioningBy() method to get a Map<Boolean, List<String>>. You could then transform both lists to an array, although usually collections are just fine.
Use a Stream and its filter method. I do not unfortunately know how to divide a Stream in two depending on the results of filtering. Won't work in Java7.The first line uses the stream() method of the Arrays class to create a sequential Stream.
The second line turns it into a parallel Stream. There is an overhead to parallelisation, so you should delete that line for small collections. I do not know how large a collection would have to be to benefit from parallel processing, but maybe over 1000 elements.
The 3rd line uses the fact that you can create a Scanner object to scan a String and then use its methods. That will return true if you pass a String like "12345.6789 rammie singh". You can do all sorts of other things. In that expression, you are calling the next String s. The compiler “knows” it is dealing with a Stream<String> and can use it in the predicate to the right of the ->
An alternative would be
...(s ->s.matches(myRegex))... Depends on what you are going to do.
All elements which match that predicate are retained in the Stream.
Then you call its toArray() method which requires something to start it off, so you pass the type String=array of Strings, followed by the double colon operator and the keyword new which means to look for the constructor for that class. Yes, you can use that construct even though you never write new FooArray().
You do of course often write new String, so what the Stream does is to look for that construct and supply it with the int number, i.e. the size of the Stream, to create such an array.
posted 3 years ago
And note what Mr van Hulst said!
No, tomorrow we rule the world! With this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java