There is two classes Auction and Lot. How to store data: Client (Thread) and Bid (int) in a such way that this data structure can be easily searched by Bid(lot.Max) and returned relevant Client object?
public class Lot {
public HashMap<ClientThread, Integer> mapBids= new HashMap<ClientThread, Integer>();
}
public class Auction {
List<Lot> lots = new ArrayList<Lot>();
for(Lot lot: lots){
int lastBid = lot.BiggestBid;
...
// How to get Client object which has "lot.BiggestBid" ?
System.out.println(
lot.mapBids.get(lot.BiggestBid).someClientThreadMethod(args)); // wrong
}
}
Probably I need in Lot a data structure which will hold pair of Client and BiggestBid, and can be return the Client who have BiggestBid…
The Client and/or BiggestBid can be the same for some entities.
Maybe two parallel arrays will work.
If the bids are unique – use TreeMap. Otherwise, use SortedList. Both Map and List are using natural order if no comparator specified during creation. The biggest and the smallest key retrieval is very efficient O(1). In case you need thread safety – use SynchronizedCollection,