简体   繁体   English

如何在其中具有对象的java中比较两个地图

[英]How to compare two maps in java which is having the objects in it

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. 在这种情况下,如何比较两个具有某些值的地图对象。如果这些地图包含大量数据,但id是唯一的,则有两个类,第一个是getter_setter,它在地图对象中具有数据,而第二个类是是getter_setter2,它的全部数据都在map_getter_Setter中。 I need a comparison on the basis of id and then whole data is going to be compared. 我需要根据id进行比较,然后将要比较整个数据。 How can i do this? 我怎样才能做到这一点?

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. 如果我正确理解了您的问题,则主要问题是id和data之类的字段位于hashmap类内,因此进行比较时不太容易。

You can write custom comparator class, which first compares on id and then data inside map by iteration and finally gives out equal. 您可以编写自定义比较器类,该类首先在id上进行比较,然后通过迭代比较map中的数据,最后给出相等值。 for this refer this answer enter link description here 为此,请参考此答案, 在此处输入链接描述

The other option is to use comparison chain. 另一种选择是使用比较链。 enter link description here which will help you to use multiple comparisons easily. 在此处输入链接说明,这将帮助您轻松使用多个比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM