0
import java.util.*;
class Getter_Setter
{
    int id;
    String name;
    public List<Getter_Setter> buckets;
    public String getName()
    {
        return name;
    }
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id=id;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    public void setGetter_Setter(List<Getter_Setter> buck)
    {
        this.buckets=buck;
    }
}
class Getter_Setter2
{
    int id;
    String name;
    public List<Getter_Setter> buckets;
    public String getName()
    {
        return name;
    }
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id=id;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    public void setGetter_Setter2(List<Getter_Setter> buck)
    {
        this.buckets=buck;
    }
}
class Simple
{
    HashMap<String,Getter_Setter> map=new HashMap<String,Getter_Setter>();
    HashMap<String,Getter_Setter2> map_getter_setter2=new HashMap<String,Getter_Setter2>();
    public static void main(String arg[])
    {
        List<Getter_Setter> temp=sum();
        List<Getter_Setter2>temp=sum1();
    }
    public static List<Getter_Setter> sum()
    {
        List<Getter_Setter> list=new ArrayList<Getter_Setter>();
        Getter_Setter get=new Getter_Setter();
        get.setId(30);
        get.setName("Hanish");
        System.out.println(get.getId());
        System.out.println(get.getName());
        list.add(get);
        map.put(get.getId(),get);
        return list;
    }
    public static List<Getter_Setter> sum1()
    {
        List<Getter_Setter2> list=new ArrayList<Getter_Setter2>();
        Getter_Setter2 get=new Getter_Setter2();
        get.setId(301);
        get.setName("Hanish1");
        System.out.println(get.getId());
        System.out.println(get.getName());
        list.add(get);
        map_getter_setter2.put(get.getId(),get);
        return list;
    }
}

how to compare the two map objects which is having some values in this case.if these mapss conatins huge data but id's are unique.There is two classes first one is getter_setter which is having the data in the map object and in the second class which is getter_setter2 which is having the whole data in the map_getter_Setter. I need a comparison on the basis of id and then whole data is going to be compared. How can i do this?

Shuchita
  • 13
  • 1
  • 6
  • @Ruchira Gayan Ranaweera The link which you have pasted in (This question already has an answer here) is wrong . The link contains only about the MAP having value as String. In this case the value will be the User Object.. Is your link is correct??? The answer is, Hashcode and Equals method of your Getter_Setter class should be override for uniqueness calculation then you need to follow the solution given in the above link.. Refer this [link](http://stackoverflow.com/questions/6871929/objects-as-map-keys-without-hashcode-and-equals) – Jaya Ananthram Apr 27 '15 at 04:40
  • it is not a duplicate question. The link which is having answer is actually comparing the Strings this phenomenon is talking about Objects. – Shuchita Apr 27 '15 at 04:43
  • Refer my comments above.. – Jaya Ananthram Apr 27 '15 at 04:44
  • so anybody is having the answer? – Shuchita Apr 27 '15 at 04:48
  • Overirde your hashcode and equals method of your Getter_Setter and Getter_Setter2 class for your uniqueness object logic and call equals method of your Map.. – Jaya Ananthram Apr 27 '15 at 04:51

1 Answers1

0

If I understand your question correctly, The main problem is having the fields like id and data are inside hashmap classes and thus making it somewhat not so straightforward to compare.

You can write custom comparator class, which first compares on id and then data inside map by iteration and finally gives out equal. for this refer this answerenter link description here

The other option is to use comparison chain.enter link description here which will help you to use multiple comparisons easily.

Community
  • 1
  • 1
Yantraguru
  • 3,604
  • 3
  • 18
  • 21