简体   繁体   English

如何扩展列表中包含的对象?

[英]How to extend objects contained in a List?

I have a List named resourceItems which containes ResourceItem objects. 我有一个名为resourceItems的列表,其中包含ResourceItem对象。

    public class ResourceItem {
        private Long id;
        private String name;
        public ResourceItem(Long id, String name) {
            this.id = id;
            this.name = name;
        }
        // getters and setters...
    }

    public class SomeClass {
        private List<ResourceItem>  resourceItems = FindAllResourcesWebSerbice();
    }

I would like to extend the objects in the List to include a boolean field named selected. 我想扩展列表中的对象,以包括一个名为selected的布尔字段。

I've tried several variations of classes that extends ResourceItem (see below) including options using generics, but have not been successful. 我尝试了几种扩展ResourceItem(参见下文)的类的变体,包括使用泛型的选项,但没有成功。 I would love a solution that uses generics for reuse. 我希望使用泛型进行重用的解决方案。

    public class ExtendedResourceItem extends ResourceItem {
        private boolean selected = false;
        public ExtendedResourceItem() {
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }
    }

    public class SomeClass {
        private List<ResourceItem>  resourceItems = FindAllResourcesWebSerbice();
        private List<ExtendedResourceItem> extendedResourceItem = resourceItems;
    }

Any help is much appreciated. 任何帮助深表感谢。

Assuming ResourceItem implements equals and hashCode correctly, why not just keep a set of the ones that are selected: 假设ResourceItem正确实现equalshashCode ,为什么不保留一组选定的对象:

Set<ResourceItem> selectedResourceItems = new HashSet<ResourceItem>();

Then you could add an item to the set when it's selected, remove it when it's deselected, and check to see if the set contains one when you need to know if it's selected. 然后,您可以在选中某个项目时将其add到该集合中,而在取消选中该项目时将其remove ,并在需要知道是否选中该集合时检查该集合contains是否contains一个项目。

This solution is very similar to Michael Myers' comment about using a Map<ResourceItem, Boolean> . 此解决方案与Michael Myers关于使用Map<ResourceItem, Boolean> 的评论非常相似。

You should use composition, not inheritance here. 您应该在这里使用组合,而不是继承。 From your description it sounds like ResourceItem is a domain object, but on screen, you should wrap it 从您的描述中听起来, ResourceItem是一个域对象,但是在屏幕上,您应该包装它

class ScreenResourceItem {
    ResourceItem item;
    boolean selected;
}

Create a new list of ScreenResourceItem and put all the wrapped ResourceItem s in it. 创建一个新的ScreenResourceItem列表,并将所有包装的ResourceItem放入其中。

Generics and list covariance are not relevant here. 泛型和列表协方差在这里不相关。

But using your snippet: 但是使用您的代码段:

private List<ResourceItem> resourceItems = ... \\ some assignment;
private List<ExtendedResourceItem> extendedResourceItems = resourceItems;

is absolutely wrong. 是绝对错误的。 Since not any ResourceItem is guarantee to be also ExtendedResourceItem. 由于没有任何ResourceItem可以保证也是ExtendedResourceItem。 (resourceItems list can contain objects that are ResourceItem but are NOT ExtendedResourceItem). (resourceItems列表可以包含ResourceItem但不是ExtendedResourceItem的对象)。

I don't see the point you want to reach, but if You want to transform resourceItems into list of resourceItems that have selected attribute, what about this concept: 我看不到要达到的目标,但是如果您想将resourceItems转换为已selected属性的resourceItems列表,那么这个概念呢?

public class ExtendedResourceItem {
    private ResourceItem item = null;
    private boolean selected = false;

    public ExtendedResourceItem(ResourceItem item, boolean selected) {
        this.item = item;
        this.selected = selected;
    }

    // ... getters and setters
}

public class SomeClass {
    private List<ResourceItem> resourceItems = FindAllResourcesWebSerbice();

    private List<ExtendedResourceItem> extendedResourceItems = new ArrayList<ExtendedResourceItem>();
    for (ResourceItem item: resourceItems) {
        extendedResourceItems.add(new ExtendedResourceItem(item, false));
    }
}

Is it the generics limits that are confusing? 泛型限制令人困惑吗? Try using the Upper Bounded Wildcards , eg 尝试使用上界通配符 ,例如

public class SomeClass {
    private List<ExtendedResourceItem> extendedResourceItems = FindAllResourcesWebSerbice();
    private List<? extends ResourceItem> resourceItems = extendedResourceItems;
}

Alternatively: 或者:

public class SomeClass {
    private List<? extends ExtendedResourceItem> extendedResourceItems = FindAllResourcesWebSerbice();
    private List<? extends ResourceItem> resourceItems = extendedResourceItems;
}

More information about wildcards Wildcards and Subtyping . 有关通配符和通配符的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按包含的枚举对对象列表进行排序? - How to sort a list of objects by contained enum? Java对象列表包含在另一个对象列表中 - Java list of objects contained in another list of objects 如何获取仅包含Java中特定字段的对象列表? - How to get a list of objects contained only specific fields in Java? 过滤列表中包含的对象的处理属性的列表 - Filter a list processing attributes of the objects contained in the list 如何从对象中包含的对象列表中每个元素的字段中检索一组字符串? - How to retrieve a Set of String from the field of each element in a List of objects that is contained in an object? 在列表中查找扩展特定类的对象 - Find objects in a list that extend a certain class 如何监视ObservableList JavaFX中包含的对象的更改 - How to monitor changes on objects contained in an ObservableList JavaFX 如何在Java中扩展列表以存储其他元数据并强制其仅接受一种类型的对象? - How to extend a list in java to store additional metadata and force it to accept only one type of objects? 如何访问地图中包含的列表中的元素? - How to Access Elements in a List that is Contained in a Map? 是否可以对扩展相同基类的不同对象的列表进行包裹/取消包裹? - Is it possible to parcel/deparcel a list of different objects that extend same base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM