java - 根据Class Attribute创建HashSet

标签 java android hashset

I have to create HashMap<Integer,List<StoreItem>> using an ArrayList . I want HashSet on the basis of store Number as we do in Ios like myArrayList.distinctUnionOfObjects.storeNumber

public static List<StoreItem> getStoreList(){
        List<StoreItem> items = new ArrayList<>();

        items.add(new StoreItem(503,"Brufeen",10));
        items.add(new StoreItem(503,"hydriline",8));
        items.add(new StoreItem(503,"Brufeen",10));
        items.add(new StoreItem(503,"capsule",2));
        items.add(new StoreItem(503,"Disprin",6));


        items.add(new StoreItem(504,"Pixlar",9));
        items.add(new StoreItem(504,"Luprin",17));
        items.add(new StoreItem(504,"BOOM BOOM",14));
        items.add(new StoreItem(504,"Glucophage",22));
        items.add(new StoreItem(504,"Panadol",16));


        items.add(new StoreItem(549,"Pixlar",9));
        items.add(new StoreItem(549,"Luprin",17));
        items.add(new StoreItem(549,"BOOM BOOM",14));
        items.add(new StoreItem(549,"Glucophage",22));
        items.add(new StoreItem(549,"Panadol",16));

        return items;


    }

    public static HashMap<Integer,List<StoreItem>> getStoreHashMap(){
        List<StoreItem> oldStores = StoreFactory.getStoreList();

        Set<StoreItem> uniqueSet = new HashSet<StoreItem>(oldStores);

        HashMap<Integer,List<StoreItem>> dictionaryOfStore = new HashMap<>();

        for(StoreItem keyItem : uniqueSet )
        {
            List<StoreItem> storeInSection = new ArrayList<>();
            for(StoreItem oldItem : oldStores)
            {
                if(keyItem.getStoreNumber() == oldItem.getStoreNumber()){
                    storeInSection.add(oldItem);
                }
            }
            dictionaryOfStore.put(keyItem.getStoreNumber(),storeInSection);
        }

        return dictionaryOfStore;
    }

uniqueSet 返回 15,因为它比较整个 StoreItem 对象。我需要一个基于 StoreItem.getStoreNumber() 创建 HashSet 的方法

其中 StoreItem

public class StoreItem {

    private int storeNumber;
    private String capsuleNames;
    private int quantity;

    public StoreItem(int storeNumber, String capsuleNames, int quantity) {
        this.storeNumber = storeNumber;
        this.capsuleNames = capsuleNames;
        this.quantity = quantity;
    }
}

或者有没有办法覆盖哈希集比较。

我知道我可以通过 For 循环中的检查来做到这一点。

最佳答案

我认为使用比较器无法实现这一目标。您可以像这样更改 getStorehashMap() 方法:

public static HashMap<Integer, List<StoreItem>> getStoreHashMap() {
    List<StoreItem> oldStores = getStoreList();
    HashMap<Integer, List<StoreItem>> dictionaryOfStore = new HashMap<Integer, List<StoreItem>>();

    for (StoreItem keyItem : oldStores) {
        if(!dictionaryOfStore.containsKey(keyItem.getStoreNumber())) {
            dictionaryOfStore.put(keyItem.getStoreNumber(), new ArrayList<StoreItem>());
        }
        dictionaryOfStore.get(keyItem.getStoreNumber()).add(keyItem);
    }

    return dictionaryOfStore;
}

关于java - 根据Class Attribute创建HashSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41300494/

相关文章:

java - IntelliJ 14 中的 Maven 命令行参数。

java - HBase 扫描时间范围在 Scala 中不起作用

java - HashSet 值计算错误

java - HashSet 存储相等的对象

java - 从 HashSet 中的 md5 列表中删除文件

java - 如何使用JSP从URL中获取参数

java - Spring Autowiring 不起作用

android - 从适用于 Android 的 Google 登录迁移到 Firebase 身份验证

android - 通话或短信电话位置

android - 将自定义类的数组列表转换为 JSONArray