Mama
Mama

Reputation: 635

Convert list to string using Java streams

I created a for each loop and I get list of price code but I want to get same things without using any loop and perform this operation using java8 for your reference I post my old code. I want to change only this position of my code.

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());
        }

But I do not want to use any loop I want to get same result without using any loop . To over come from this issue I try this way but I am not get any success.

itemPriceCodes.stream().map(n -> String.valueOf(n)).collect(Collectors.toList());

Here This is my full code of this function

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;      
      }

Upvotes: 1

Views: 530

Answers (1)

Ronak Jain
Ronak Jain

Reputation: 3348

It should be

List<String> priceCodeList = itemPriceCodes.stream().map(ItemPriceCode::getPriceCode)).collect(Collectors.toList());

EDIT: Java 16+

List<String> priceCodeList = itemPriceCodes.stream().map(ItemPriceCode::getPriceCode)).toList()

Although this would return an immutable list.

Furthur reading: Differences of Java 16's Stream.toList() and Stream.collect(Collectors.toList())?

Although it doesn't seem necessary considering the use case if you want to use String.valueOf() you need to override toString accordingly in your class, as String.valueOf() uses it to get the string representation.

Definition of valueOf()

public static String valueOf(Object obj) {  
   return (obj == null) ? "null" : obj.toString();  
}

Thus to make your code work.. add this in your class

class ItemPriceCode{
  .
  .
  .
  public String toString(){
     return this.getPriceCode();
  }
}

Upvotes: 6

Related Questions