Retrieve Last element from Collection

Hi,I am a beginner in Java. Can somebody tell me how to retrieve last element from java.util.collection without Iterating?
[136 byte] By [ms_mahesh123a] at [2007-11-26 12:22:36]
# 1
Object list = Collection.toArray();Object lastElement = list[list.length-1];
Ayman_javaa at 2007-7-7 15:16:10 > top of Java-index,Archived Forums,Socket Programming...
# 2
Object list[] = Collection.toArray();Object lastElement = list[list.length-1];
Ayman_javaa at 2007-7-7 15:16:10 > top of Java-index,Archived Forums,Socket Programming...
# 3
A more straightforward approach is to use Collection.size() and access the last element.Another possibility is to reverse the Collection with Collections.reverse() and access the first element.
ChuckBinga at 2007-7-7 15:16:10 > top of Java-index,Archived Forums,Socket Programming...
# 4

> Hi,

> I am a beginner in Java. Can somebody tell me

> how to retrieve last element from

> java.util.collection without Iterating?

You realize of course that this is rather silly since not every Collection necessarily has a "last" element. For it to have the notion of a "last" element it would have be ordered and would be a List.

kablaira at 2007-7-7 15:16:10 > top of Java-index,Archived Forums,Socket Programming...