Home > Back-end >  how to get a particular field value from map object using java
how to get a particular field value from map object using java

Time:12-10

I am trying to set manufacture price code ,that value is in my map object but when I want to get getName() from map object I am not able to get that particular value. If I use

ipcToMFPNameMap.getClass().getName()

this line of code to get particular value I get "java.util.HashMap" in

my manufacture price code filed for your reference I post my code what I tried to get the particular result

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 got this type of Result enter image description here

But I want this this type of Result enter image description here

I get issue exact at this point

ipcs.setManufacturerPriceCode(ipcToMFPNameMap.getClass().getName());

my manufacture price code is a string type

CodePudding user response:

Your Map contains ManufacturerPriceCodes objects keyed on their priceCode which is defined as a String. So you can get the name of an item in the Map as follows (where priceCode is a String).

String manufacturerPriceCodesName = ipcToMFPNameMap.get(priceCode).getName();

The ManufacturerPricesCodes appears to capture the price code as a String but Item has a List<ItemPriceCode>. You'll have to figure out how to map back and forth.

CodePudding user response:

Give more explanation.

  1. In JSON file, is values bound to key priceCode and manufacturerPriceCode always equal?
  2. What is the relationship between classes ItemPriceCodes and ManufacturerPriceCodes ?
  •  Tags:  
  • java
  • Related