erertg ghg
erertg ghg

Reputation: 89

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

Upvotes: 0

Views: 1345

Answers (2)

Chetan Kinger
Chetan Kinger

Reputation: 15212

If you are using Java 8, you could simply use a one liner :

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

Upvotes: 1

user180100
user180100

Reputation:

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
}

Upvotes: 2

Related Questions