• Post Reply Bookmark Topic Watch Topic
  • New Topic

'contains' in AbstractCollection  RSS feed

 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I could find out 'contains' method in HashSet(or Collection for that matter) is an 'equals' match;rather than a substring match.
Also ,unless we override this,there is no out-of-box API in the Collection classes to perform the substring index-of operation.
Is this observation correct?
Also,do java string operations by default support substring matching for unicode strings?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ranadhir Nag:
As far as I could find out 'contains' method in HashSet(or Collection for that matter) is an 'equals' match;rather than a substring match.
Also ,unless we override this,there is no out-of-box API in the Collection classes to perform the substring index-of operation.
Is this observation correct?

Yes it is. If you want substring matching, you need to iterate over the collection yourself. You shouldn't override the contains or indexOf methods for this because it will break the contracts for those methods. Instead, implement new method (e.g. containsSubstring and indexOfSubstring) that will do what you want.

Also,do java string operations by default support substring matching for unicode strings?

Java Strings support unicode, so yes, they also support substring matching for those. Check the indexOf, lastIndexOf and contains methods (keep in mind: a String IS-A CharSequence).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!