Solved

convert a hashtable to arraylist

Posted on 2003-12-09
7
4,511 Views
Last Modified: 2007-12-19
Hi ,
Can I convert a HashTable to an ArrayList

thx
0
Comment
Question by:applekanna
  • 4
  • 3
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 9909825
depends if you want the valkues or keys.
to get the keys use:

ArrayList list = new ArrayList(ht.values());

to get the keys:

ArrayList list = new ArrayList(ht.keySet());
0
 
LVL 3

Author Comment

by:applekanna
ID: 9909858
One question what is the differnce between

using constructor
ArrayList list = new ArrayList(ht.values());

and typecasting
ArrayList list = (ArrayList ) ht.values();

Thx
0
 
LVL 92

Expert Comment

by:objects
ID: 9909880
> using constructor
> ArrayList list = new ArrayList(ht.values());

Creates a new instance of ArrayList

> and typecasting
> ArrayList list = (ArrayList ) ht.values();

Assigns list to the collection returnurned by values().
This will only work if the return instance is an instance of ArrayList (which I'm pretty sure it's not).
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:applekanna
ID: 9910038
So is typecasting is only used when return type is of object and of other primitive types like char to int is it?

0
 
LVL 92

Expert Comment

by:objects
ID: 9910092
eg. if you have an ArrayList containing Strings':

String s = (String) mylist.get(0);
0
 
LVL 3

Author Comment

by:applekanna
ID: 9910108
thx for ur help :)
0
 
LVL 92

Expert Comment

by:objects
ID: 9910116
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now