hi forum,
i am using an arraylist for storing a list of objects.i dont want the objects in the list to be ordered but the list should be duplicate free.how can i achieve this.plz help quickly.provide source code if possible.thanks in advance.

A quick and dirty way of doing it would be to create a new ArrayList (arrList2), use a loop to read each object in the original ArrayList (arrList1), use the indexOf method to check arrList2 for the presence of the object that you are checking, add the object to arrList2 if the object isn't present in the list, and replace the ArrayList instance in arrList1 with arrList2.

There is also a contains(Object o) method on List. So, you could check to see if the object is in the ArrayList before adding the Object.

Or you could extend ArrayList with your own class that checks to see if Object is present in the ArrayList using the contains method and then adding the new object only if it isn't present in the current list.

I actually would recommend using Kate's advice though.

Regards,

Nate

Hashset is an excellent idea, but if you want to just use arraylists then the contain method hook mentioned is the way to go.

Using contains would indeed work, but if the List gets large it gets ever more expensive to do the check (it's an O(n) operations, iterating over the entire dataset until either the end is reached or a match is found).
Using a container that prohibits duplicates is indeed a more logical choice.
HashSet checks for duplicates during insert based on hashCode and equals (so make sure to correctly override both for your custom classes when using them in a HashSet) which is a lot faster potentially.

thanks u guys for ur quick replys. i m intending to replace the arraylist collection by an hashSet collection.can anyone provide code to implement this.i m new to java and so i didnot understand the equals and hashcode overridding stuff that jwenting mentioned.any light on this ???? thanks again.

hascCode is used in concert with equals to determine whether (and where) in a HashSet (or other hash based collection) an object exists.
You need to have these methods in working order (and thus overridden from the defaults in Object) in order for the algorithms to work correctly.

There's a good explanation in "Effective Java" by Joshua Bloch.

hi, guys
where i have a situation that is regarding session. i opened 10 browsers with same user and password.
if i had make any changes to 1 st broser, then it will effect those changes in another browsers.
what happens if i close the 1 st browser and try to use the session of 1 st browser in 5th browser?
my mail: <EMAIL SNIPPED>

commented: thread hijacking zombie master -2

don't hijack other peoples' threads.
don't revive long dead threads.
do your own homework.
don't ask for private help by email.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.