-2

I am iterating a List<MyDTO> to get the results. My requirement is to get the first and last name in the list stored in separate String.Any suggestions?

    for (MyDTO result : results) {
        System.out.println("countName :" + result.getCountName());//returns 20 names
        System.out.println("Spread Percent :" + result.getSpreadPercent());
        System.out.println("Addr :" + result.getAddr());
    }

output:

ASD
xxx
Joh
Joe
xyz

I want the output as:

first Name: ASD
Last Name : xyz
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • How is that your ouptut? Where did the `countName` go? What about `Spread Percent` and `Addr`? What does your `MyDTO` look like? Which properties represent the first and last name? When you made the minimal effort to look up how to get first and last elements in a list, what did you find and what didn't you understand? – Savior Sep 13 '17 at 19:00

3 Answers3

2

You can use :

MyDTO firstresult = results.get(0);//first value
MyDTO lastresult = results.get(results.size() - 1);//last value

This can return the same value if your list contain only one element.

Note

before you use this, you have to check if the list is not empty else you will get an exception.

MyDTO firstresult = null;
MyDTO lastresult  = null;
if (!results.isEmpty()) {
    firstresult = results.get(0);//first value
    lastresult = results.get(results.size() - 1);//last value
}

Please see my updated code in post above.In the list i have many properties like countName,SpreadPercent,Addr. I want to get only first and last element value of countNam

With the same solution you can just use :

results.get(0).getCountName();
results.get(results.size() - 1).getAddr();
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Please see my updated code in post above.In the list i have many properties like countName,SpreadPercent,Addr. I want to get only first and last element value of countName. – user8190500 Sep 13 '17 at 18:57
1

You can use the indexes to access the elements:

System.out.println("First Name :" + results.get(0). getCountName());

System.out.println("Last Name :" + results.get(results.size()-1).getCountName());

Edit :: Based on the

System.out.println("countName :" + result.getCountName());//returns 20 names

Seems like the getCountName returns a List further. To print just the firstName and lastName for this list, you can simply reuse the above code with indexing on the inner list as:

System.out.println("First Name :" + results.getCountName().get(0));

System.out.println("Last Name :" + reslts.getCountName().get(reslts.getCountName().size()-1));
Naman
  • 27,789
  • 26
  • 218
  • 353
0

you can get any element of a list by the index, just be aware to move the index between 0 and size - 1

List<MyDTO> myList = ...
//the 1st element is
MyDTO fisrt = myList.get(0);

but since that is a list, you can too use a linkedList and use the methods get first and get last

List<MyDTO> x = new LinkedList<>();

MyDTO first = ((LinkedList<MyDTO>) x).getLast();
MyDTO last = ((LinkedList<MyDTO>) x).getFirst();
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97