I have this HashMap:
HashMap< itemDetails, Pair<ArrayList<itemDetails>, AssociatedInfo>> AssociatedItemCasuaList = new HashMap<>();
where its key is class, and its values consist of pair (a= arraylist of class itemDetails , b= class AssociatedInfo ):
class itemDetails {
public ArrayList<Integer> itemId;
public float expectedSupport = 0;
// etc
}
and
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;
}
}
and
class AssociatedInfo {
public int noOfKCasual = 0;
public int AssociateListStart = 0;
public int AssociateListEnd = 0;
}
I want to sort the first pair of the the values of HashMap
which is the ArrayList<Integer> itemId
in the class itemDetails
I used this Comparator
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));
}
}
I don't know how to write 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());
Update: Example:
AssociatedItemCasuaList < **key**=LHS, **value**=Pair<a, b> >
Let key=LHS:
LHS.itemId=1
LHS.expectedSupport=87.5
and values = Pair < a, b >
Let focus here in a
only in this example.
a= ArrayList<itemDetails>
Let itemDetails RH2
and every time (in loop) I add RHS2 to key LHS as:
AssociatedItemCasuaList.get(LHS).a.add(RHS2)
here RHS2
takes different values every time
RHS2.itemId
RHS2.expectedSupport
until now, I don't have problem.
I want to sort the ArrayList in a
(that I filled with RHS2
) based on its itemId
Ignoring the fact I didn't understand a single word, what about this:
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.