Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How we get the List objects in backward direction? [duplicate]

Hi i am getting List object that contains pojo class objects of the table. in my case i have to show the table data in reverse order. mean that, for ex i am adding some rows to particular table in database when i am added recently, the data is storing at last row in table(in database). here i have to show whole content of the table in my jsp page in reverse order, mean that what i inserted recently have to display first row in my jsp page.

here my code was like,

List lst = tabledate.getAllData();//return List<Table> Object
Iterator it = lst.iterator();
MyTable mt = new MyTable();//pojo class
while(it.hasNext())
{
     mt=(MyTable)it.next();
     //getting data from getters.
     System.out.println(mt.getxxx());
     System.out.println(mt.getxxx());
     System.out.println(mt.getxxx());
     System.out.println(mt.getxxx());
}
like image 318
Naresh Avatar asked Oct 25 '25 21:10

Naresh


1 Answers

Use a ListIterator to iterate through the list using hasPrevious() and previous():

ListIterator it = lst.listIterator(lst.size());
while(it.hasPrevious()) {
    System.out.println(it.previous());
}
like image 85
moinudin Avatar answered Oct 28 '25 12:10

moinudin