aspose file tools*
The moose likes Java in General and the fly likes Remove dates from an array list using iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Remove dates from an array list using iterator " Watch "Remove dates from an array list using iterator " New topic
Author

Remove dates from an array list using iterator

Josiah smith
Greenhorn

Joined: Feb 07, 2012
Posts: 8
I am trying to modify the ArrayListofDates so that after generating the random dates it loops thru the Dates and checks to see if it is already in the arraylist. If there is a duplicate it then should prompt the user if they want to remove that date from the array. I am trying to use the contains and remove method of array list. Here is the code and it compiles fine but does not work as intended.



Any suggestions would be greatly appreciated.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Hi Josiah,

In the code that you have posted I couldn't see where you were using "contains" but I see a set.add(element) function. Either ways, since you are referring to a list of Dates, so you would have to override and write your own contains definition.

Regards,
Praveen.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
My bad! This is the actual documentation for contains method

boolean contains(Object o) -
"Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))."

As you can see, the contains will return true if and only if the "equals" function returns true(assuming you have atleast 1 element in the list). But by-default the equals method here refers to the equals definition of Object class.

So please ignore the suggestion in the previous post, you would have to override the equals method instead(to compare 2 Dates).

Josiah smith
Greenhorn

Joined: Feb 07, 2012
Posts: 8
Do you have any examples of code that uses iterators and the contains method. The code I created works for integers within an array, but when I make the array contain dates it doesn't produce the correct output.

Thanks for your help
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
You are right, basic datatypes like Integers and Strings have their own version of equals method, so you can use a contains function on these types of lists directly.

I'll give you an example, see if you can write the actual code.



After you write the equals logic according to your requirements, subsequent calls to contains method of a CustomDate list would further call this equals method.

Note - If you override equals method for a class, you need to override another default method called hashCode. Do read up more on this before you do the override.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
Praveen Kumar M K wrote:. . . basic datatypes like Integers and Strings . . .

Note - If you override equals method for a class, you need to override another default method called hashCode. Do read up more on this before you do the override.
Why are you calling Integer and String basic? There is nothing basic about them, as if they were primitives.

Please don’t double-space code in the code tags, nor write line numbers, which are added automatically.
Beware of dates like 8 2 2012. If the month is 2, that counts as March in the Calendar class. I would have preferred to get a daysInMonth value from the switch block and use the random call once only.

And welcome to the Ranch
Josiah smith
Greenhorn

Joined: Feb 07, 2012
Posts: 8
Hello,
Sorry If i double spaced i have been working on this for days. This is what I am coming up with to detect and prompt removal for the duplicate dates. I am having a rough time though. I think my error lies in the condional statement. I have been working on this for days and nobody has really been able to help me. Thank you very much for your time.
V/r,
Josiah
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Josiah smith wrote:Hello,
Sorry If i double spaced i have been working on this for days. This is what I am coming up with to detect and prompt removal for the duplicate dates. I am having a rough time though. I think my error lies in the condional statement. I have been working on this for days and nobody has really been able to help me. Thank you very much for your time.
V/r,
Josiah


I found your original way of using a Set quite ok.

I'd suggest using a slight trick with the dates however: you can get the date "value" as a long with the date.getTime() method. Now just define your set to contain Long (note the capical 'L') elements. Then you can simple write something like


EDIT: an addition to your original post. You are aware that you are running through the iterator already once, before you do your actual "test and remove" run?

EDIT2: and make sure to define the set outside of the while loop!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove dates from an array list using iterator