Use of contains in Java ArrayList<String>

0 votes
asked Oct 15, 2010 by mr-morgan

If I have an ArrayList of String forming part of a class in Java like so:

private ArrayList<String> rssFeedURLs;

If I want to use a method in the class containing the above ArrayList, using ArrayList contains to check if a String is contained in this ArrayList, I believe I should be able to do so as follows:

if (this.rssFeedURLs.contains(rssFeedURL)) {

Where rssFeedURL is a String.

Am I right or wrong?

10 Answers

0 votes
answered Oct 15, 2010 by aaron-mciver

Right...with strings...the moment you deviate from primitives or strings things change and you need to implement hashcode/equals to get the desired effect.

EDIT: Initialize your ArrayList<String> then attempt to add an item.

0 votes
answered Oct 15, 2010 by willcodejavaforfood

Yes, that should work for Strings, but if you are worried about duplicates use a Set. This collection prevents duplicates without you having to do anything. A HashSet is OK to use, but it is unordered so if you want to preserve insertion order you use a LinkedHashSet.

0 votes
answered Oct 15, 2010 by wds

Perhaps you need to post the code that caused your exception. If the above is all you have, perhaps you just failed to actually initialise the array.

Using contains here should work though.

0 votes
answered Oct 15, 2010 by andy-thomas

You are right. ArrayList.contains() tests equals(), not object identity:

returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))

If you got a NullPointerException, verify that you initialized your list, either in a constructor or the declaration. For example:

private List<String> rssFeedURLs = new ArrayList<String>();
0 votes
answered Oct 15, 2010 by haylem

Your question is not very clear.

  • What's your code exactly doing? Give more code.
  • What's the error you're getting?

You say you get a null-pointer. You cannot get a null pointer as a value returned by contains().

However you can get a NullPointerException if your list has not been initialized. By reading your question now, I'd say that what you show here is correct, but maybe you just didn't instantiate the list.

For this to work (to add a feed URL if it isn't already in the list):

if (!this.rssFeedURLs.contains(rssFeedURL)) {

then this declaration would do:

private ArrayList<String> rssFeedURLs = new ArrayList<String>();

or initialize your list later on, but before trying to access its methods:

rssFeedUrls = new ArrayList<String>();

Finally... Do you really need a List? Maybe a Set would be better if you don't want duplicates. Use a LinkedHashSet if preserving the ordering matters.

0 votes
answered Oct 15, 2010 by aviad-ben-dov

You're correct. As others said according to your comments, you probably did not initialize your ArrayList.

My point is different: you claimed that you're checking for duplicates and this is why you call the contains method. Try using HashSet. It should be more efficient - unless you need to keep the order of URLs for any reason.

0 votes
answered Oct 15, 2010 by michael-mcgowan

You are right that it should work; perhaps you forgot to instantiate something. Does your code look something like this?

String rssFeedURL = "";
this.rssFeedURLS = new ArrayList<String>();
if(this.rssFeedURLs.contains(rssFeedURL)) {
// this code will execute

For reference, note that the following conditional will also execute if you append this code to the above:

String copyURL = new String(rssFeedURL);
if(this.rssFeedURLs.contains(copyURL)) {
// code will still execute because contains() checks equals()

Even though (rssFeedURL == copyURL) is false, rssFeedURL.equals(copyURL) is true. The contains method cares about the equals method.

0 votes
answered Oct 15, 2010 by mr-morgan

Thanks to you all for answering so quickly. I could always use a set but I have the ArrayList working now. The problem was that in the constructor of the class containing the ArrayList, I was not saying:

public RSS_Feed_Miner() {
    this.rssFeedURLs = new ArrayList<String>();

D'Oh! for a Friday afternoon.

0 votes
answered Oct 12, 2015 by thilina-anuradh
ArrayList<String> newlyAddedTypes=new ArrayList<String>();





//called here

0 votes
answered Sep 15, 2017 by syed-danish-haider

In the below code we have taskfilled as arraylist,retrieveTaskidForDelete as method by which we add the value in task filled arraylist and checkid is String.we need to check if the string value contain in arraylist.

String checkid;
ArrayList<String> taskfilled=retrieveTaskidForDelete();
                if(!taskfilled.contains(checkid)) {
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter