• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList without duplicates

 
shriganesh kakade
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont want duplicates ? Use a set
 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i know why someone recommended a set to me. here is how you can do it with a list though.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:



<char> or <Character> ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

try this
[Moderator action: Removed ready-made solution. Athira Vineeth, please DontBeACodeMill.(⇐click)]
 
shriganesh kakade
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can't understand how to do it please help..
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List allows duplicates
Set doesnot allow duplicates.
 
shriganesh kakade
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shriganesh kakade
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much..
I am checking that one..
I will post if somewhere i got error..
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
shriganesh kakade
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shriganesh kakade wrote:
I will again post if got problem.

You are welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic