How to map values from List<String> into a List<Model>

Wion

I'm practicing Java 8 features and I got to a problem that I couldn't solve:

I have a List of Strings that I need to map into a List of Customers, the List of Strings only has the name of the customers, but Customer Model has other properties:

 public class Customer {
    private int id{get; set;};
    private String name{get; set;};
    private String company{get; set;};
}

public static List<Customer> convertList() { 

    List<Customer> customerList = new ArrayList<Customer>();
    
    List<String> nameList = new ArrayList<String>();
    nameList.add("Customer A");
    nameList.add("Customer B");
    nameList.add("Customer C");
    nameList.add("Customer D");
    nameList.add("Customer E");
    nameList.add("Customer F");
    
    return customerList.stream()
                       .map()//here I got stuck
                       .collect(Collectors.toList()); 
}

What I want to do is to to set the value of the List into the Name property of the List, I tried using a stream and map but couldn't figure how to use it in this scenario.

JavaTechnical
List<Customer> customerList = nameList.stream().map(name -> {
     Customer c = new Customer();
     c.setName(name);
     c.setId(generateId());
     c.setCompany(getCompany(name));
     return c;
}).collect(Collectors.toList());

You need to map the nameList to customerList so, you should stream on the nameList and convert it to customerList not the other way around. Next, you need to figure out how to get the additional information like id, company etc using some external method/service if at all that is required during the map() operation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract all String values from a list of map

Removing values from a List<Object> that is part of a Map<String,List<Object>>

How to create a List<String> from a List<Map<String, String>> in Dart?

How to Convert String Values From Map<String, List<String>> to type Double? Flutter

scala - access values from Option[List[Map[String,String]]]

How do i retrieve a List<Map<String,Object>> from cloud firestore and convert it into a custom model?

How to retrieve data from List<Map<String, dynamic>> model in DataTable flutter?

How to Reverse Values in Map <String, List<String>> Flutter?

How to format string with values from list?

How to get float values from string list

How to remove repeated string values from a list

How to initialize a list in Dart from type List<Map<String, Object>>?

How Can we show One Key and its Values from Map<String, List<Object>> at a time on a JSP page?

Extract values (not keys) from a List<Map<String,String>>, and flatten it to a List<String>

String from a list of model not binding

How to map from a Map to List

get key from Map<String,List<String>> knowing one of the List values

how to convert List<Map<String, String>> to Map<String, List<String>>

How to convert the values of a Map of List to a single List

How to get recent data by date from a List<Map<String, String>>?

How to Map String in A Dataframe from A Corresponding String List

How To Add String To List<String>? List<String> is Model In Model

How to assign multiple values from one Model into a List<T> in the ViewModel?

How to assign values to a model class from a list in Flutter?

How to add/transfer the values list from one model to another in django?

Get inner most values in the Map as a List in `Map<String, List<Map<String,Map<String,String>>>>` Java

How to split map with list as values into map with smaller list as values

How to Convert List<Map<String, Object>> to List<Map<String, String>>

How to add specific values from array list to the Hash map?