Solved

convert a hashtable to arraylist

Posted on 2003-12-09
7
4,572 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 84
recursion example 16 126
netstat -ano | find "8000" and taskkill /f /pid 2984 3 56
maven module vs maven project 3 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

770 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