tags:

views:

117

answers:

8

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?

A: 

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.

Aaron
What do you mean? For one, Strings aren't primitives anyway - and secondly, this idiom does work correctly with any Object (so long as its `equals` and `hashcode` methods are implemented correctly).
Andrzej Doyle
Correct but without implementing equals/hashcode it will not work by default (as the OP would theorize)...and based on this question I am guessing the OP is obviously not aware of that...and yes Strings are not primitives but the moment you deviate form String and/or primitive you need to implement hashcode/equals to get expected results.
Aaron
@Andrzej Doyle edited to be more clear
Aaron
Integer/Double/Boolean are not primitives/Strings and they already provide equals and hashCode.
Steve Kuo
@Steve Good thing we got that clarified, everyone please continue with your day...life can now move forward...yawn
Aaron
+2  A: 

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.

willcodejavaforfood
You meant `LinkedHashSet`, not `LinkedListSet`.
haylem
@haylem - Thanks mate :)
willcodejavaforfood
A: 

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.

wds
+1  A: 

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>();
Andy Thomas-Cramer
A: 

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)) {
    this.rssFeedURLs.add(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.

haylem
A: 

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.

Aviad Ben Dov
A: 

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

String rssFeedURL = "http://stackoverflow.com";
this.rssFeedURLS = new ArrayList<String>();
this.rssFeedURLS.add(rssFeedURL);
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.

Michael McGowan
A: 

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.

Mr Morgan