hsz
hsz

Reputation: 152286

Convert java.util.HashMap keys to vector in Matlab

I have a HashMap object that contains key=>value set which are both integers.

F = java.util.HashMap;
F.put(1, 123);
F.put(3, 432);
F.put(7, 31);

I need to extract keys to the vector. I access keys with:

F.keySet.toArray

It returns Object:

ans =

java.lang.Object[]:
    [1]
    [3]
    [7]

How to convert it to vector ?

[1 3 7]

Upvotes: 2

Views: 2511

Answers (2)

Eastsun
Eastsun

Reputation: 18869

You may try as following:

>> F = java.util.HashMap;
F.put(1, 123);
F.put(3, 432);
F.put(7, 31);
>> vec = cell2mat(F.keySet.toArray.cell)
vec =
     3
     7
     1
>> whos
  Name      Size            Bytes  Class                 Attributes

  F         1x1                    java.util.HashMap               
  ans       0x0                 0  double                          
  vec       3x1                24  double                          
  z         3x1                    java.lang.Object[]   

Upvotes: 4

Mukul Goel
Mukul Goel

Reputation: 8467

is this what you looking for?

Vector V=new Vector();
for(int i=0;i<3;i++)
V.add(ans[i]);

where ans is the Object[] that you have?

Upvotes: 0

Related Questions