• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Collection

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do i read the 1st and the last value from the Collection

Regards,
aakash
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you explored the JavaDoc for collection? See if that helps you find a couple ways.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Associate an iterator to the collection and get the first and the last elements in the collection..

U may need to typecast the object when retrieving the element

bye
 
aakash bhatt
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have got iterator but it would iterate one by one. I do not want to write a while loop and break statement for 1st element
and for last element to iterate the whole collection through iterator has next.

Is there any other way, as Collectio have size() but do not have any get method to retrieve the element.
Any other way around other than this.

Thanks
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The loop is one choice, toArray might be another. I wouldn't worry much about the efficiency of the loop unless you have bazillions of entries. Collection doesn't have item(n) or elementAt(n). Some of the implementing classes do, but if you have to work with Collection you can only use Collection methods.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have to write this against a Collection rather than List, you have no choice but to iterate (toArray will iterate and allocate an array, so it's possibly more expensive [ArrayList has an array already, so it can use System.arraycopy(), so it won't iterate, but I digress]).

The reason is that Collection is the lowest common denominator of a "thing that holds other objects," and it has no concept of ordering. List provides indexed access, allowing you to call list.get(0) and list.get(list.size()-1).

Note that this implies that Collection itself doesn't have any idea of "first" and "last" elements. In fact, it's conceivable (though unlikely in practice) that getting the first and last elements from a generic Collection could return different elements on subsequent calls.

For example, if I'm holding five coins in my hand, which one is the first one? And after I shake them around in my hand? The collection of coins hasn't changed since they are unordered.

Are you sure you don't have a List?
 
Let me tell you a story about a man named Jed. He made this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic