Java 使用聚合操作将对象集合复制到另一个集合而不复制副本
我有一个名为Java 使用聚合操作将对象集合复制到另一个集合而不复制副本,java,collections,Java,Collections,我有一个名为items的项目列表,正在使用以下方法将此列表复制到集合: List<Item> items = new ArrayList<>(); items.add(new Item("Suremen Body Spray", 1900, 45)); items.add(new Item("HP wireless mouse", 5500, 5)); items.add(new Item("UWS USB memory stick", 190
items
的项目列表
,正在使用以下方法将此列表复制到集合
:
List<Item> items = new ArrayList<>();
items.add(new Item("Suremen Body Spray", 1900, 45));
items.add(new Item("HP wireless mouse", 5500, 5));
items.add(new Item("UWS USB memory stick", 1900, 8));
items.add(new Item("MTN modem", 1900, 45));
items.add(new Item("MTN modem", 1900, 45));
Collection<Item> noDups = new LinkedHashSet<Item>(items); //Copy items to noDups
//Print the new collection
noDups.stream()
.forEach(System.out::println);
List items=new ArrayList();
增加(新项目(“Suremen身体喷雾”,1900,45));
添加(新项目(“HP无线鼠标”,5500,5));
添加(新项目(“UWS USB记忆棒”,1900,8));
添加(新项目(“MTN调制解调器”,1900,45));
添加(新项目(“MTN调制解调器”,1900,45));
集合节点=新的LinkedHashSet(项目)//将项目复制到节点
//打印新收藏
noDups.stream()
.forEach(System.out::println);
当我运行代码时,所有项目都被复制到集合中,如输出所示
仅使用字符串的不同测试工作正常:
List<String> names = new ArrayList<>();
names.add("Eugene Ciurana");
names.add("Solid Snake");
names.add("Optimus Prime");
names.add("Cristiano Ronaldo");
names.add("Cristiano Ronaldo");
Collection<String> itemCollection = new HashSet<String>(names);
itemCollection.stream()
.forEach(System.out::println);
List name=new ArrayList();
名称。添加(“尤金·西乌拉纳”);
名称。添加(“实心蛇”);
名称。添加(“擎天柱”);
姓名。加上(“克里斯蒂亚诺·罗纳尔多”);
姓名。加上(“克里斯蒂亚诺·罗纳尔多”);
Collection itemCollection=新哈希集(名称);
itemCollection.stream()
.forEach(System.out::println);
我可以使用什么方法将列表复制到集合中而不复制副本?是否有任何聚合操作,或者我必须编写自定义方法?您需要在
项
类中实现and方法。您需要在项
类中实现and方法。我只是想添加一个答案来说明我最终是如何做到的(当然使用Adam的建议)
我将equals
和hashCode
方法的实现添加到我的Item类中:
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Item)) {
return false;
}
if(obj == this) {
return true;
}
Item other = (Item)obj;
if(this.getName().equals(other.getName())
&& this.getPrice() == other.getPrice()
&& this.getCountryOfProduction().equals(other.countryOfProduction)) {
return true;
} else {
return false;
}
}
public int hashCode() {
int hash = 3;
hash = 7 * hash + this.getName().hashCode();
hash = 7 * hash + this.getCountryOfProduction().hashCode();
hash = 7 * hash + Double.valueOf(this.getPrice()).hashCode();
return hash;
}
我只是想添加一个答案来说明我最终是如何做到的(当然是使用亚当的建议) 我将
equals
和hashCode
方法的实现添加到我的Item类中:
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Item)) {
return false;
}
if(obj == this) {
return true;
}
Item other = (Item)obj;
if(this.getName().equals(other.getName())
&& this.getPrice() == other.getPrice()
&& this.getCountryOfProduction().equals(other.countryOfProduction)) {
return true;
} else {
return false;
}
}
public int hashCode() {
int hash = 3;
hash = 7 * hash + this.getName().hashCode();
hash = 7 * hash + this.getCountryOfProduction().hashCode();
hash = 7 * hash + Double.valueOf(this.getPrice()).hashCode();
return hash;
}
不要太确定哈希代码不是必需的。合同要求使用自定义的
等于
的自定义哈希代码,如果合同被破坏,哈希集
可能会丢失重复项。不要确定哈希代码不是强制性的。契约要求使用自定义的equals
自定义哈希代码,如果契约被破坏,HashSet
可能会丢失重复的哈希代码。您可以使用Objects.hash()
和Objects.equals()
(自Java 7起)简化/缩短上述代码或者Guava's或Apache Commons Lang's,您可以使用Objects.hash()
和Objects.equals()
(自Java 7以来)或Guava's或Apache Commons Lang's和