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> itemId
itemDetails
我用这个比较
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
刚检查:你有没有在'itemDetails'类中重写'equals'和'hashCode'? –
另外,你需要做什么?你能解释一下“在类itemDetails中排序第一对HashMap的值是ArrayList itemId”吗? –
请遵循Java编码约定:类型名称(类,接口,枚举)始终以大写字母开头。字段,变量和方法的名称始终以小写字母开头。 – RealSkeptic