简体   繁体   English

将对象列表转换为String数组

[英]Convert a List of objects to a String array

I have the below pojo which consists of the below members so below is the pojo with few members in it 我有下面的pojo,它由以下成员组成,所以下面是其中很少成员的pojo

public class TnvoicetNotify  {
    private List<TvNotifyContact> toMap = new ArrayList<TvNotifyContact>();
    private List<TvNotifyContact> ccMap = new ArrayList<TvNotifyContact>();

}

now in some other class i am getting the object of above class TnvoicetNotify in a method signature as parameter as shown below .. So i want to write the code of extraction from list and storing them in string array within this method itself 现在在其他某个类中,我正在方法签名中获取上述TnvoicetNotify类的对象作为参数,如下所示。.因此,我想编写从列表中提取的代码,并将其存储在此方法本身的字符串数组中

public void InvPostPayNotification(TnvoicetNotify TnvoicetNotify)
    {

        String[] mailTo =  it should contain all the contents of list named toMap  
        String[] mailCC =  it should contain all the contents of list named ccMap 
     }

now in the above class i need to extract the toMap which is of type list in the above pojo named TnvoicetNotify and i want to store each item if arraylist in a string array as shown in below fashion 现在在上面的类中,我需要提取toMap,它是上述pojo中名为TnvoicetNotify的pojo类型的列表,并且如果arraylist以字符串形式显示,我想存储每个项目,如下所示

for example first item in list is A1 and second is A2 and third is A3 so it should be stored in string array as 例如列表中的第一项是A1,第二项是A2,第三项是A3,因此应将其存储在字符串数组中,如下所示:

 String[] mailTo = {"A1","A2","A3"};

similarly i want to achieve the same for cc section also as in above pojo it is in list i want to store in the below fashion 类似地,我也想在cc部分中实现相同的效果,就像在上面的pojo中一样,它在列表中我想以以下方式存储

 String[] mailCc = {"C1","C2","C3"};

so pls advise how to achieve this within InvPostPayNotification method 所以请建议如何在InvPostPayNotification方法中实现此目标

Pseudo code, because I don't know details for TnvoicetNotify : 伪代码,因为我不知道TnvoicetNotify详细信息:

public void invPostPayNotification(final TnvoicetNotify tnvoicetNotify)
{
    final List<String> mailToList = new ArrayList<>();
    for (final TvNotifyContact tv : tnvoicetNotify.getToMap()) { // To replace: getToMap() 
        mailToList.add(tv.getEmail()); // To replace: getEmail() 
    }
    final String[] mailTo = mailToList.toArray(new String[mailToList.size()])
    // same for mailCc then use both arrays
}

如果您使用的是Java 8,则只需使用一个内衬即可:

String[] mailCC = ccMap.stream().map(TvNotifyContact::getEmail).toArray(String[]::new);

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

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