Get HashMap with Reflection - java

public class First {
public final static Map<String, String> MAP = new HashMap<>();
static {
MAP.put("A", "1");
MAP.put("B", "2");
}
}
public class Second {
public static void main(String[] args) {
Class<?> clazz = Class.forName("First");
Field field = clazz.getField("MAP");
Map<String, String> newMap = (HashMap<String, String>) field.get(null); // Obviously doesn't work
}
}
Pretty much it. I have no trouble getting for example values of String variables, but I'm stuck with this one. Tryed to google it, failed. Also, if possible I'd like to get this Map without instantiating its class.

The only thing you are missing is to handle the exceptions for:
Class.forName("First");
clazz.getField("MAP");
field.get(null);
The code below get the static map field from First class. Here I'm just throwing/propagating the exceptions in the main method but you should handle the exceptions in a try/catch block accordingly.
class First {
public final static Map<String, String> MAP = new HashMap<>();
static {
MAP.put("A", "1");
MAP.put("B", "2");
}
}
public class Second {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException,
ClassNotFoundException, NoSuchFieldException, SecurityException {
Class<?> clazz = Class.forName("First");
Field field = clazz.getField("MAP");
Map<String, String> newMap = (HashMap<String, String>) field.get(null); // Obviously doesn't work
System.out.println(newMap); //Prints {A=1, B=2}
}
}

Here the same example with a non static class:
package at.noe.szb;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class ReflectionTest {
private class First {
public Map<String, String> MAP = new HashMap<>();
First(){
MAP.put("A", "1");
MAP.put("B", "2");
}
}
#Test
public void testMap() throws Exception {
Class<?> clazz = Class.forName("at.noe.szb.First");
Field field = clazz.getField("MAP");
Map<String, String> newMap = (HashMap<String, String>) field.get(clazz);
assertEquals("{A=1, B=2}", newMap.toString());
}
}

Related

How to create a Guava HashMultiset from a Map<String, Object>

This doesn't compile:
Map<String, Object> map = new HashMap<String, Object>();
HashMultiset<Map<String, Object>> n1Properties = HashMultiset.create(map);
None of the overloaded create methods accepts a Map. But you can use the addAll method to add the Map converted to a Collection.
import com.google.common.collect.HashMultiset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class SO {
public static void main(String args[]) {
Map<String, Object> map = new HashMap<>();
map.put("foo", "FOO");
map.put("bar", "BAR");
HashMultiset<Map<String, Object>> n1Properties = HashMultiset.create();
n1Properties.addAll(Collections.singleton(map));
System.out.println(n1Properties);
}
}
Output:
[{bar=BAR, foo=FOO}]

How to create a hashmap in java class

This is my code. My intention is create a hashmap with 4 values, then export this class as a jar, add it to another project, and use the hashmap values there.
I'm getting error in all the "hmap.put". I'm unable to understand what I'm doing wrong. Please help.
import java.util.HashMap;
public class MyFirstClass {
private HashMap<Integer, String> hmap = new HashMap<Integer, String>();
hmap.put(2, "Jane");
hmap.put(4, "John");
hmap.put(3, "Klay");
hmap.put(1, "Deena");
public HashMap<Integer, String> gethmap()
{
return this.hmap;
}
public void sethmap(HashMap hmap)
{
this.hmap = hmap;
}
}
import java.util.HashMap;
public class MyFirstClass {
private HashMap<Integer, String> hmap = new HashMap<Integer, String>() {
{
hmap.put(4, "John");
hmap.put(3, "Klay");
hmap.put(1, "Deena");
}
};
public HashMap<Integer, String> gethmap() {
return this.hmap;
}
public void sethmap(HashMap<Integer, String> hmap) {
this.hmap = hmap;
}
}
Above code will help you to get the result which you desire. You should also note that you can not use instance variable directly inside class. you have to use that inside method only.
Java doesn't allow executing any statements outside of the scope of any method, field initialization or static block - that's why you get an error.
I suppose, your intent is to do some initialization with that four lines. And Java has support for such kind of initialization - it is the class constructor. So the proper code would look like the following:
import java.util.HashMap;
public class MyFirstClass {
private HashMap<Integer, String> hmap = new HashMap<Integer, String>();
// this is a constructor
public MyFirstClass() {
hmap.put(2, "Jane");
hmap.put(4, "John");
hmap.put(3, "Klay");
hmap.put(1, "Deena");
}
// here goes your other code
}
This way every object of MyFirstClass you create using new MyFirstClass() will contain the data you put in the constructor.
You can read more about the constructors in Java in the official documentation: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
There are multiple ways to do this. Easiest one is to just add brackets to your put statements:
import java.util.HashMap;
public class MyFirstClass {
private HashMap<Integer, String> hmap = new HashMap<Integer, String>();
{
hmap.put(2, "Jane");
hmap.put(4, "John");
hmap.put(3, "Klay");
hmap.put(1, "Deena");
}
public HashMap<Integer, String> gethmap() {
return this.hmap;
}
public void sethmap(HashMap hmap) {
this.hmap = hmap;
}
}
You should add a constructor to your class:
public MyFirstClass() {
this.hmap = new HashMap<Integer,String>();
// you can do .put here if you wish
}
And change the hmap field to:
private HashMap<Integer, String> hmap;
You're using a method outside of a method. You cannot call Hashmap.put within the class but outside the method - as was mentioned you want to do that in the constructor of the class
public class MyFirstClass {
public MyFirstClass() { //put it here }
}
You can put it in a static block, just as:
private static final Map<Integer, String> NAME_MAP = new HashMap<Integer, String>() {
private static final long serialVersionUID = 1L;
{
NAME_MAP.put(2, "Jane");
NAME_MAP.put(4, "John");
NAME_MAP.put(3, "Klay");
NAME_MAP.put(1, "Deena");
}
};

How to assert Map contains Map with entry

I have a unit test that needs to check for a nested map value. I can get my assertion to work by pulling out the entry and matching the underlying Map, but I was looking for a clear way to show what the assertion is doing. Here is a very simplified test:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class MapContainsMapTest {
#Test
public void testMapHasMap() {
Map<String, Object> outerMap = new HashMap<String, Object>();
Map<String, Object> nestedMap = new HashMap<String, Object>();
nestedMap.put("foo", "bar");
outerMap.put("nested", nestedMap);
// works but murky
assertThat((Map<String, Object>) outerMap.get("nested"), hasEntry("foo", "bar"));
// fails but clear
assertThat(outerMap, hasEntry("nested", hasEntry("foo", "bar")));
}
}
It seems the problem is the outer map is being compared using hasEntry(K key, V value) while what I want to use is hasEntry(Matcher<? super K> keyMatcher, Matcher<? super V> valueMatcher). I am not sure how to coerce the assertion to use the second form.
Thanks in advance.
If you only want to put Map<String, Object> as values in your outerMap adjust the declaration accordingly. Then you can do
#Test
public void testMapHasMap() {
Map<String, Map<String, Object>> outerMap = new HashMap<>();
Map<String, Object> nestedMap = new HashMap<String, Object>();
nestedMap.put("foo", "bar");
outerMap.put("nested", nestedMap);
Object value = "bar";
assertThat(outerMap, hasEntry(equalTo("nested"), hasEntry("foo", value)));
}
Object value = "bar"; is necessary for compile reasons. Alternatively you could use
assertThat(outerMap,
hasEntry(equalTo("nested"), Matchers.<String, Object> hasEntry("foo", "bar")));
If You declare outerMap as Map<String, Map<String, Object>> you don't need the ugly cast. Like this:
public class MapContainsMapTest {
#Test
public void testMapHasMap() {
Map<String, Map<String, Object>> outerMap = new HashMap<>();
Map<String, Object> nestedMap = new HashMap<>();
nestedMap.put("foo", "bar");
outerMap.put("nested", nestedMap);
assertThat(outerMap.get("nested"), hasEntry("foo", "bar"));
}
}
I would probably extend a new Matcher for that, something like that (beware, NPEs lurking):
class SubMapMatcher extends BaseMatcher<Map<?,?>> {
private Object key;
private Object subMapKey;
private Object subMapValue;
public SubMapMatcher(Object key, Object subMapKey, Object subMapValue) {
super();
this.key = key;
this.subMapKey = subMapKey;
this.subMapValue = subMapValue;
}
#Override
public boolean matches(Object item) {
Map<?,?> map = (Map<?,?>)item;
if (!map.containsKey(key)) {
return false;
}
Object o = map.get(key);
if (!(o instanceof Map<?,?>)) {
return false;
}
Map<?,?> subMap = (Map<?,?>)o;
return subMap.containsKey(subMapKey) && subMap.get(subMapKey).equals(subMapValue);
}
#Override
public void describeTo(Description description) {
description.appendText(String.format("contains %s -> %s : %s", key, subMapKey, subMapValue));
}
public static SubMapMatcher containsSubMapWithKeyValue(String key, String subMapKey, String subMapValue) {
return new SubMapMatcher(key, subMapKey, subMapValue);
}
}
Try like this :
assertThat(nestedMap).contains(Map.entry("foo", "bar"));
assertThat(outerMap).contains(Map.entry("nested", nestedMap));

How to serialize with Jackson a java.util.Map based class

I have a class which looks like this:
#JsonFormat(shape=JsonFormat.Shape.OBJECT)
public class MyMap implements Map<String, String>
{
protected Map<String, String> myMap = new HashMap<String, String>();
protected String myProperty = "my property";
public String getMyProperty()
{
return myProperty;
}
public void setMyProperty(String myProperty)
{
this.myProperty = myProperty;
}
//
// java.util.Map mathods implementations
// ...
}
And a main method with this code:
MyMap map = new MyMap();
map.put("str1", "str2");
ObjectMapper mapper = new ObjectMapper();
mapper.getDeserializationConfig().withAnnotationIntrospector(new JacksonAnnotationIntrospector());
mapper.getSerializationConfig().withAnnotationIntrospector(new JacksonAnnotationIntrospector());
System.out.println(mapper.writeValueAsString(map));
When executing this code I'm getting the following output: {"str1":"str2"}
My question is why the internal property "myProperty" is not serialized with the map?
What should be done to serialize internal properties?
Most probably you will end up with implementing your own serializer which will handle your custom Map type. Please refer to this question for more information.
If you choose to replace inheritance with composition, that is to make your class to include a map field not to extend a map, then it is pretty easy to solve this using the #JsonAnyGetter annotation.
Here is an example:
public class JacksonMap {
public static class Bean {
private final String field;
private final Map<String, Object> map;
public Bean(String field, Map<String, Object> map) {
this.field = field;
this.map = map;
}
public String getField() {
return field;
}
#JsonAnyGetter
public Map<String, Object> getMap() {
return map;
}
}
public static void main(String[] args) throws JsonProcessingException {
Bean map = new Bean("value1", Collections.<String, Object>singletonMap("key1", "value2"));
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(map));
}
}
Output:
{"field":"value1","key1":"value2"}

why is nullpointerexception thrown in this code in Java?

I am unable to trace through why null pointer exception is thrown here, I am sure it is pretty simple, but somehow I am missing it. It is thrown when the checkoutBook method is called. Any help here?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Library {
HashMap<String, List<String>> checkoutBooks;
Library() {
HashMap<String, List<String>> checkoutBooks = new HashMap<String, List<String>>();
}
public void checkoutBook(String isbn, String patron) {
if (checkoutBooks.containsKey(isbn)) {
checkoutBooks.get(isbn).add(patron);
} else {
List<String> patronlist = new ArrayList<String>();
patronlist.add(patron);
checkoutBooks.put(isbn, patronlist);
System.out.println("hello");
}
}
public static void main(String[] args){
Library library = new Library();
library.checkoutBook("000", "Cay Horstman");
library.checkoutBook("000", "Sharron Morrow");
}
}
Because you didn't assign any value to this variable:
HashMap<String, List<String>> checkoutBooks;
You just defined new one in constructor. So remove that instance checkoutBooks variable, or do this:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Library {
HashMap<String, List<String>> checkoutBooks;
Library() {
checkoutBooks = new HashMap<String, List<String>>();
}
public void checkoutBook(String isbn, String patron) {
if (checkoutBooks.containsKey(isbn)) {
checkoutBooks.get(isbn).add(patron);
} else {
List<String> patronlist = new ArrayList<String>();
patronlist.add(patron);
checkoutBooks.put(isbn, patronlist);
System.out.println("hello");
}
}
public static void main(String[] args){
Library library = new Library();
library.checkoutBook("000", "Cay Horstman");
library.checkoutBook("000", "Sharron Morrow");
}
}
Because you didn't initialize the field checkoutBooks in the constructor. You initialized a local variable with the same name. Change your constructor to this:
HashMap<String, List<String>> checkoutBooks;
Sandbox() {
checkoutBooks = new HashMap<String, List<String>>();
}
In the Library constructor, you declare a local variable named checkoutBooks, and initialize it, but you don't initialize the same-named field at class level. Some suggestions:
At a minimum, to fix the error, change the constructor to do assignment only rather than declaring a new variable:
Library() {
checkoutBooks = new HashMap<String, List<String>>();
}
Since you're not reassigning the checkoutBooks field after construction, you can declare the field final. Then it will catch this sort of error immediately at compile time, because final fields are required to be initialized. It also prevents accidental reassignment of the field later, which enhances robustness.
class Library {
final HashMap<String, List<String>> checkoutBooks;
...
Since you don't do anything else in the constructor, you can inline the initialization directly into the field declaration:
class Library {
final HashMap<String, List<String>> checkoutBooks = new HashMap<String, List<String>>();
// no constructor
...
As of Java 7, you can avoid repeating the type arguments by using <>:
class Library {
final HashMap<String, List<String>> checkoutBooks = new HashMap<>();
checkoutBooks.containsKey
is null when you used HashMap<String, List<String>> checkoutBooks = new HashMap<String, List<String>>(); inside constructor, thats why getting Exception
class Library {
HashMap<String, List<String>> checkoutBooks;
Library() {
checkoutBooks = new HashMap<String, List<String>>(); // just change here
}
public void checkoutBook(String isbn, String patron) {
if (checkoutBooks.containsKey(isbn)) {
checkoutBooks.get(isbn).add(patron);
} else {
List<String> patronlist = new ArrayList<String>();
patronlist.add(patron);
checkoutBooks.put(isbn, patronlist);
System.out.println("hello");
}
}
public static void main(String[] args){
Library library = new Library();
library.checkoutBook("000", "Cay Horstman");
library.checkoutBook("000", "Sharron Morrow");
}
}

Categories

Resources