• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Filter list with criteria

 
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to filter a list, which contains some data object. Based on the criteria I want to filter out the object of the list and return the list.
Below is the code for reference.


From the above, I want only one object in the final list which contains the "abc".
I missed the code, can somebody help me to get the fileter list based on the criteria.

Thanks,
Atul
 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do these statements return (what do these Stream methods map and filter return)?

map(a -> Arrays.stream(a))
filter(c -> sentence.contains(c.toString())

And, there is a Stream method peek(), which is meant to be used for debugging; try using it to figure whats happening with the stream methods at each step.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still working on my Java8 skills. Here's a possible solution. Not quite as Java8'esq as I'd like.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this?
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Carey Brown
Here is a solution:



NOTES:
map: Transforms Data to a String (reason) [EDIT: returns a stream of Data to stream of String]
map: Transforms String (reason) to arrays of Strings with two elements each [EDIT: returns stream of two arrays]
flatMap: Transforms the elements of two streams (from two arrays to two streams) to a single stream with all the elements
filter: This one is easy.
collect: The resulting (filtered) reason string element is written to a List collection of type String.

map, flatMap and filter are intermediate stream operations. The peek() method I had mentioned in the earlier message is also an intermediate operation. collect is a terminal operation; this is a reduction operation The stream is reduced to a collection.

One can introduce peek() method in-between the map/flatmap/filter methods in the above code and see the data transformation. You see the data gets transformed as it flows thru the stream This is one of the main features of the streams. Also note that fliter, map, and reduce (in this case collect) are the main and most commonly used functions of stream operations.

Note the code posted by the OP:



I was hoping OP would try little bit and post back some reply. The above code doesn't compile.
- The statement sentence.contains(c.toString()) is not valid. See the NOTES above for details.
- filter returns a Stream. Note that the intermediate stream operations (in this example, map, flatMap, filter and peek) return a Stream.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Prasad Saya
Your approach returns a List<String> not a List<Data>.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The use of Scanner.tokens() allows for lazy parsing of the reason. In connection with anyMatch() the parsing can be short circuited as soon as a match is found. This is not true of the split() approach.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one returns a List<Data> (little clunky, but it does work):



EDIT: Code refined.
 
Greenhorn
Posts: 27
1
IBM DB2 Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking of this in the checkForReason method, using a set for the dataList...



Don't know if this helps at all.
 
Atul More
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasad,

Thanks for reply.
The thing was came to my mind as well but I was not sure, whether it will work or not as I am new to Java8 functional programming.

I made some changes to the solution which you provide and its working as expected.

Thank you,
Atul
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Atul More wrote:Hi Prasad,
Thanks for reply. The thing was came to my mind as well but I was not sure, whether it will work or not as I am new to Java8 functional programming. I made some changes to the solution which you provide and its working as expected.
Thank you,
Atul



You are welcome!
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about a different approach ?

We simply delegate our custom logic to another class (ReasonChecker) which maintains its own state. The object can be even cached.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both the reasons and the sentence are stored inefficiently for the purpose of finding the intersection of two sets of words. A HashSet is much more efficient than a list. And we only care if one word is the same in both sets so when the first match is found we can abort any further comparison. The set for reasons can be made in the Data constructor and stored in the Data object. The set for the sentence could be made on the fly or made and saved so that it could be reused. Another advantage of using a set is that multiple occurrences of the same word will only appear once in a set (we don't how long a sentence could potentially get).

This code has some smaller methods that improve performance and readability.
sentenceToWords() -- Creates set of words from sentence. Set may be saved.
containsAny() -- Returns true on the first match of words from two sets
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey
Have a cow for your efforts and the advances in your use of java 8! Your solution is about what I had in mind as well. Some small remarks:

1) I would add a constructor (Data(String... reasons)) to the Data class, for convenience, when there are only a few reasons. Saves from creating a full String parameter that then must be decomposed.

2) The splitting of the sentence can also be nicely done with Pattern.compile(...).splitAsStream(...).collect(...)

3) Since the 'toSet() is not guaranteed to return a HashSet (there is a way to specify that you want a HashSet, by the way) you could also do a sort in the SentenceToWord method, returning a LIst, and use a BinarySearch as filter. It would make for some nice practise!
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Piet,
thanks for the cow and the pointers, I'll be looking into them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic