简体   繁体   中英

How can I assign values in pairs?

I want to assign values and make it as a pair.

For example OrderID and ItemID

Items are in list. What should I use to look like this (OrderID,ItemID) where OrderID should be the same for all ItemID in list.

All examples and samples are welcome.

You could use a map and store key value-pairs if itemID is unique
Map<Long,Long> pairMap=new HashMap<Long,Long>(); and to add things in it pairMap.put(ItemID,OrderID) .

You can either create a new object or you can store them in a little Object array. If they have a more common ancestor from which they inherit you can instead make that type of array. You can also give one type of the two a field that holds the other (if you have a one to many relation between them for example). In your case you could consider giving orderID a field for itemID and storing the related item in there. It al depends on what you want to do with it though.

A multimap could be a good construct: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multimap.html

Multimap<O, I> orders = ArrayListMultimap.create();

Replace O and I with the types for orders and items. The structure will hold an array list of Is for each O.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM