2015-08-28 33 views
0

的价值观我有此HashMap:的Java:如何使用ArrayList中使用比较器作为HashMap中

HashMap< itemDetails, Pair<ArrayList<itemDetails>, AssociatedInfo>> AssociatedItemCasuaList = new HashMap<>(); 

其中的关键是类,它的值由对(A =类的ArrayList itemDetails,B =类AssociatedInfo):

class itemDetails { 
    public ArrayList<Integer> itemId; 
    public float expectedSupport = 0; 
    // etc 
} 

class Pair<T, U> { 

     T a; 
     U b; 

     Pair(T a, U b) { 
      this.a = a; 
      this.b = b; 
     } 

     T getA() { 
      return a; 
     } 

     U getB() { 
      return b; 
     } 
    } 

class AssociatedInfo { 
    public int noOfKCasual = 0; 
    public int AssociateListStart = 0; 
    public int AssociateListEnd = 0; 
} 

我要排序的第一对HashMap 的值中的哪一个类中的ArrayList<Integer> itemIditemDetails

我用这个比较

public class ItemComparator implements Comparator<ArrayList<Integer> >{ 
    @Override 
    public int compare(final ArrayList<Integer> entry1, final ArrayList<Integer> entry2){ 
     if (entry1 == null && entry2 == null) 
      return 0; 
     if (entry1 == null) 
      return 1; 
     if (entry2 == null) 
      return -1; 
     if (entry1.isEmpty() && entry2.isEmpty()) 
      return 0; 
     if (entry1.isEmpty()) 
      return 1; 
     if (entry2.isEmpty()) 
      return -1; 
     return entry1.get(0).compareTo(entry2.get(0)); 
    } 
    } 

我不t知道如何写Collections.sort

if (AssociatedItemCasuaList.containsKey(LHS)) { 
    AssociatedItemCasuaList.get(LHS).a.add(RHS2); 
    AssociatedItemCasuaList.get(LHS).b.AssociateListStart = 0; 
    AssociatedItemCasuaList.get(LHS).b.AssociateListEnd += 1; 
    AssociatedItemCasuaList.get(LHS).b.noOfKCasual += 1; 
} else { 
    ArrayList<itemDetails> ArrayListRHS = new ArrayList<itemDetails>(); 
    ArrayListRHS.add(RHS2); 
    AssociatedInfo AttribAssociatedInfo1 = new AssociatedInfo(); 
    AttribAssociatedInfo1.noOfKCasual = 1; 
    AttribAssociatedInfo1.AssociateListStart = 0; 
    AttribAssociatedInfo1.AssociateListEnd = 0; 
    AssociatedItemCasuaList.put(LHS, new Pair(ArrayListRHS, AttribAssociatedInfo1)); 
} 
//  Collections.sort(AssociatedItemCasuaList.get(LHS), new ItemComparator()); 

更新: 示例:

AssociatedItemCasuaList < **key**=LHS, **value**=Pair<a, b> > 

让键= LHS:

LHS.itemId=1 
LHS.expectedSupport=87.5 

和值=配对< A,B>

让这里重点在a只在th就是例子。

a= ArrayList<itemDetails> 

itemDetails RH2

和每次(环路),我想补充RHS2加键LHS为:

AssociatedItemCasuaList.get(LHS).a.add(RHS2) 

这里RHS2每次取不同值

RHS2.itemId 
    RHS2.expectedSupport 

到现在为止,我没有问题。

我想基于其的itemId

+0

刚检查:你有没有在'itemDetails'类中重写'equals'和'hashCode'? –

+0

另外,你需要做什么?你能解释一下“在类itemDetails中排序第一对HashMap的值是ArrayList itemId”吗? –

+2

请遵循Java编码约定:类型名称(类,接口,枚举)始终以大写字母开头。字段,变量和方法的名称始终以小写字母开头。 – RealSkeptic

回答

0

忽略了一个事实我不明白一个字排序中a ArrayList中(即我充满RHS2),你看这个:

public class ItemComparator2 implements Comparator<itemDetails> { 
    Comparator myComp = new ItemComparator(); 
    @Override 
    public int compare(itemDetails a, itemDetails b){ 
    return myComp.compare(a.itemId, b.itemId); 
    } 
} 

// ... 

Collections.sort(AssociatedItemCasuaList.get(LHS).getA(), new ItemComparator2()); 
相关问题