Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

turn a Set into a indexed List?

 
Holmes Wong
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to use a list (or collection) to display data on
jsp page. Is there any fats way to trun a Set into an indexed Collection
or List? (Can get individual element using get(index))
thanks,
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since a Set is a Collection, you should be able to do this:

But instead of doing all that, why don't you just use an iterator?

[ October 07, 2003: Message edited by: Phil Chuang ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List list = new ArrayList(set);
The order of the list will be whatever order the set had. For a TreeSet, this means either natural order, or order determined by the Comparator; for HashSet, well... just pretend it's random. (HashSet order is based on hashCode() and capacity, but the exact algorithm is not specified, so it's best to make no assumptions.)
 
Holmes Wong
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, Jim. This may be what I want. I will try it out.
QUOTE]Originally posted by Jim Yingst:
List list = new ArrayList(set);
The order of the list will be whatever order the set had. For a TreeSet, this means either natural order, or order determined by the Comparator; for HashSet, well... just pretend it's random. (HashSet order is based on hashCode() and capacity, but the exact algorithm is not specified, so it's best to make no assumptions.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic