有两个数组,前提是数组对象是一样的。需要比较这两个数组中对象的值是否一致?需要考虑对象的顺序。如果对象里面在嵌套一个数组怎么处理。
重写对象equals()和hashcode()方法
1.为什么要重写equals()方法?
因为object中的equals()方法比较的是对象的引用地址是否相等,当需要判断对象里的内容是否相等,则需要重写equals()方法。
2.重写equals()方法为什么要同时重写hashcode()方法?
重写equals()方法同时重写hashcode()方法,就是为了保证当两个对象通过equals()方法比较相等时,他们的hashCode值也一定要保证相等。
新增两个对象
@Data
@Slf4j
public class ProductBillingConfigCompare {
/**
* 费用类型
*/
private String costType;
/**
* 结算周期
*/
private String settlementInterval;
/**
* 计算方式
*/
private String calculateWay;
/**
* 费用比例
*/
private BigDecimal costValue;
/**
* 服务费列表
*/
private List<ProductBillingConfigExt> billingConfigExtList;
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;//地址相等
}
if(obj == null){
return false;//非空性:对于任意非空引用x,x.equals(null)应该返回false。
}
if(obj instanceof ProductBillingConfigCompare){
ProductBillingConfigCompare configCompare = (ProductBillingConfigCompare) obj;
//比较两个对象嵌套数组的对象值,不考虑排序问题
boolean listCompare = false;
List<ProductBillingConfigExt> compareList = new ArrayList<>();
//如果两个对象的数组都不为空
if(!CollectionUtils.isEmpty(this.billingConfigExtList) && !CollectionUtils.isEmpty(configCompare.billingConfigExtList)){
//当两个数组Size一样才比较对象的值是否相等,如果Size不一样直接false
if(this.billingConfigExtList.size() == configCompare.getBillingConfigExtList().size()){
for (int i = 0; i < this.billingConfigExtList.size(); i++) {
ProductBillingConfigExt configExt1 = this.billingConfigExtList.get(i);
for (int j = 0; j < configCompare.billingConfigExtList.size() ; j++) {
ProductBillingConfigExt configExt2 = this.billingConfigExtList.get(j);
if(configExt1.equals(configExt2)){
compareList.add(configExt1);
break;
}
}
}
}
//如果比较结果数组和当前对象的数组Size一样则两个数组值一直
if(this.billingConfigExtList.size() == compareList.size()){
listCompare = true;
}
}else if(CollectionUtils.isEmpty(this.billingConfigExtList) && CollectionUtils.isEmpty(configCompare.billingConfigExtList)){
//两个对象数组都为空则对象嵌套的数组相同
listCompare = true;
}
//需要比较的字段相等,则这两个对象相等
if(this.costType.equals(configCompare.getCostType())
&& this.settlementInterval.equals(configCompare.getSettlementInterval())
&& this.calculateWay.equals(configCompare.getCalculateWay())
&& this.costValue.equals(configCompare.getCostValue())
&& listCompare){
return true;
}
}
return false;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + (costType == null ? 0 : costType.hashCode());
result = 31 * result + (settlementInterval == null ? 0 : settlementInterval.hashCode());
result = 31 * result + (calculateWay == null ? 0 : calculateWay.hashCode());
result = 31 * result + (costValue == null ? 0 : costValue.hashCode());
result = 31 * result + (billingConfigExtList == null ? 0 : billingConfigExtList.hashCode());
return result;
}
}
@Data
public class ProductBillingConfigExt {
/**
* 服务费范围开始
*/
private BigDecimal serviceFeeStart;
/**
* 服务费范围结束
*/
private BigDecimal serviceFeeEnd;
/**
* 比例/固定值
*/
private BigDecimal serviceFee;
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;//地址相等
}
if(obj == null){
return false;//非空性:对于任意非空引用x,x.equals(null)应该返回false。
}
if(obj instanceof ProductBillingConfigCompare){
ProductBillingConfigExt configExt = (ProductBillingConfigExt) obj;
//需要比较的字段相等,则这两个对象相等
if(this.serviceFeeStart.equals(configExt.getServiceFeeStart())
&& this.serviceFeeEnd.equals(configExt.getServiceFeeEnd())
&& this.serviceFee.equals(configExt.getServiceFee())){
return true;
}
}
return false;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + (serviceFeeStart == null ? 0 : serviceFeeStart.hashCode());
result = 31 * result + (serviceFeeEnd == null ? 0 : serviceFeeEnd.hashCode());
result = 31 * result + (serviceFee == null ? 0 : serviceFee.hashCode());
return result;
}
}
在写一个比较方法
public static boolean isListEqual(List<ProductBillingConfigCompare> list1, List<ProductBillingConfigCompare> list2) {
// 两个list引用相同(包括两者都为空指针的情况)
if (list1 == list2) {
return true;
}
// 两个list都为空(包括空指针、元素个数为0)
if ((list1 == null && list2 != null && list2.size() == 0)
|| (list2 == null && list1 != null && list1.size() == 0)) {
return true;
}
// 两个list元素个数不相同
if (list1.size() != list2.size()) {
return false;
}
// 两个list元素个数已经相同,再比较两者内容
// 采用这种可以忽略list中的元素的顺序
// 涉及到对象的比较是否相同时,确保实现了equals()方法
if (!list1.containsAll(list2)) {
return false;
}
return true;
}
调用
boolean result = isListEqual(list1,list2);
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R