• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Filtering String array elements

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a String array as

String finalArray[] = {"1", "a", "abc", "0.5", "1edfc"}

What is the best way to split the finalArray into two array

String s1[] = {"1", "0.5", "1edfc"} // should contain all strings who have number in it

String s2[] = {"a", "abc"} // should contain all strings who have all characters including special characters if any in it
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[123], 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.
 
Campbell Ritchie
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And note what Mr van Hulst said!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic