I have created a function which return the List<String>.
I am passing a string "(A,1),(Y,4),(F,5)" and I want to split it out and get the 3 individual string object which are like A,1 Y,4 F,5 as 3 individual objects in a list.
I used Java8 to create it but it return me only one value like A,1
The function is
The same thing I have achieved via this function:
How can I work using Java8.
Any help would be appreciated.
Regexes aren't specific to Java8, so I shall duplicate this discussion in one of the ordinary Java® fora.
What are you trying to do? You are starting with a String, which looks familiar to me, so have you asked anything similar before. Then you are splitting it, and remember what you get from splitting a String: a String. I think you should go back to basics: start by splitting that String and print out the results with Arrays.toString(). Then you will find that your match() call does something completely different.
Why are you trying to create substrings? If the text is called seats, why aren't you using a Seat object or a List<Seat> here?
I know its not Java8 specific feature but just want to use Java8 coding features to achieve which I have achieved in the second methos which I have mentioned.
The "seats" is a string, I forgot to replace it with str parameter.
posted 1 year ago
So what did you get when you split the String with some regex? What will happen if you use the methods of Matcher, e.g. find, group? How many items will you get in line 4 and line 5? How does the Stream in the first code block differ from the loop in the old‑style code? What will happen if you map r, which is a String, to new String(r)?
Even I don't want complete solution, I have written the code but its not working as per expectation, just want to help to correct it.
Want to understand where I missed the thing.
I am still in learning phase of Java 8.
I am able to do it in earlier Java version but trying things using Java 8 features.
Hope you understand.
posted 1 year ago
In one case you are iterating the String in a loop and adding all the group()s to a List: easy enough with Java5‑style code. With the input you showed, you are going to run the loop thrice and get a three‑element List.
In the other case, you are starting with one object (Stream.of) and then calling a method of Matcher. All the methods of Matcher I could find return one thing, and none returns a String or anything like that. The group() method returns a String, one only, so, after using that, you are going to get a one‑element Stream downstream from there.
If however you do what you say you are doing, namely split the String, you can get a String: look at the return value for this method. You will of course require a different regex.
It might be possible to create multiple Streams in a loop and then concatenate them with a method of Stream, but I think that is going to produce some very strange‑looking code.