【问题标题】:JavaBeans ComparisonJavaBeans 比较
【发布时间】:2010-10-01 00:39:42
【问题描述】:

有谁知道一个免费的开源库(实用程序类),它允许您比较一个 Java bean 的两个实例并返回这两个实例中值不同的属性列表/数组?请发布一个小样本。

干杯
托马斯

【问题讨论】:

标签: java compare javabeans


【解决方案1】:

BeanComparator 的 Apache commons 就是你要找的。​​p>

更新。一个将 JavaBeans 与一个属性进行比较的简单示例(仅针对一个属性进行比较,您应该创建与要匹配的属性一样多的 BeanComparators)。

import org.apache.commons.beanutils.BeanComparator;

public class TestBeanComparator
{
    public TestBeanComparator()
    {
    }

    public class TestBean
    {
        int value;

        public TestBean()
        {
        }

        public int getValue()
        {
            return value;
        }

        public void setValue(int value)
        {
            this.value = value;
        }
    }

    public static void main(String[] args)
    {
        TestBeanComparator tbc = new TestBeanComparator();

        tbc.go();
    }

    public void go()
    {
        TestBean tbs [] = new TestBean[10];

        for (int i = 0; i < tbs.length; i++)
        {
            tbs[i] = new TestBeanComparator.TestBean();
            tbs[i].setValue((int) (Math.random() * 10));

            System.out.println("TestBean["+i+"] = " + tbs[i].getValue());
        }

        BeanComparator bc = new BeanComparator("value");

        System.out.println("");
        System.out.println("Value to match: " + tbs[0].getValue());
        for (int i = 1; i < tbs.length; i++)
        {
            if(bc.compare(tbs[i], tbs[0]) == 0)
            {
               System.out.println("Match found in bean "+ i); 
            }
        }
    }
}

经过一些测试,一个结果是成功的。这是输出:

TestBean[0] = 0
TestBean[1] = 4
TestBean[2] = 0
TestBean[3] = 2
TestBean[4] = 7
TestBean[5] = 3
TestBean[6] = 0
TestBean[7] = 3
TestBean[8] = 7
TestBean[9] = 3

Value to match: 0
Match found in bean 2
Match found in bean 6

显然增加 TestBean 数组大小会增加获得匹配的机会。

您需要将以下 jar 导入到您的项目中:commons-logging-version.jar、commons-beanutils-version.jar、commons-beanutils-core- version.jar、commons-beanutils-bean-collections-version.jar、commons-collections-version.jar。

这些文件包含在commons-loggingcommons-beanutilscommons-collections API 中。

【讨论】:

  • 似乎没有回答这个问题。 BeanComparator 通过单个属性比较两个 bean;问题是关于一次比较多个字段。
【解决方案2】:

BeanComparator 可用于collection sorting
这是一个Comparator 实现,它根据共享属性值比较bean。

BeanComparators 是可以作用于任何 Java Bean 的通用比较器。通过良好的底层 bean 反射系统,它们可以处理具有 bean 属性、数组、集合和映射的 bean。

如果我们有一个java.util.List 类型为Person 的Java Bean — 其中Person 的年龄是Integer,名称是字符串,国家是Country bean(它本身有一个字符串名称) — 我们可以使用BeanComparator 对它进行排序

Some examples:

List people = ...; // list of Person objects

//sort by age
BeanComparator comp = new BeanComparator("age");
Collections.sort(list, comp);

//sort by name
BeanComparator comp = new BeanComparator("name");
Collections.sort(list, comp);

//sort by country name
BeanComparator comp = new BeanComparator("country.name");
Collections.sort(list, comp);

没有必要写一个带有很多属性选项的PersonComparator 来排序。相反,一个 BeanComparator 类会处理所有事情。


你会发现full example here,有以下场景:

如果您正在实现一个比较器来动态比较属性(例如,考虑根据客户选择的列对网页上的表中的行进行排序),那么您可以推迟构建比较器,直到您知道哪个属性具有被选中进行排序。

这就是BeanComparator 真正闪耀的地方。当您使用 BeanComparator 时,您通常为实现此行为而编写的大量代码会减少到几行。

【讨论】:

  • 似乎没有回答这个问题。 BeanComparator 通过单个属性比较两个 bean;问题是关于一次比较多个字段。
  • 如果我有两个以上包含 Name,Age,Country Name 的 bean,我将如何将它与 BeanComparater 进行比较? Bcoz, BeanComparater 多用于比较两个java bean...是否可以比较超过2个java bean的属性...
  • @samkitshah 不确定:过去 5 年我没有使用过它(因为这个答案是在 2009 年写的)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2013-12-29
  • 1970-01-01
  • 2010-12-16
相关资源
最近更新 更多