2016-12-23 2 views
0

Мне нужно создать HashMap<Integer,List<StoreItem>> с помощью ArrayList. Я хочу HashSet на базе магазина Количество, как мы делаем в Иос как myArrayList.distinctUnionOfObjects.storeNumberСоздать HashSet на основе атрибута класса

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. Мне нужен способ, который создать HashSet на основе StoreItem.getStoreNumber()

где 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; 
    } 
} 

или есть способ переопределить HashSet сравнения.

И я знаю, что смогу это сделать с помощью чек в For Loop.

+0

Я имею в виду два варианта: (1) Используйте 'TreeSet' и пользовательского компаратор, а не' HashSet' (2) Если вы настаиваете на 'HashSet', сделайте его' HashSet ', где' MyList' расширяет 'ArrayList' с соответствующими' equals() 'и' hashCode() '. –

ответ

2

Я не думаю, что вы можете достичь этого с помощью компаратора. Вы можете изменить метод 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; 
} 
+0

Я заполнил его на последней строке цикла for. – salix

+0

да, вы правы. Спасибо, я получил это сейчас. – Nepster

+0

приветствую :) – salix