aspose file tools
The moose likes Beginning Java and the fly likes List String Contains Method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "List String Contains Method" Watch "List String Contains Method" New topic
Author

List String Contains Method

Sam Benry
Ranch Hand

Joined: Mar 21, 2008
Posts: 89


this code shows that contains method is case sensitive. Is there any alternative method or way to make it not case sensitive and thus return true?
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Hello Sam,

this code shows that contains method is case sensitive. Is there any alternative method or way to make it not case sensitive and thus return true?


Well, in fact the contains() method uses the Object.equals() method. The String.equals() is case-sensitive. For String comparison, there is a String.equalsIgnoreCase() method. You could define your own datatype implementing its own equals() (and hashCode()!) method which uses String.equalsIgnoreCase() internally.
However, note that contains() is a very expensive method for lists, since all elements could be iterated. Other collection types might be more appropriate.

Regards,
Thomas


SCJP 5.0, SCJD in progress
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

If you keep using String, most other collections have the same impact because you can't use equals() - and that's what almost all collections are using.

However, TreeSet can help you out:

TreeSet uses either the compareTo method of the objects, or (in this case) a Comparator. If the Comparator says the objects are equal (compare returns 0), the objects are equal. With String.CASE_INSENSITIVE_ORDER you can acchieve a collection which ignores the case of all contents.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: List String Contains Method
 
Similar Threads
Current JSP(JSF) doesn't match the URL
How can I determine if a string array contains a particular string?
doubt - generics in method ...
Problems searching through a list
display a arraylist value in JSP using JSTL