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.
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.
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>
Edited 6 Years Ago by peter_budo: Keep It On The Site - Do not post asking for an answer to be sent to you via email or PM.
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:
Runtime run = Runtime.getRuntime();
Process pr = ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.