Map (Hashmap) to Array

Hello Java Friends,

Please, I have a map(Mappings = new HashMap<String, Integer>) which contains both String and Integer elements. I wanted to store the string elements of the Map in a String Array and Integer elements in Integer Array but end up with error messages. How can I implement it?

See part of my code below:

publicclass Tryit{

private Map<String, Integer> Mappings;

String[]StringElem;

int [] IntegerElem;

public Tryit(){

Mappings =new HashMap<String, Integer>()

StringElem =new String [100];

IntegerElem =newint [100];

-

-

-

for (String index : Mappings.KeySet()){

StringElem = index;

}

for (int i : Mappings.values(){

IntegerElem = i;

}

}

}

[1430 byte] By [Jona_Ta] at [2007-11-27 0:35:50]
# 1

> String[] StringElem;

> int [] IntegerElem;

> ...

> for (String index : Mappings.KeySet()){

> StringElem = index;

> }

> for (int i : Mappings.values(){

> IntegerElem = i;

> }

StringElem is an array of String. index is a String.

You cannot assign a String to an array of String. (incompatible type)

Likewise, you cannot assign i to integerElem (incompatible type).

Hope that reminds you of how you should fix it now.

KathyMcDonnella at 2007-7-11 22:44:45 > top of Java-index,Java Essentials,Java Programming...
# 2
A Map can give you its keySet() (which is a Set) and its values() whichis a Collection; both a Set and a Collection can be transmogrified toan array, using the *ahem* toArray() method.The Arrays utility class knows how to sort arrays.kind regards,Jos
JosAHa at 2007-7-11 22:44:45 > top of Java-index,Java Essentials,Java Programming...
# 3

import java.util.*;

public class ToArrayExample {

public static void main(String[] args) {

//create example map

Map < String, Integer > m = new HashMap < String, Integer > ();

m.put("one", 1);

m.put("two", 2);

m.put("three", 3);

m.put("four", 4);

//copy to arrays

int size = m.size();

String[] keys = new String[size];

Integer[] vals = new Integer[size];

m.keySet().toArray(keys);

m.values().toArray(vals);

//display

System.out.format("keys = %s%n", Arrays.toString(keys));

System.out.format("vals = %s%n", Arrays.toString(vals));

}

}

DrLaszloJamfa at 2007-7-11 22:44:45 > top of Java-index,Java Essentials,Java Programming...
# 4

In addition to JosAH's and DrLaszloJamf's advice, you might

also want to look at the entrySet() method,

which is more efficient than calling keySet() and values().

Just have a loop like this:

String[] names = new String[map.size()];

int[] numbers = new int[map.size()];

int i=0;

for(Map.Entry<String,Integer> e: map.entrySet()) {

names[i] = e.getKey();

numbers[i] = e.getValue();

i++;

}

KathyMcDonnella at 2007-7-11 22:44:45 > top of Java-index,Java Essentials,Java Programming...
# 5
Hello KathyMcdonnel, JosAH and DrLaszloJamf,I thank you all for the help.Jona_T
Jona_Ta at 2007-7-11 22:44:45 > top of Java-index,Java Essentials,Java Programming...
# 6

Hello friends,

The idea you gave me worked but still I am having a small problem: I have two classes, class A and B. In Class A, I implemented a set and get method which I will use in Class B to retrieve the keys and Values. But when I call the get method in Class B, I always get null. The arrays in both classes are all declared globally.

Get and Set Method in Class A :

public void setKeyValues() {

int i = 0;

for(Map.Entry<String, Integer> e: map.entrySet()) {

ElementName[i]= e.getKey();

i++;

}

}

public String []getKeyValues(){

return ElementName;

}

Call the Get Method in Class B

import package X.A

public class B{

private String [] names;

public B(){

A bix = new A();

int length = bix.getKeyValues().length;

names = new String[length];

-

System.out.printf ("%d", gettingKeys(bix.getKeyValues());

-

-

}

public String[] gettingKeys(String []naming) {

names = new int[bix.getKeyValues().length];

int size = naming.length;

for (int p = 0; p < size; p++) {

names[p]=naming[p];

p++;

}

return names;

}

}

Please where is the problem ? I am unable to read and print the map values and keys from another friend class.

Jona_T

Jona_Ta at 2007-7-11 22:44:45 > top of Java-index,Java Essentials,Java Programming...