如何显示集合是否已添加或删除?
首先,我为发布与“4462626”类似的问题表示歉意。我想比较两个对象(这是父子关系)。下面的代码你觉得怎么样。我认为它效率不高,因为存在太多 for 循环。你能给我建议吗? (注意:我不允许修改 Item.class 并且 ItemEx.class 必须扩展 Item.class)
EXPECTED RESULT
------------------------
add:4
delete:2
------------------------
package com.javastudy;
import java.util.ArrayList;
import java.util.List;
public class CollectionCompareToObjectsForLoop {
public static void main(String[] args) {
List<Item> beforeList = new ArrayList<Item>();
List<ItemEx> afterList = new ArrayList<ItemEx>();
beforeList.add(new Item(1L));
beforeList.add(new Item(2L)); // delete
beforeList.add(new Item(3L));
afterList.add(new ItemEx(1L));
afterList.add(new ItemEx(3L));
afterList.add(new ItemEx(4L)); // added
// Check Add
List<Item> addList = new ArrayList<Item>();
for(Item afterItem : afterList){
if(checkAdd(afterItem, beforeList)){
addList.add(afterItem);
}
}
// Check Delete
List<Item> deleteList = new ArrayList<Item>();
for(Item beforeItem : beforeList){
if(checkDelete(beforeItem, afterList)){
deleteList.add(beforeItem);
}
}
// Print Result
for(Item item : addList){
System.out.println("add:" + item.getId());
}
for(Item item : deleteList){
System.out.println("delete:" + item.getId());
}
}
private static boolean checkAdd(Item afterItem, List<Item> beforeList) {
for(Item beforeItem : beforeList){
if (afterItem.getId().equals(beforeItem.getId())){
return false;
}
}
return true;
}
private static boolean checkDelete(Item beforeItem, List<ItemEx> afterList) {
for(Item afterItem : afterList){
if (beforeItem.getId().equals(afterItem.getId())){
return false;
}
}
return true;
}
}
package com.javastudy;
public class Item {
private Long id;
public Item(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
package com.javastudy;
public class ItemEx extends Item {
private String name;
public ItemEx(Long id) {
super(id);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Firstly, I apologize for posting similar question to "4462626". I want to compare to two objects (which are parent-child relationship). How do you think following code. I think it is not efficient because too many for-loops exist. Can you advice me? (notice: I am not allowed to modify Item.class and ItemEx.class must extend Item.class)
EXPECTED RESULT
------------------------
add:4
delete:2
------------------------
package com.javastudy;
import java.util.ArrayList;
import java.util.List;
public class CollectionCompareToObjectsForLoop {
public static void main(String[] args) {
List<Item> beforeList = new ArrayList<Item>();
List<ItemEx> afterList = new ArrayList<ItemEx>();
beforeList.add(new Item(1L));
beforeList.add(new Item(2L)); // delete
beforeList.add(new Item(3L));
afterList.add(new ItemEx(1L));
afterList.add(new ItemEx(3L));
afterList.add(new ItemEx(4L)); // added
// Check Add
List<Item> addList = new ArrayList<Item>();
for(Item afterItem : afterList){
if(checkAdd(afterItem, beforeList)){
addList.add(afterItem);
}
}
// Check Delete
List<Item> deleteList = new ArrayList<Item>();
for(Item beforeItem : beforeList){
if(checkDelete(beforeItem, afterList)){
deleteList.add(beforeItem);
}
}
// Print Result
for(Item item : addList){
System.out.println("add:" + item.getId());
}
for(Item item : deleteList){
System.out.println("delete:" + item.getId());
}
}
private static boolean checkAdd(Item afterItem, List<Item> beforeList) {
for(Item beforeItem : beforeList){
if (afterItem.getId().equals(beforeItem.getId())){
return false;
}
}
return true;
}
private static boolean checkDelete(Item beforeItem, List<ItemEx> afterList) {
for(Item afterItem : afterList){
if (beforeItem.getId().equals(afterItem.getId())){
return false;
}
}
return true;
}
}
package com.javastudy;
public class Item {
private Long id;
public Item(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
package com.javastudy;
public class ItemEx extends Item {
private String name;
public ItemEx(Long id) {
super(id);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您为项目提供的
Id
实际上是 ID,因此具有相同 ID 的项目被视为相等,并且每个 ID 仅有一个项目。然后您可以使用以下代码:一些备注:
LinkedHashMap
的行为类似于映射,但会记住元素插入的顺序。这是为了使输出可预测,并具有与beforeList
和afterList
中相同的顺序。Item
和ItemEx
没有方法equals(Object)
和hashCode()
,因此它们不能直接用作 HashMap 中的键。我的假设是,您认为具有相同 ID 的两个Item
是相等的。newLinkedHashMap
只是在方法computeDeleteAndAdd
中保存一些按键操作。如果没有这些方法,您将不得不使用new LinkedHashMap(...)
而不是简单的newLinkedHashMap(...)
。I assume that the
Id
s you gave to the items are really IDs, so that items having the same ID are considered equal and that there is only one item per ID. Then you can use the following code:Some remarks:
LinkedHashMap
behaves like a map but remembers the order in which the elements have been inserted. This is to make the output predictable and to have the same order as in thebeforeList
andafterList
.Item
andItemEx
don't have the methodsequals(Object)
andhashCode()
, so they cannot be used directly as keys in aHashMap
. This is my assumption that you consider twoItem
s with the same ID to be equal.newLinkedHashMap
just save some keystrokes in the methodcomputeDeleteAndAdd
. Without these methods you would have to saynew LinkedHashMap<Long, Item>(...)
instead of a simplenewLinkedHashMap(...)
.这个解决方案失去了列表的顺序......
This solution loses the order of the lists...