aspose file tools*
The moose likes Java in General and the fly likes ArrayList without duplicates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList without duplicates" Watch "ArrayList without duplicates" New topic
Author

ArrayList without duplicates

shriganesh kakade
Greenhorn

Joined: May 10, 2012
Posts: 23
In my code i want to avoid the duplicate entries in the arraylist.
I am getting problem in that.
I have used the hash map but still can't remove the duplicate rows with the same name.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Dont want duplicates ? Use a set


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

now i know why someone recommended a set to me. here is how you can do it with a list though.


SCJP
Visit my download page
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Randall Twede wrote:



<char> or <Character> ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
The problem with checking whether your List already contains the element is that you are going to go into quadratic complexity. You can get back to linear complexity with a Set and List together. Adding to a HashSet or ArrayList is constant or amortised constant time, and the contains() method of HashSet also runs in constant time. You create both the List and Set, then add every element to the Set, retaining the return value of the add() method. If that is true, you can add to the List.
Yes, if you go through the API for Set#add(E) and look for its return value, you find you can dispense with the contains() call.
When you have finished all the adding, you can simply let the Set go out of scope.
I presume the ready-made combined class (LinkedHashSet) doesn’t suit your purposes?
shriganesh kakade
Greenhorn

Joined: May 10, 2012
Posts: 23
Thank you very much for your valuable replies.
what i want is add the object to the list and if one field from that object (consider name), is present in that list then delete that one
and replace it with the new one i.e. same name but other values related to that name will be changed to newly entered values.
how should I do this?
i have tried some like if that list does not contain that name then only add the object using the constructor.
i.e.
empEducList.add(new empEducation(emp.getEmpEdu().getDegName(), emp.getEmpEdu().getUniversity(), emp.getEmpEdu().getPercentage()));

here if the degree name is already present in the list with some values of other fields then replace that one with new values of percentage and University.
but here i have done if name is not present then add, how should i do above thing when it is present? Is it possible?
Please help...
Athira Vineeth
Greenhorn

Joined: Sep 06, 2010
Posts: 16
hi

try this
[Moderator action: Removed ready-made solution. Athira Vineeth, please DontBeACodeMill.(⇐click)]
shriganesh kakade
Greenhorn

Joined: May 10, 2012
Posts: 23
i can't understand how to do it please help..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
That sounds different from what you asked at first. I don’t quite understand what you need. I think you ought to draw a diagram of what you are doing, on paper, with before and after states. Then you might be better able to see what you want.
Seeing objects and their fields makes me suspect that you actually want a Map, but I can’t be certain.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

List allows duplicates
Set doesnot allow duplicates.


http://www.lifesbizzare.blogspot.com || OCJP:81%
shriganesh kakade
Greenhorn

Joined: May 10, 2012
Posts: 23
Campbell Ritchie wrote:That sounds different from what you asked at first. I don’t quite understand what you need. I think you ought to draw a diagram of what you are doing, on paper, with before and after states. Then you might be better able to see what you want.
Seeing objects and their fields makes me suspect that you actually want a Map, but I can’t be certain.


Actually i want to first check whether some value present in arraylist or not..to avoid duplicates.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

shriganesh kakade wrote:
Campbell Ritchie wrote:That sounds different from what you asked at first. I don’t quite understand what you need. I think you ought to draw a diagram of what you are doing, on paper, with before and after states. Then you might be better able to see what you want.
Seeing objects and their fields makes me suspect that you actually want a Map, but I can’t be certain.


Actually i want to first check whether some value present in arraylist or not..to avoid duplicates.



That's pretty straightforward. Have you looked at the JavaDoc to see if there is a method to check if a List "contains" a particular element?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
shriganesh kakade
Greenhorn

Joined: May 10, 2012
Posts: 23
Henry Wong wrote:
shriganesh kakade wrote:
Campbell Ritchie wrote:That sounds different from what you asked at first. I don’t quite understand what you need. I think you ought to draw a diagram of what you are doing, on paper, with before and after states. Then you might be better able to see what you want.
Seeing objects and their fields makes me suspect that you actually want a Map, but I can’t be certain.


Actually i want to first check whether some value present in arraylist or not..to avoid duplicates.



That's pretty straightforward. Have you looked at the JavaDoc to see if there is a method to check if a List "contains" a particular element?

Henry


thank you for your reply
yes i have used contains method but my problem is that i have used constructor of the class to add new element to the list and so even if code correctly checks
whether list contains the element or not, it continuously adds the element when the page loads..
I am new to this.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

shriganesh kakade wrote:
thank you for your reply
yes i have used contains method but my problem is that i have used constructor of the class to add new element to the list and so even if code correctly checks
whether list contains the element or not, it continuously adds the element when the page loads
..
I am new to this.


Okay, we are definitely missing some context here, because that sentence makes little sense. Can you elaborate?

Henry
shriganesh kakade
Greenhorn

Joined: May 10, 2012
Posts: 23
i want to add class object to the arraylist,when the user submits the page.
but before adding, it should be checked that whether the list contains that value.
if it doesn't contain then only the object should be added..
and so in my code i had made use of contains method to check existence.if exists then error will be displayed.
if does not exist, i had used constructor of the class to add the value.
i.e.
list.add(new Emp(fname, addr, emailid));

and problem is when i enter the duplicate value it still adds that value..
i can't understand where is the problem
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

shriganesh kakade wrote:i want to add class object to the arraylist,when the user submits the page.
but before adding, it should be checked that whether the list contains that value.
if it doesn't contain then only the object should be added..
and so in my code i had made use of contains method to check existence.if exists then error will be displayed.
if does not exist, i had used constructor of the class to add the value.
i.e.
list.add(new Emp(fname, addr, emailid));

and problem is when i enter the duplicate value it still adds that value..
i can't understand where is the problem



Now it makes sense -- you don't know what is the issue. In this case, no context is better than incorrect context -- as was from your previous post.


Well, you will have to debug it -- add debugging messages around the part that adds to the list, the part that checks the list, and most importantly, the response from the contains() method. Hopefully, you'll have enough information to figure it out. If not, then feel free to report the info back here.

Henry
shriganesh kakade
Greenhorn

Joined: May 10, 2012
Posts: 23
Thank you very much..
I am checking that one..
I will post if somewhere i got error..
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

you need to make the object first.
Override the equals method on your class (and the hashcode method)
as the default/inherited equals method uses object identity.
So as you are making a new object every time it will never exist in the list.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Wendy Gibbons wrote:you need to make the object first.
Override the equals method on your class (and the hashcode method)
as the default/inherited equals method uses object identity.
So as you are making a new object every time it will never exist in the list.


To elaborate on this: when contains(element) is called on a collection, it iterates over all the objects in the list calling element.equals(obj). Now, the default implementation of equals, defined in java.lang.Object, is a single line and it says return (this == obj). That means only if these two objects have the same exact reference are they considered equal. Collection.contains returns true only if a call to equals returns true. So, unless you override the equals and hashCode methods in the classes you design, you will not be able to use them effectively with collections.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Dennis Deems wrote:To elaborate on this: when contains(element) is called on a collection, it iterates over all the objects in the list calling element.equals(obj).

That's the general rule, but there are exceptions.

HashSet first uses the result of hashCode() to find the bucket the element should be in. It then iterates over the objects in that bucked using equals(Object). It's therefore important that hashCode() and equals(Object) "match", as specified in the Javadoc of java.lang.Object.

TreeSet actually doesn't use equals(Object) or hashCode() at all - it instead uses java.lang.Comparable's compareTo or java.util.Comparator's compare method. This actually makes the class a bit dangerous, as it can break symmetry:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shriganesh kakade
Greenhorn

Joined: May 10, 2012
Posts: 23
I had overridden equals() and hashCode() methods,
then before adding class object, i checked whether name from the list equals to the name from the object, by iterating through the list,
then if result is false then add the object.
And it is working..
Thank you all for your help.
I will again post if got problem.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

shriganesh kakade wrote:
I will again post if got problem.

You are welcome
 
wood burning stoves
 
subject: ArrayList without duplicates