• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use contais / equals for arrayList of RowFilter.regexFilter  RSS feed

 
joe encalada
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you can help me. Every time I execute System.out.println(filters.contains(rf)) it gives me false, I tried to modify equals on rowfilter but I can't get it. I modify some code from the net and this is it.


 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because they are different objects.

You created two instances of the class. Just because you used the same parameter to create each instance does not automatically make the objects equal.

The contains(...) method will use the equal(...) method of each instance to determine if the objects are the same.

The equals method is NOT overridden so each instance will be considered to be different.
 
joe encalada
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it. But how to override the equals method in RowFilter.I can't figure out.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your code. Always use the tags; doesn't it look better
About overriding equals: there are three standard references I now about, Bloch, Odersky Spoon and Venners, and Langer. You shou‍ld be able to find them from thei links in two of my old posts: 1 2. The equals method is a standard difficult problem in Java┬« coding.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't override the equals method. That class is part of the JDK.

Why do you think you need to do this? Why are you creating two instances?


Instead  you can do something like :



 
joe encalada
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating  two instances because this code is a test of other bigger program. In that program I have  an arrayList of  RowFilter. Something like this:





And then In other moment I need to remove a filter (for example :  RowFilter.regexFilter("TD002")  )   so:




Thanks for your welcome. I hope you can help me.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already showed you how to create a reference to a filter.

If you have many filters to track you can use a Hashmap to get the appropriate filter. Basic code would be:



Then when you want to use a specific filter you can do something like:



Now you only ever create a single instance of each filter.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:You don't override the equals method. . . .
Sorry. I thought it was the other class which needed the equals method overriding. Maybe I ought to learn to read threads properly.
 
joe encalada
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   Rob Camick your  suggestion was really helpful. So, lot of thanks my friend.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!