Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- h = java.util.HashMap;
- h.put(5, 'test');
- h.put(7, magic(4));
- h=java.util.HashMap;
- st.val = 7;
- h.put(7, st);
- ??? No method 'put' with matching signature found for class 'java.util.HashMap'.
- function hmap = struct2hashmap(S)
- if ((~isstruct(S)) || (numel(S) ~= 1))
- error('struct2hashmap:invalid','%s',...
- 'struct2hashmap only accepts single structures');
- end
- hmap = java.util.HashMap;
- for fn = fieldnames(S)'
- % fn iterates through the field names of S
- % fn is a 1x1 cell array
- fn = fn{1};
- hmap.put(fn,getfield(S,fn));
- end
- >> M = java.util.HashMap;
- >> M.put(1,'a');
- >> M.put(2,33);
- >> s = struct('a',37,'b',4,'c','bingo')
- s =
- a: 37
- b: 4
- c: 'bingo'
- >> M.put(3,struct2hashmap(s));
- >> M
- M =
- {3.0={a=37.0, c=bingo, b=4.0}, 1.0=a, 2.0=33.0}
- >>
- % Must initialize with a dummy value to allow numeric keys
- m = containers.Map(0, 0, 'uniformValues',false);
- % Remove dummy entry
- m.remove(0);
- m(5) = 'test';
- m(7) = magic(4);
- m(9) = struct('foo',42, 'bar',1:3);
- m(5), m(7), m(9) % get values back out
- h = java.util.HashMap;
- carr = {7, 'hello'};
- h.put(7, carr);
- % OR
- h = java.util.HashMap;
- st.val = 7;
- h.put(7, struct2cell(st));
Add Comment
Please, Sign In to add comment