简体   繁体   中英

Java hashmap with Multiple values

I Know its been asked a hundred times, and the answer is always the same, You Can not use multiple repeating values in a hashmap. But lets get to the problem. I have an import file, the import file has information around the lines of a CustomerID, a ProductID, and Units sold (its a basic Receipt format). What I want to do is take the import, put it into a map, and be able to reference it.

Map<integer,DoubleSales> hashmap = new HashMap <integer,DoubleSales>
 try {
Scanner dataFile = new Scanner 9new File ("./salesData.csv"));
dataFile.nextLine();
while(dataFile.hasNextLine()){
String[] lineValues = line.split (",");
Integer CustomerID = Integer.parseInt(lineValues[0]);
Integer ProductID = Integer.parseInt(lineValues[1]);
integer Units = Integer.parseInt(lineValues[2]);
DoubleSales sales = new DoubleSales(CustomerID,ProductID,Units);
ProductData.put(CustomerID,sales);
}

class DoubleSales{
int CustomerID;
int ProductID;
int Units;

DoubleSales(int custID, int prodID, int Units){
CustomerID = custID;
ProductID = prodID;
Units = units;
}
}

The import file has data in the format of

CustomerID, ProductID, UnitsSold
1,10002,3
1,10004,5
1,10008,2
1,10010,3
1,10010,3

Using the code up there, When I print the customerID value of 1, I get just the last entry which is 10010,3. How would I do it to print out, all values of CustomerID 1, and the Units sold?

for example:
1,10002,3
  10004,5
  10008,2
  10010,3
  10010,3 
(will add the two 10010 values later.)

I do not wish to Use array lists.

Try MultiValueMap from Apache Common Collections.

Click here for more reference

In your case, a simple Map won't do your favor, everything you write to the value of a specified customer will be overridden, if you want to retain all entries while keeping them easily referenced, try:

First, create a structured map

Map<Integer,List<DoubleSales>> productData = new HashMap<Integer,List<DoubleSales>>();

Second, add products like this

List<DoubleSales> entries;
if(productData.get(CustomerID) == null) {
    entries = new ArrayList<DoubleSales>();
    entries.add(sales);
    productData.put(CustomerID, entries);
} else {
    List<DoubleSales> entries = productData.get(CustomerID);
    entries.add(sales);
}

Third, review your products list that you just added

List<DoubleSales> products = productData.get(CustomerID);
if (products != null) {
    for(DoubleSales product : products) {
        // access your product here.
    }
}

You have duplicated CustomerID (all having 1 as id) and you using that as a key in Hashmap. That is the reason it is keep ovverding when you insert a new record with the same id. Looks like your product id is unique. Try that or have unique customer id.

I think in that case it is better to implement the structure with a matrix. It could be done easily with arrays (or lists), where rows could contain a bean formed by the product id and the units sold, being indexed by the customer id

My first idea was Jerry Chin's solution, but I would like to show you a second approach, just to demonstrate that there are multiple solutions to the same problem.

You could store your values in a TreeSet<DoubleSales> . This would not limit the entries, you can enter for example 1,10010,3 as many times as you want.

Then, define an ordering ( Comparator ) on the DoubleSales , to group the orders by CustomerID.

When you print your list, you can check if the customerID of the current record is different from the prevoius record. If different, then it is the first record of the new customer. If not, it belongs to the same customer.

And the code:

SortedSet<DoubleSales> set = new TreeSet<DoubleSales>(new Comparator<DoubleSales>() {
    @Override
    public int compare(DoubleSales o1, DoubleSales o2) {
        return Long.compare(o1.customerId, o2.customerId);
    }
});

// ... import data
set.add(new DoubleSales( /*...*/ ));

// iterate through data
DoubleSales prevDS = null;
for (DoubleSales ds : set) {
    if (prevDS == null || ds.customerId != prevDS.customerId) {
        // first record of a customer
        // print CustomerID, ProductID, UnitsSold
    } else {
        // second or next record of a customer
        // print ProductID, UnitsSold only
    }
    prevDS = ds;
}

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