1

How to get specific object values ​​from java & lt; List & gt; and conve...

 1 year ago
source link: https://www.codesd.com/item/how-to-get-specific-object-values-from-java-list-and-convert-to-string-array.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to get specific object values ​​from java & lt; List & gt; and convert to String Array

advertisements

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?


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);


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK