user20714961
user20714961

Reputation:

How to get particular field value from map object using java

I have a map object and it contains manufacture Price code as a string value. When I try to get manufacture price code value from map object, it gives me "java.util.HashMap", but I want it to give me manufacture price code value. For your reference, I posted the code I tried for this issue

private Item getItemManufacturerPriceCodes(Item item) {
    List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
    List<String> priceCodeList = new ArrayList<String>();
    for (ItemPriceCode ipc : itemPriceCodes) {
        //get the string value from the list
        priceCodeList.add(ipc.getPriceCode());
    }
    //pass this string value in query
    List<ManufacturerPriceCodes>mpc = manufacturerPriceCodesRepository.
             findByManufacturerIDAndPriceCodeInAndRecordDeleted(item.getManufacturerID(),priceCodeList,NOT_DELETED);
      
    //Convert list to map
    Map<String, ManufacturerPriceCodes> ipcToMFPNameMap = mpc.stream().collect(
            Collectors.toMap(ManufacturerPriceCodes :: getPriceCode,Function.identity()));// Object

     for (ItemPriceCode ipcs : itemPriceCodes) {
         ipcs.setManufacturerPriceCode(ipcToMFPNameMap.getClass().getName());
     }
     item.getItemPriceCodes()
             .removeIf(ipcs -> DELETED.equals(ipcs.getRecordDeleted()));
     return item;      
}

I get this type of result.

enter image description here

I want this type of result

enter image description here

I get an error at this point

for (ItemPriceCode ipcs : itemPriceCodes) {
    String manufacturePriceCode =ipcToMFPNameMap.get(priceCode).getName();
    ipcs.setManufacturerPriceCode(manufacturePriceCode);
}

How to get manufacture Price code from my map object, ipcToMFPNameMa?

Upvotes: 0

Views: 698

Answers (1)

h3y
h3y

Reputation: 11

Make sure to review the documentation for HashMap, the code snippets you included show that you aren't using the correct methods to retrieve key-mapped values.

From your original code snippet, you are invoking Map.getClass().getName() to set your manufacturerPriceCode.

for (ItemPriceCode ipcs : itemPriceCodes) {
    ipcs.setManufacturerPriceCode(ipcToMFPNameMap.getClass().getName());
}

getName is a method of the Java Class API. Invoking it returns the name of the Class entity.

Examples:

String.class.getName() returns "java.lang.String"

byte.class.getName() returns "byte"

This causes your manufacturerPriceCode to be set to "java.util.HashMap".

In your second snippet, you changed your code to:

for (ItemPriceCode ipcs : itemPriceCodes) {
    String manufacturePriceCode =ipcToMFPNameMap.get(priceCode).getName();
    ipcs.setManufacturerPriceCode(manufacturePriceCode);
}

This will create a compile time error due to ipcToMFPNameMap.get(priceCode).getName(); as getName() is invalid here.

To return key-mapped values in HashMap, simply use get().

for (ItemPriceCode ipcs : itemPriceCodes) 
    ipcs.setManufacturerPriceCode(ipcToMFPNameMap.get(icps)); 

Upvotes: 1

Related Questions