简体   繁体   English

通过将字符串(来自数组)分配为对象的参数之一,将其他参数设置为空,将字符串数组转换为对象列表

[英]Convert String array to List of Objects by assigning the string(from array) as one of the parameter of the Object leaving other parameters as null

I have a String array 我有一个字符串数组

adrs[] = {
    "BD Jules Ferry",
    "Rue de Republique",
    "Avenue de Julin"};

I have an Object named 'Address' (//getter setter omitted just for simplicity) 我有一个名为“地址”的对象(为简单起见,省略了// getter setter)

public class Address{

     adrsLine1 String;

     adrsLine2 String;

     adrsLine3 String;

     postalCode String;

}

I want to convert this adrs[] to List<<Address>> addrsList , by assigning each string in the array as adrsLine1 parameter of Address . 我想通过将数组中的每个字符串分配为Address adrsLine1参数,将该adrs[]转换为List<<Address>> addrsList

Once converted each address object in my addrsList will be like, 转换后,我的addrsList每个地址对象都会像这样,

  • addrsList.get(0) will have [BD Jules Ferry, null, null, null] addrsList.get(0)将具有[BD Jules Ferry,null,null,null]
  • addrsList.get(1) will have [Rue de Republique, null, null, null] addrsList.get(1)将具有[Rue de Republique,null,null,null]
  • addrsList.get(2) will have [Avenue de Julin, null, null, null] addrsList.get(2)将具有[Avenue de Julin,null,null,null]

I use Java 8, Currently, I use for loop to achieve this, is there any short cut or more efficient way available in Java 8? 我使用Java 8,目前,我使用for loop来实现这一点,Java 8中是否有任何捷径或更有效的方法?

Sample code: 样例代码:

private String[] AdrsArray = {"BD Jules Ferry", "Rue de Republique", "Avenue de Julin"};

List<Address> addrsList = new ArrayList<>();

for (String adrsLine : AdrsArray){
    Address address = new ReturnAddress();
    address.setAdrsLine1(adrsLine);
    addrsList.add(address);
} 

Output: 输出:

Address address = addrsList.get(0); 
address.getAdrsLine1() //should have "BD Jules Ferry"
address.getAdrsLine2() //should have null
address.getAdrsLine3() //should have null
address.getAdrsPostalCode() //should have null

You can use streams available in Java 8 to simplify your code as shown below. 您可以使用Java 8中可用的streams来简化代码,如下所示。

List<Address> addrsList = Arrays.stream(adrs).map(adr -> {
    Address address = new ReturnAddress();
    address.setAdrsLine1(adr);
    return address;
}).collect(Collectors.toList());
List<Address> addressList = Arrays.stream(AdrsArray)
                                  .map(Address::new)
                                  .collect(Collectors.toList());

You have to add a constructor to Address class that accepts adrsLine1 . 您必须向Address类添加一个接受adrsLine1的构造adrsLine1

public class Address{

     private String adrsLine1;

     private String adrsLine2;

     private String adrsLine3;

     private String postalCode;

     Address(String address1) {
       adrsLine1 = address1;
     }

}

You can use streams. 您可以使用流。 I would recommend you to implement a constructor in class Address , such as 我建议您在Address类中实现一个构造函数,例如

Address(String adrsLine1) {
  this.adrsLine1 = adrsLine1;
}

Then you can do it with this short code snippet: 然后,您可以使用以下简短代码段进行操作:

List<Address> addrsList = Arrays.stream(adrs)
    .map(adr -> new Address(adr))
    .collect(Collectors.toCollection(ArrayList::new));

If you need a specific List type such as ArrayList , you should use Collectors.toCollection(ArrayList::new) , otherwise (if you can accept any list type) you can use just Collectors.toList() instead. 如果需要特定的列表类型(例如ArrayList ,则应使用Collectors.toCollection(ArrayList::new) ;否则(如果您可以接受任何列表类型),则可以仅使用Collectors.toList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM