Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get specific object values from java <List> and convert to String Array

I need your assistant and help in getting the values of an object in a Java and then to convert the output to String Array so I can pass it to a procedure. Here is my Java class:

public class PendingRequests  {

    private String requestDate; //Getter & Setter
    private String requestNo; //Getter & Setter
    private String employeeName; //Getter & Setter

}

And in the bean I am defining a List called "selectedRequests":

private List<PendingRequests> selectedRequests;

The selectedRequests is having values and I need to get the values of the requestNo from it and then convert it to String Array. With my attempts, I was only able to print them in the console by using the below code:

for(Object obj : selectedRequests){
    System.out.println("Obj = "+((PendingRequests)obj).getRequestNo());

But is it the correct way and what should I do next?

like image 656
99maas Avatar asked Nov 17 '25 05:11

99maas


1 Answers

String[] requestNos = new String[selectedRequests.size()];

for (int i = 0; i < selectedRequests.size(); i++) {
    requestNos[i] = selectedRequests.get(i).getRequestNo();
}

And here is the Java 8 version of the same thing:

String[] requestNos = selectedRequests
                          .stream()
                          .map(r -> r.getRequestNo())
                          .toArray(String[]::new);

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!