简体   繁体   中英

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

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

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

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

 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

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

so pls advise how to achieve this within InvPostPayNotification method

Pseudo code, because I don't know details for 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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