Set working incorrectly in JAVA - java

I just want to remove duplicate elements from list. To do this I have written a POJO class Student as :
class Student{
private String roll;
private String name;
public Student(String roll, String name) {
this.roll = roll;
this.name = name;
}
#Override
public boolean equals(Object obj) {
Student st = (Student) obj;
return st.getRoll().equals(roll);
}
public String getRoll() {
return roll;
}
public void setRoll(String roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return roll ;
}
}
And a test class as below :
public class TestMain {
public static void main(String[] args) {
List<Student> list = Arrays.asList(
new Student("19", "goutam kumar singh"),
new Student("20", "goutam kumar singh"),
new Student("11", "goutam kumar singh"),
new Student("19", "goutam kumar singh")
);
List<Student> arrayList = new CopyOnWriteArrayList<>(list);
Set<Student> set = new HashSet<>();
for(Student st : arrayList)
set.add(st);
System.out.println(set);
}
}
but in the output all the four elements in the set but i am expecting only three element as fourth element is duplicate and must be removed.
Where I am going wrong?

You have to override the hashCode() method too. Override hashCode() methods for those property for which you override equals() method.
While working with Collection it's useful to remember the contract between hashCode() and equals() method -
1. If two objects are equal, then they must have the same hash code.
2. If two objects have the same hashcode, they may or may not be equal.
For more information you may visit this link

A HashSet stores elements internally as keys in a HashMap. Because of this, it will use your Student object as the keys for that map, using the hash code for each object. Since you don't provide an implementation for this method hashCode(), the default one from Object is used and each of your students will have a different hash code.
You must extend this method in your class, being aware of the equals-hashCode contract. If two objects are equal, they must have the same hashCode (the reverse isn't allways true). For further details see this Object.hashCode()

Related

How to modify variable class from HashSet or HashMap of Class

I have a HashSet and a HashMap of Class. How to modify the class variable?
so I have a class Data, then I create HashSet of Data and HashMap of Data
then do population. later, I would like to modify the value name and number.
class Data {
private String name;
private int number;
public Data(String name, int number) {
this.name = name;
this.number = number;
}
public String toString() {
return name + ":" + number;
}
public void modifyNumber (int i) {
this.number+=i;
} }
public class Main {
public static void main(String[] args) {
Set<Data> dataSet = new LinkedHashSet<Data>();
Map<String, Data> map = new LinkedHashMap<String, Data>();
// I do the dataSet and map population, then do something else
//now I want to modify the value of name and number for HashSet and HashMap of Class
}}
create the getter and setter method for Data class
then get the object and change it
public void setName(String name){
this.name=name;
}
map.get("id").setName("newname");
map.get("id").modifyNumber(number);
To work inside a HashSet, the Data class should override equals and hashcode methods.
Otherwise the set will not operate as expected.
In your implementation you failed to override those methods.
But when you do that, you cannot change the attribute values which are used in equals and hashcode. This may cause the object to be in the wrong hash bucket for its new value.
You can change the other attributes.
hashset.iterator.next().setXXX();

Java HashMap return value not confirming with my understanding of equals and hashcode

The output of the following code sample is:
{1--e=e2, 2--e1=e1}
package com.sid.practice;
import java.util.HashMap;
import java.util.Map;
public class InputOutputPractice
{
public InputOutputPractice()
{
}
public static void main(String[] args)
{
Employee e = new InputOutputPractice().new Employee(1, "e");
Employee e1 = new InputOutputPractice().new Employee(2, "e1");
Employee e2 = new InputOutputPractice().new Employee(1, "e2");
Map m = new HashMap();
m.put(e, "e");
m.put(e1, "e1");
m.put(e2, "e2");
System.out.println(m);
}
class Employee
{
public Employee(int id, String name)
{
this.id=id;
this.name = name;
}
private int id;
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
#Override
public boolean equals(Object obj)
{
return ((Employee)obj).getId()==(this.getId());
}
#Override
public int hashCode()
{
return Integer.valueOf(getId()).hashCode();
}
#Override
public String toString()
{
return this.id + "--" + this.name;
}
}
}
I do not understand how the Object e2 was able to overwrite the key in Object e, but not the value. In my understanding the output should have been:
{1--e2=e2, 2--e1=e1}
Actually, you got it backwards. The value was overridden. The key wasn't replaced since as far as HashMap is concerned, e and e2 are identical.
Your output is {1--e=e2, 2--e1=e1}:
key = e, value = "e2" (which overrode the old value "e")
key = e1, value = "e1"
The Javadocs for HashMap state for the put method:
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
Hence, the key is not overwritten, only the value.
The java.util.HashMap implementation does not replace the existing key when it is equal to the key supplied in the put() call. So, your third put() checks the existing contents of the map, finds an existing equal key and just updates the associated value.
This illustrates why equals() and hashCode() should generally take all properties into account as objects which are considered equal are considered interchangeable by many of the util classes.
The reason behind the output {1--e=e2, 2--e1=e1} is :
Map do not replaces key it only does so for value when there is match (present on the basis of key) in the existing Map.
So applies in this case:
Here e is equal to e2 for Map. When Map search for location to put m.put(e2, "e2"); it goes to location where e--"e1" is present and replaces "e1" by "e2" and leaves the key i.e e in this case intact

About sorting array list

I am trying to ensure that the objects i insert in productDatabase have not been already inserted and that i sort my arraylist using method sortData but without using any comparators in method sortData
public class Application {
public static void main(String[] args) {
Product p = new Product(15,"test",3.45);
Product p2 = new Product(15,"test",3.45);
Product p3 = new Product(4716,"koukouroukou",1.25);
Product p4 = new Product(6002,"bananofatsoula",0.60);
ProductDatabase productDatabase = new ProductDatabase();
productDatabase.addProduct(p);
productDatabase.addProduct(p2);
productDatabase.addProduct(p3);
productDatabase.addProduct(p4);
productDatabase.printDatabase();
productDatabase.sortDatabase();
productDatabase.printDatabase();
}
public class Product {
private int code;
private String name;
private double price;
public Product(int code, String name, double price){
this.code = code;
this.name = name;
this.price = price;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString(){
return code+" , description: "+name+", price: "+price;
}
public int hashCode(){
return 31 * code + name.hashCode();
}
public boolean equals(Object o){
Product other = (Product)o;
if (this.code == other.code){
return true;
}
else{
return false;
}
}
import java.util.ArrayList;
import java.util.Collection;
public class ProductDatabase {
private ArrayList<Product> productDatabase;
public ProductDatabase(){
productDatabase = new ArrayList<Product>();
}
public void addProduct(Product p){
if(!productDatabase.contains(p)){
productDatabase.add(p);
}
}
public void printDatabase(){
for(Product product : productDatabase){
System.out.println(product);
}
}
public void sortDatabase(){
// ????????????????????????????????????????????????????????????
So my questions are
Does contain(p) is enough to ensure that the same product is not already in the list?
products are the same when they have the same code and name.if not what i have to do?
How i sort my withous using comparators in class ProductDatabase.maybe by a new method in product ?
Does productDatabase extends Product???
You can return bool value in order to know the product is already there or not . Code is used to ensure the differentiation of products if not add another code id.
Product instance will carry information of just one Product so Sort must be done in the member function of class having all the records of product, not just one.
No product_database does not extend product . It is a log of product class not a part .
your questions 1 and 2 are a little unclear. Can you re-write them? As for question 3. No ProductDatabase does not extend product, neither should it. ProductDatabase HAS products. ProductDatabase is not a product
Yes, contains() is enough (in our case) to ensure uniqueness
Since you implemented equals and hashCode - you're good
You don't need to sort if you don't have another purpose to do so, but since you're using an ArrayList every time contains() is called it iterates the whole list which is not very efficient. A better implementation would use Set (a HashSet for example)
ProductDatabase does not have to extend Product - it contains a list/set of products but it doesn't have any character/behavior like Product
Yes, contain(p) is enough to ensure that the same product is not already in the list, because you overrided "equals" method.
In "equals" you can use shorter construction:
Product other = (Product)o;
return this.code == other.code;
For sort ArrayList with java.util.Collections class two options possible:
Collections.sort(List list, Comparator c). You have to write own Comparator class and pass as second parameter.
Collections.sort(List list) and class Product must implement Comparable interface
Yes, Contain(p) is enough to ensure that the same product is not already in the list BUT that is NOT efficient. Use a Set instead of ArrayList.
For question 2, you have to decide the when the two products are equal and code that in your equals method like you did for 'product code'

How to customize java hashset for checking duplication on the basis of set object instance variable value

By default set can not have a duplicate records, but suppose I have class
class Employee {
Integer emp_id;
String name;
// other fields and their getter
Employee(String name) {
emp_id++;
this.name=name;
}
}
Now I declare a set in my other class
set<Employee> empSet = new HashSet<Employee>();
Employee e1 = new Employee ("abc");
Employee e2 = new Employee ("abc");
so while insert it to the set
empSet.add(e1);
empSet.add(e2);
Then first e1 add to set but second e2 returns false.
Now what I want that there is no duplication of their name. So I want to check while insert into set.
You should override the hashcode and the equals method of Employee class.
Where you can ensure the policy of duplication on equals method by populating different hashcode for different the Objects.
Find a example of same implementation.
You can use COMPARATOR class to remove duplicates... You have to override compare method... For example code refer this page:http://java2novice.com/java-collections-and-util/treeset/duplicate-objects/
As other users mentioned, you need to override hashCode() and equals() methods of Employee class that determines if any two Employee objects are equal/same.
Below is sample implementation for your quick understanding. This would not allow you to add two Employee objects with same name into the Set.
public class Employee {
static int emp_id;
String name;
// other fields and their getter
Employee(String name) {
emp_id++;
this.name=name;
}
#Override
public int hashCode() {
int code = name.hashCode();
return code;
}
#Override
public boolean equals(Object obj) {
Employee empObj = (Employee) obj;
if(empObj.name.equalsIgnoreCase(this.name)) {
return true;
}
return false;
}
}
Hope this would be helpful.

How can I order TreeMaps or ArrayLists holding Persons based on their ID, name, or birthdate?

I have tried almost everything and I can't seem to get my lists to order themselves.
Here's some code:
private List<Person> names = new ArrayList<Person>();
private Map<Integer, Person> peopleMap = new TreeMap <Integer, Person>();
for(int i = 0; i<20; i++)
{
Person personOne = new Person();
peopleMap.put(personOne.id,personOne);
names.add(personOne);
}
Collections.sort(names);
run();
}
My Person class:
public class Person implements Comparable {
public String name;
public int id;
public Date birthdate;
static int idRecord = 0;
The values are filled with randoms. My date has a date format.
I also have a toString method inside my person class, but for some reason when I try to print my maps it gives me the hashcode (this is the hashcode right?) Person#a62fc3.
Here is my toString inside the person clasS:
public String toString()
{
char tab = '\t';
return ("ID Number: "+id+tab+" Name: "+tab+name+tab+" Birthdate: "+(birthdate.toString()));
}
I should add that I am not able to call my toString method inside my person class. Because it is printing Person#a62fc3.
public void sortByID()
{
char tab = '\t';
for (int i = 1; i<20; i++)
System.out.println((peopleMap.get(i)).toString());
//System.out.println("ID Number: "+(peopleMap.get(i).id)+tab+" Name: "+tab+peopleMap.get(i).name+tab+" Birthdate: "+peopleMap.get(i).birthdate);
run();
}
The commented code will work but the code calling the toString does not print what it should
Compare to method inside of my Person class:
public int compareTo(Object obj) {
Person o = (Person) obj;
if (this.id == o.id) { return 0; }
if (this.id > o.id) { return 1; }
if (this.id < o.id) { return -1; }
return 0;
I can provide more code if it's needed.
Compare by name method and it's output. Should I make an arrayList to store my values in and then sort it in that?
public void sortByName()
{
// char tab = '\t';
for(int j = 1; j<20; j++)
{
// System.out.println("ID Number: "+(names.get(j).id)+tab+" Name: "+tab+peopleMap.get(j).name+tab+" Birthdate: "+peopleMap.get(i).birthdate);
//Person p = names.get(j);
System.out.println(names.get(j).toString());
}
}
Output:
Person#10b30a7
Person#1a758cb
Person#1b67f74
Person#69b332
Person#173a10f
Person#530daa
Person#a62fc3
Person#89ae9e
Person#1270b73
Person#60aeb0
Person#16caf43
Person#66848c
Person#8813f2
Person#1d58aae
Person#83cc67
Person#e09713
Person#de6f34
Person#156ee8e
Person#47b480
Thanks
Well, I can't pinpoint the exact problem, I have a few suggestions.
Maps aren't sorted.
In general, an Map is not sorted, so you will not be able to sort the keys of the map. If you want to sort the Map use the SortedMap interface.
Use Generics when possible
The Comparable interface is generic. You should probably be implementing Comparable<Person>
Then your compareTo() method should look like this:
public int compareTo(Person p) {
if (this.id > p.id) return 1;
else if (this.id < p.id) return -1;
else return 0;
}
The difference between Comparator<Person> and Comparable<Person>
You need to take a look at the Comparator interface as well as the Comparable interface.
Your Person should implement comparable in that way that you usually want a person to be sorted. Then you should write some implementations of Comparator.
public classPersonNameComparator implements Comparator<Person> {
public int compare(Person p1, Person p2) {
return p1.name.compareTo(p2.name);
}
}
The importance of using the #Override annotation
It is important to always use the #Override annotation whenever you are trying to override a method of a super class or implement an interface method. The following are a few links regarding why this is a good idea:
Overriding the java equals() method quirk
When do you use Java's #Override annotation and why?
One issue that I see is that TreeMap sorts by key not by value. Your compareTo will not be used in the sorting of the tree since it is the value in the map. Since the key in the map is the id the the items in the tree should be sorted by the id of the person.
How do you know that the map isn't sorted? Can you show us some output that shows that it is not? Are you by any chance changing the ID of the Person after it gets put into the map?
Oh, and what is names compared to personMap? Also, are the ids really contiguous starting from 1? What does this code spit out:
for (Person person : peopleMap.values()) {
System.out.println(person);
}
did you use the #Override method to make sure that you are actually overriding the toString method? It looks like it is still printing out the default toString() (ie the value of the pointer to the object).
see : comparator API.
"The ordering imposed by a Comparator c on a set of elements S is said to be consistent with equals if and only if (compare((Object)e1, (Object)e2)==0) has the same boolean value as e1.equals((Object)e2) for every e1 and e2 in S."
I don't see an equals method in your Person class. The default implementation of equals compares identity. And if you override equals, you must define hashCode two.
And this question : Consistent Equals() results, but inconsistent TreeMap.containsKey() result
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class Person implements Comparable<Person> {
public final String name;
public final int id;
public final Date birthdate;
public Person(int id, String name, Date birthdate) {
this.id = id;
this.name = name;
this.birthdate = birthdate;
}
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
for (int i = 10; i > 0; i--) {
list.add(new Person(i, "name" + String.valueOf(i), new Date()));
}
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
#Override
public boolean equals(Object other) {
if (!(other instanceof Person)) {
return false;
}
return this.id == ((Person)other).id;
}
#Override
public int hashCode() {
return 41 * id;
}
#Override
public String toString() {
return "Person<" + id + ">";
}
#Override
public int compareTo(Person other) {
if (!(other instanceof Person)) {
throw new IllegalArgumentException();
}
return this.id - ((Person)other).id;
}
}
Outputs :
[Person<10>, Person<9>, Person<8>, Person<7>, Person<6>, Person<5>, Person<4>, Person<3>, Person<2>, Person<1>]
[Person<1>, Person<2>, Person<3>, Person<4>, Person<5>, Person<6>, Person<7>, Person<8>, Person<9>, Person<10>]

Categories

Resources