answersLogoWhite

0


Best Answer

Unfortunately, there seems to be no built in way of adding all elements of a list to a map. This means that we need to iterate through each element of the ArrayList and add it to the HashMap individually.

/**
* Generic method to add all elements from source into dest.
*/
public static final void addAll(final ArrayList source, final HashMap dest) {

// Iterate through each element in source and put it in dest.
for (int i = 0; i < source.size(); ++i) {

// We will use the index of each element of source as the key in dest.
dest.put(i, source.get(i));

}

}

Sample use:

// Create a new ArrayList and add some stuff to it.
ArrayList list = new ArrayList();
list.add(1); list.add(2); list.add(3);
list.add(4); list.add(5); list.add(6);
list.add(7); list.add(8); list.add(9);

// Print out our list
System.out.println(list);

// Create a new map and call our function
HashMap map = new HashMap();
addAll(list, map);

// Print out to verify identical contents
System.out.println(map);

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you convert an ArrayList to a HashMap?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Use of java.util package?

The java.util package contains many useful utilities provided by the Java programming language. They include:Collections - ArrayList, Vector, HashMap etcEvent modelsDate & time featuresString tokenizerRandom number generatoretc.


How can you convert ArrayList to Vector?

Both of these types of Collections allow for a new instance to be created with the contents of another Collection. // This method will accept a Vector and return a new ArrayList which contains all elements of that Vector static ArrayList toArrayList(Vector v) { return new ArrayList(v); } // This method will accept an ArrayList and return a new Vector which contains all elements of that ArrayList static Vector toArrayList(ArrayList al) { return new Vector(al); }


Difference between hashtable and hashmap in java?

1) hashtable is synchronized , hashmap is not 2) hashtable is slow , hashmap is fast 3) hashtable is old and hashmap is new


Collection framework in java?

Collection framework is a framework in java that helps us handle multiple java objects in one shot. For example if you have an employee validation system where you have details about all the employees in an office, you will have lets say 1000 employee objects available in an ArrayList which we can iterate and check if every employee that is going through the door is a valid employee. Some of the collections we can use are: a. ArrayList b. Vector c. HashMap d. HashSet e. etc


Where can one find information on hashmap?

One can find all the official documentation for HashMap, as well as a download for it, at the official Oracle site. Some sites that have good information on using HashMap include Java Revisited and Tutorials Point.


Which all represent the properties of hashtable in java?

HashTableLike Vector, Hashtable has existed from prehistoric Java times. For fun, don't forget to note the naming inconsistency: HashMap vs. Hashtable. Where's the capitalization of t? Oh well, you won't be expected to spell it. Anyway, just as Vector is a synchronized counterpart to the sleeker, more modern ArrayList, Hashtable is the synchronized counterpart to HashMap. Remember that you don't synchronize a class, so when we say that Vector and Hashtable are synchronized, we just mean that the key methods of the class are synchronized. Another difference, though, is that while HashMap lets you have null values as well as one null key, a Hashtable doesn't let you have anything that's null.


Hash table in java?

HashMapThe HashMap gives you an unsorted, unordered Map. When you need a Map and you don't care about the order (when you iterate through it), then HashMap is the way to go; the other maps add a little more overhead. Where the keys land in the Map is based on the key's hashcode, so, like HashSet, the more efficient your hashCode() implementation, the better access performance you'll get. HashMap allows one null key and multiple null values in a collection.HashTableLike Vector, Hashtable has existed from prehistoric Java times. For fun, don't forget to note the naming inconsistency: HashMap vs. Hashtable. Where's the capitalization of t? Oh well, you won't be expected to spell it. Anyway, just as Vector is a synchronized counterpart to the sleeker, more modern ArrayList, Hashtable is the synchronized counterpart to HashMap. Remember that you don't synchronize a class, so when we say that Vector and Hashtable are synchronized, we just mean that the key methods of the class are synchronized. Another difference, though, is that while HashMap lets you have null values as well as one null key, a Hashtable doesn't let you have anything that's null.


What are the requirements to download a java arraylist?

The requirements to download a java arraylist are a pc with java software installed. A java arraylist is used to store a group of elements in a specific order.


How can you sort an arraylist of strings?

You can sort an ArrayList by using the sort method of the Collecions class (java.util.Collections). Assuming you have an ArrayList called foo: Collections.sort(foo);


How the values stores in HashMap?

values are stored in a bucket in hashmap, if two objects map to same bucket location by hash function then they are stored as same bucket location but in a form of linked list.


Is the hashmap commonly used in Java?

Absolutely. The hashmap is used quite a bit in java scripting as it is important in making many things in Java work. It's difficult to learn, but handy to know about.


What type of platform does Arraylist Java run on?

Arraylist Java runs on Oracle which is a relational data management database produced by the Oracle Corporation. Arraylist Java has been part of the Java framework ever since Java 5.