• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Filtering String array elements  RSS feed

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • 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
 
Saloon Keeper
Posts: 10104
212
  • Mark post as helpful
  • send pies
  • 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: 64096
215
  • Mark post as helpful
  • send pies
  • 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: 64096
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!