我需要将 listB 的所有元素合并到另一个列表 listA 中。
如果 listA 中已经存在某个元素(基于自定义相等性检查),我不想添加它。
我不想使用 Set,也不想覆盖 equals() 和 hashCode()。
原因是,我不想防止 listA 本身出现重复,我只想在 listA 中已经存在我认为相等的元素时不从 listB 合并。
我不想覆盖 equals() 和 hashCode(),因为这意味着我需要确保我对元素的 equals() 实现在任何情况下都适用。然而,listB 中的元素可能未完全初始化,即它们可能缺少对象 ID,而该对象 ID 可能存在于 listA 的元素中。
我目前的方法涉及一个接口(interface)和一个实用函数:
public interface HasEqualityFunction<T> {
public boolean hasEqualData(T other);
}
public class AppleVariety implements HasEqualityFunction<AppleVariety> {
private String manufacturerName;
private String varietyName;
@Override
public boolean hasEqualData(AppleVariety other) {
return (this.manufacturerName.equals(other.getManufacturerName())
&& this.varietyName.equals(other.getVarietyName()));
}
// ... getter-Methods here
}
public class CollectionUtils {
public static <T extends HasEqualityFunction> void merge(
List<T> listA,
List<T> listB) {
if (listB.isEmpty()) {
return;
}
Predicate<T> exists
= (T x) -> {
return listA.stream().noneMatch(
x::hasEqualData);
};
listA.addAll(listB.stream()
.filter(exists)
.collect(Collectors.toList())
);
}
}
然后我会像这样使用它:
...
List<AppleVariety> appleVarietiesFromOnePlace = ... init here with some elements
List<AppleVariety> appleVarietiesFromAnotherPlace = ... init here with some elements
CollectionUtils.merge(appleVarietiesFromOnePlace, appleVarietiesFromAnotherPlace);
...
在 listA 中获取我的新列表,所有元素都从 B 中合并。
这是一个好方法吗?是否有更好/更简单的方法来完成同样的任务?
最佳答案
你想要这样的东西:
public static <T> void merge(List<T> listA, List<T> listB, BiPredicate<T, T> areEqual) {
listA.addAll(listB.stream()
.filter(t -> listA.stream().noneMatch(u -> areEqual.test(t, u)))
.collect(Collectors.toList())
);
}
您不需要 HasEqualityFunction 接口(interface)。您可以重用 BiPredicate 来测试这两个对象在您的逻辑中是否相等。
此代码根据给定的谓词仅过滤 listB 中不包含在 listA 中的元素。它遍历 listA 的次数与 listB 中的元素数一样多。
另一种性能更好的实现方式是使用一个包装器类来包装您的元素,并将谓词作为 equals 方法:
public static <T> void merge(List<T> listA, List<T> listB, BiPredicate<T, T> areEqual, ToIntFunction<T> hashFunction) {
class Wrapper {
final T wrapped;
Wrapper(T wrapped) {
this.wrapped = wrapped;
}
@Override
public boolean equals(Object obj) {
return areEqual.test(wrapped, ((Wrapper) obj).wrapped);
}
@Override
public int hashCode() {
return hashFunction.applyAsInt(wrapped);
}
}
Set<Wrapper> wrapSet = listA.stream().map(Wrapper::new).collect(Collectors.toSet());
listA.addAll(listB.stream()
.filter(t -> !wrapSet.contains(new Wrapper(t)))
.collect(Collectors.toList())
);
}
这首先将每个元素包装在一个 Wrapper 对象中,并将它们收集到一个 Set 中。然后,它过滤 listB 中不包含在该集合中的元素。相等性测试是通过委托(delegate)给定的谓词来完成的。约束是我们还需要提供一个 hashFunction 来正确实现 hashCode。
示例代码为:
List<String> listA = new ArrayList<>(Arrays.asList("foo", "bar", "test"));
List<String> listB = new ArrayList<>(Arrays.asList("toto", "foobar"));
CollectionUtils.merge(listA, listB, (s1, s2) -> s1.length() == s2.length(), String::length);
System.out.println(listA);
关于java 8 如果不存在,则将 List 的所有元素合并到 List 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36198025/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
如果我使用ruby版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
查看我的Ruby代码:h=Hash.new([])h[0]=:word1h[1]=h[1]输出是:Hash={0=>:word1,1=>[:word2,:word3],2=>[:word2,:word3]}我希望有Hash={0=>:word1,1=>[:word2],2=>[:word3]}为什么要附加第二个哈希元素(数组)?如何将新数组元素附加到第三个哈希元素? 最佳答案 如果您提供单个值作为Hash.new的参数(例如Hash.new([]),完全相同的对象将用作每个缺失键的默认值。这就是您所拥有的,那是你不想要的。您可以改用
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
我的模型有defself.empty_building//stuffend我怎样才能对这个现有的进行rspec?,已经尝试过:describe"empty_building"dosubject{Building.new}it{shouldrespond_to:empty_building}endbutgetting:Failure/Error:it{shouldrespond_to:empty_building}expected#torespondto:empty_building 最佳答案 你有一个类方法self.empty_bu
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht