2

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?

1
  • 2
    As a sidenote, you could just say for(PendingRequests obj : selectedRequests) if your ArrayList is declared with the Generic PendingRequests Commented Sep 17, 2015 at 13:04

5 Answers 5

10
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);
Sign up to request clarification or add additional context in comments.

2 Comments

So that I have an index into the array, yes.
Kind of detail for the OP but what if the list is a LinkedList? Better: int i = 0; for(r : selectedRequests) { requestNos[i++] = r.getRequestNo(); } )
2
String[] stringArray = new String[selectedRequests.size()];
int i =0;
for(PendingRequests request: selectedRequests){
   stringArray[i] = request.getRequestNo();
   i++;
}

Comments

2

In Java 8:

selectedRequests.stream()
                .map(PendingRequests::getRequestNo)
                .forEach(System.out::println)

Comments

0

try:

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

forgive me it throws any errors, I just wrote it on the fly. And don't forget to add null checks as this code will throw NullPointerException if something is null.

Comments

0

Java 8 single line

String[] requestNos = selectedRequests.stream()
                      .map(
                          pr -> pr.getRequestNo()
                      )
                      .collect(Collectors.toList())
                      .toArray(new String[]{});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.