Home > other >  How to map list of elements using java
How to map list of elements using java

Time:12-08

I am new to java I want to map my for each loop list elements using map and get the value from map, but I am not able to use map in my for each statement. For your reference I post my code

private Item getItemManufacturerPriceCodes(Item item) {
          List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes;
       
          for(ItemPriceCode ipc : itemPriceCodes) {
              Optional<ManufacturerPriceCodes> mpc = manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), ipc.getPriceCode(), NOT_DELETED);
              if(mpc.isPresent())
                  ipc.setManufacturerPriceCode(mpc.get().getName());
          }
          item.getItemPriceCodes()
          .removeIf(ipc -> DELETED.equals(ipc.getRecordDeleted()));
          return item;
      }

I want to use my query code line above for each loop and pass list of price code inside map then get the values from map. this above code works fine . when I pass one price code value the loop move one time but when pass ten value in that case loop move ten times. But I want loop always move one time how many value I pass using map. how can I do it.

I want to use below line above for each loop

   Optional<ManufacturerPriceCodes> mpc = manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), ipc.getPriceCode(), NOT_DELETED);

Getting same result using map. First of pass list of elements price code inside map and get the values from map then set those values.

I tried below way but it not working as above programme

private Item getItemManufacturerPriceCodes(Item item) {
          List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
        Optional<ManufacturerPriceCodes> mpc = manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), itemPriceCodes, NOT_DELETED);
          for(ItemPriceCode ipc : itemPriceCodes) {       
              if(mpc.isPresent())
                  ipc.setManufacturerPriceCode(mpc.get().getName());
          }
          item.getItemPriceCodes()
          .removeIf(ipc -> DELETED.equals(ipc.getRecordDeleted()));
          return item;
      }

How can I map these list of price code and set them. My main aim is modify those piece of code using map and work same as my above code that i explain in my problem statement. Is it possible to modify those codes using map.

CodePudding user response:

Seems like you are trying to find a way to call setManufacturerPriceCode on a list of itemPriceCodes based on the ManufacturerPriceCodes that references them. Assuming there is no reference from the ItemPriceCode to the ManufacturerPriceCodes, I'd go about this differently:

In your ManufacturerPriceCodesRepository repository:

@Query("Select ipc.id, mpc.name from ManufacturerPriceCodes mpc join mpc.priceCode ipc where mpc.id = :id and ipc in :itemPriceCodes and mpc.recordDeleted = :notDeleted")
 List<Object[]> findMFPNameByIdAndRecordDeletedAndPriceCodes(String Id, <type> recordDeleted, List<ItemPriceCode> itemPriceCodes)

This will return a List<Object[]>, which each Object[] representing one ItemPriceCode id/name pair. You can use this in your getItemManufacturerPriceCodes method:

private Item getItemManufacturerPriceCodes(Item item) {
  List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
  List<Object[]> keyPairs= manufacturerPriceCodesRepository.findMFPNameByIdAndRecordDeletedAndPriceCodes(item.getManufacturerID(), NOT_DELETED, itemPriceCodes);

  Map<String,String> ipcToMFPNameMap = keyPairs.stream().collect(
                Collectors.toMap(x -> x[0], x->x[1]));

  itemPriceCodes
          .forEach(ipc ->{ 
              if (ipcToMFPNameMap.get(ipc.getId())!=null)
                ipc.setManufacturerPriceCode(ipcToMFPNameMap.get(ipc.getId());
          })
          .removeIf(ipc -> DELETED.equals(ipc.getRecordDeleted()));
  return item;
}

I'm sure there are more elegant ways than dealing with object[] with Spring, and certainly for the stream handling, but this is a general idea.

CodePudding user response:

To map a list of elements using Java, you can use the map() method from the Stream API. This method takes a function as an argument and applies it to each element in the list, producing a new list containing the results.

Here is an example of how you can use the map() method to square each element in a list of numbers:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squares = numbers.stream()
                           .map(x -> x * x)
                           .collect(Collectors.toList());

In this example, the map() method squares each element in the numbers list and stores the result in the squares list. The collect() method is used at the end to convert the stream of results back into a list.

Note that this code uses the Java 8 version of the map() method, which is a part of the Stream API. In previous versions of Java, you can use the Map interface and the map() method from the Collections class to achieve the same result. However, the syntax is slightly different and the Stream API is generally considered to be a more powerful and convenient way to manipulate collections of data.

  • Related