Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help on filtering (java 8)  RSS feed

 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have an arraylist that contains objects that have a certain status. I would like to filter this arraylist based on the different types of statuses. For instance, I would like to filter Available and Unavailable entities.
In my research on how to do it, I found several methods to do this. I did not find a method that I found suitable and that I could apply at the same time

The following example seemed very nice, but honestly I cannot read what it actually does:
http://stackoverflow.com/questions/33992279/filter-arraylist-by-multiple-variables-in-java

Could you help me read the examples? I would like to try the filtering stuff myself. How I read it now:


1: Declare method that uses ArrayList containing Student objects.
a Predicate of type Student (???)
Filter on text that contains the following letters: acceptStudent (???)
2. Create a new arraylist containing Student objects
3. For all students in the original arraylist,
4. if ???
5. Add it to the second list (filtering in this case means making a new arraylist that does contain the objects that do comply with the criterion)
8. Return the list.


I cannot believe that this sorting method is complete. I believe that some elements are missing. I also tried to read the second one (before Java 8) from that same webpage:


1: Declare method that uses ArrayList containing Student objects. Do not know what the function of the booleans is here.
The other (int and String-variables) show what the value of the variables should be, to be filtered.
6. Make a new arraylist, again containing Student objects
7. For all the students in the original list
8. Perform checks. Again, I do not know what the booleans actually do
16. If all requirements have been met, add it to the second list.
19. Return the second list


This one seems more complete, but I do not fully understand this one either. Moreover, I think there is an error, since the method is given a void and a return.
Could you guys help me to read these codes? Then, I could give it a try myself and hopefully I will be able to do the filtering myself


 
Carey Brown
Bartender
Posts: 3022
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sander Hoovenaar wrote:I have an arraylist that contains objects that have a certain status. I would like to filter this arraylist based on the different types of statuses. For instance, I would like to filter Available and Unavailable entities.

Rather than describe the code you found on the internet that may or may not apply to your situation, I would like to see a more detailed set of requirements for your specific application. For instance,
  • Do you have one status variable that can have a number of different states or do you have multiple status variables?
  • Can you code the criteria for a specific test, or does the test need to handle a variety of criteria combinations that are only known at run time?
  • Do you want to end up with a single result list for items that satisfy the criteria, or are you trying to split the list into two (or more) lists based on the criteria?
  •  
    Ronald Hoovenaar
    Ranch Hand
    Posts: 53
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:
    Sander Hoovenaar wrote:I have an arraylist that contains objects that have a certain status. I would like to filter this arraylist based on the different types of statuses. For instance, I would like to filter Available and Unavailable entities.

    Rather than describe the code you found on the internet that may or may not apply to your situation, I would like to see a more detailed set of requirements for your specific application. For instance,
  • Do you have one status variable that can have a number of different states or do you have multiple status variables?
  • Can you code the criteria for a specific test, or does the test need to handle a variety of criteria combinations that are only known at run time?
  • Do you want to end up with a single result list for items that satisfy the criteria, or are you trying to split the list into two (or more) lists based on the criteria?


  • Okay. I need two filters, one for every class, since both of the classes have different Statuses. The criteria need thus to be different.
    The description below is the same for both filters:
    There is only one status variable for the objects, with a different set of states.
    The criteria are known on forehand, e.g. do this when Object of class A has status available, do this when Object of class B has Status ReadyToBeProcessed
    I would like to have a single list with objects that meet the criteria. With this list, I will take further action with other methods.
    I would like the filter to be applied on ArrayLists (for both classes-objects).
     
    Carey Brown
    Bartender
    Posts: 3022
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sander Hoovenaar wrote:Okay. I need two filters, one for every class, since both of the classes have different Statuses. The criteria need thus to be different.
    The description below is the same for both filters:
    There is only one status variable for the objects, with a different set of states.
    The criteria are known on forehand, e.g. do this when Object of class A has status available, do this when Object of class B has Status ReadyToBeProcessed
    I would like to have a single list with objects that meet the criteria. With this list, I will take further action with other methods.
    I would like the filter to be applied on ArrayLists (for both classes-objects).

    Thank you, that helps. This is what I think you are saying:
  • You have two different classes.
  • Each class has its own status field.
  • The status field for the two classes do not represent the same thing.
  • For one class the status may be Available, for the other class the status may be ReadyToBeProcessed.
  • You are dealing with either a list of ClassA or a list of ClassB, but not a list containing both A and B.
  • You will have two filter methods, one for a list of ClassA and a different one for a list of ClassB.

  • Are you looking for an implementation using Java8 Lambdas? Or, will Java 7 suffice?
     
    Ronald Hoovenaar
    Ranch Hand
    Posts: 53
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That is correct. I do not prefer a method of either java 8 or java 7. I just want to get the job done most effectively.
     
    Carey Brown
    Bartender
    Posts: 3022
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In Java 7
  • Create a new empty list for result
  • Loop through original list of, say, ClassA
  • IF A.getStatus() == Available
  • THEN add A to result list
  • Do something useful with result list

  •  
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sander Hoovenaar wrote:I have an arraylist that contains objects that have a certain status.

    Am I right in thinking that all these objects have a common supertype, and that your ArrayList will be defined as ArrayList<Supertype>?

    Because it that's the case, then you could use polymorphism.

    Winston
     
    Ronald Hoovenaar
    Ranch Hand
    Posts: 53
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Sander Hoovenaar wrote:I have an arraylist that contains objects that have a certain status.

    Am I right in thinking that all these objects have a common supertype, and that your ArrayList will be defined as ArrayList<Supertype>?

    Because it that's the case, then you could use polymorphism.

    Winston

    No. For this filter function I do not work with objects that have a common supertype.
     
    Ronald Hoovenaar
    Ranch Hand
    Posts: 53
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:In Java 7
  • Create a new empty list for result
  • Loop through original list of, say, ClassA
  • IF A.getStatus() == Available
  • THEN add A to result list
  • Do something useful with result list


  • Works like a charm. Thanks for simplifying the problem here.
    If I make changes to the object of the new arraylist, how will I be able to apply those changes to the same object in the original arraylist as well?
     
    Paweł Baczyński
    Bartender
    Posts: 2054
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sander Hoovenaar wrote:If I make changes to the object of the new arraylist, how will I be able to apply those changes to the same object in the original arraylist as well?

    If it is the same object (not a copy) you wouldn't need to.
    The changes would be visible. It wouldn't matter where you got the reference from (which collection, variable, etc...)This prints:
    List 1: [A, B]
    List 2: [B, C]
    List 1: [A, X]
    List 2: [X, C]

     
    Ronald Hoovenaar
    Ranch Hand
    Posts: 53
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see now. Did not expect that, but it's wonderful
    How about removing one object from both lists? It throws an exception and I think that makes sense, because I throw away one line, while it is still looping.
     
    Ronald Hoovenaar
    Ranch Hand
    Posts: 53
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just made a new For-loop just below it. That seems to work
     
    Campbell Ritchie
    Marshal
    Posts: 55793
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Isn't there also a removeIf method in ListCollection now? There doesn't seem to be retainOnlyIf so you will need a generous sprinkling of bang signs ! in your code.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!