I have used this example to
Accessing Deeply nested HashMaps in Java
build the data structure to store node names and properties.
Here is the updated code:
class NestedMap {
private final HashMap<String, NestedMap> child;
private Map<String, Object> value = new HashMap<>();
public NestedMap() {
child = new HashMap<>();
setValue(null);
}
public boolean hasChild(String k) {
return this.child.containsKey(k);
}
public NestedMap getChild(String k) {
return this.child.get(k);
}
public void makeChild(String k) {
this.child.put(k, new NestedMap());
}
public Map<String, Object> getValue() {
return value;
}
public void setValue(Map<String, Object> value) {
this.value = value;
}
}
And my usage example:
class NestedMapIllustration {
public static void main(String[] args) {
NestedMap m = new NestedMap();
m.makeChild("de");
m.getChild("de").makeChild("content");
m.getChild("de").getChild("content").makeChild("00");
m.getChild("de").getChild("content").makeChild("0");
m.getChild("de").getChild("content").makeChild("1");
m.getChild("de").getChild("content").makeChild("01");
m.getChild("de").getChild("content").getChild("01").makeChild("fieldsets");
m.getChild("de").getChild("content").getChild("01").getChild("fieldsets").makeChild("0");
m.getChild("de").getChild("content").getChild("01").getChild("fieldsets").getChild("0").makeChild("fields");
m.getChild("de").getChild("content").getChild("01").getChild("fieldsets").getChild("0").getChild("fields").makeChild("0");
Map<String, Object> properties = new HashMap<>();
properties.put("key", "value");
properties.put("key2", "value");
m.getChild("de").getChild("content").getChild("01").getChild("fieldsets").getChild("0").getChild("fields").setValue(properties);
}
Instead of creating a new object each value I would like to always create a new HashMap where I can store the node properties.
I receive my data structure by visiting nodes in the JCR datastore and extracting their values and properties. This is how my resulting data structure should look in the output yaml file:
How can I do that more efficiently?
You've gone out of your way to let you use any key, but you're using string keys, even though one of the keys is "01" which suggests it's a number instead.
Can I conclude from this that keys are always strings?
In that case, why not define a separator, say, the slash, and use a plain old TreeMap<String, V>? Then you can do:
m.put("de/content/01/fieldsets/0/fields", properties);
If you want everything in the de/content/01 'tree', you can do:
m.subMap("de/content/01/", "de/content/010");
The above will give you a map containing every child of de/content/01. The 0 at the end of the 010 there is 'magic': Zero is the next character, after slash, in the ascii table.
If you want any given key to map to any number of values, you can use:
TreeMap<String, List<V>> map = new TreeMap<>();
to put things in:
map.computeIfAbsent(key, k -> new ArrayList<>()).add(elem);
and to get things out:
for (V value : map.getOrDefault(key, List.of())) {
// works even if key isn't in there (loops 0 times then)
}
Solution to the problem using recursion
public HashMap<String,Object> nestedMap(Node node) {
HashMap<String, Object> map = new LinkedHashMap<>();
PropertyIterator pi;
try {
pi = node.getProperties();
//Get properties for the root node
while(pi.hasNext())
{
Property p = pi.nextProperty();
String name = p.getName();
String val = p.getString();
map.put(name,val);
}//end of while for properties of root node
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterable<Node> children;
try {
children = NodeUtil.getNodes(node);
for (Node child : children) {
if (!child.getPrimaryNodeType().getName().contains("mgnl:page")) {
map.put (child.getName(), nestedMap(child));
}//end of checking if PrimaryNodeType is of type mgnl:page
}
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
Related
I am having a JSON which contains upto 1000 Keys. I need some specific keys out of it.
Rather than traversing through the JSON and finding key and put its value in required parameter.
I thought of doing it in other way.
I am creating a HashMap of the keys I need.
Now i want to pass a JSONObject through it, where if we find the keys in JSONObject, it will automatically update the HashMap with the required keys.
Is there some function given by Spring where we can do it easily or do I Have to loop through it.
For Example:
JSONObject:-
{
"a":"a",
"b":"b",
"c":"c",
"d":"d",
"e":"e",
}
HashMap that I created :-
Map<String, Object> keys = new HashMap<>();
keys .put("a", "");
keys .put("b", "");
I want a function where i would pass two params
function HashMap mapJsonToHashMap(HashMap, JSONObject) {
}
Returned HashMap would be :-
{
"a":"a",
"b":"b"
}
IMO 1000 keys are not a big deal, so I would go for a simple solution of deserialize it to an object, then just filter/map using streams. Something like:
ObjectMapper objectMapper = new ObjectMapper();
Set<String> keys = new HashSet<>();
keys.add("key-1");
keys.add("key-3");
List<Parameter> list =
objectMapper.readValue("[{ \"key\":\"key-1\", \"value\":\"aaa\" }, { \"key\":\"key-2\", \"value\":\"bbb\" }, { \"key\":\"key-3\", \"value\":\"ccc\" }]", new TypeReference<List<Parameter>>(){});
List<Parameter> filteredList = list.stream()
.filter(l -> keys.contains(l.getKey()))
.collect(Collectors.toList());
// in case you really want to put results in a Map
Map<String, String> KeyValueMap = filteredList.stream()
.collect(Collectors.toMap(Parameter::getKey, Parameter::getValue));
public class Parameter
{
private String key;
private String value;
public String getKey()
{
return key;
}
public void setKey(String key)
{
this.key = key;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
}
You can try something like this,
Convert JSON to HashMap
Then remove unwanted entries from the converted map
public HashMap mapJsonToHashMap(HashMap keys, JSONObject json) {
// Convert JSON to HashMap
ObjectMapper mapper = new ObjectMapper();
Map<String, String> jsonMap = mapper.readValue(json, Map.class);
// Iterate jsonMap and remove invalid keys
for(Iterator<Map.Entry<String, String>> it = jsonMap .entrySet().iterator();
it.hasNext(); ) {
Map.Entry<String, String> entry = it.next();
if(!keys.containsKey(entry.getKey())) {
it.remove();
}
}
return jsonMap;
}
I have two java classes:
public class MyClass1 {
private String userId;
private String userName;
private List<CustomList1> customList1;
// getters and setters
// inner CustomList1 class
}
public class MyClass2 {
private String userId;
private List<CustomList2> customList2;
// getters and setters
// inner CustomList2 class
}
Now, I have have lists of these classes:
List<MyClass1> classOneList;
List<MyClass2> classTwoList;
In both classOneList and classTwoList lists, object should be sorted with userId ascending. userId in both lists should have same values. What I want to check is that:
Has both lists same size? If not, thow error exception about.
Has every next element from both list the same userId? If not, throw another exception.
Step 1. I have done with simply if statement.
By prototype, step 2. should look like this:
for (el1, el2 : classOneList, classTwoList) {
el1.getUserId().isEqualTo(el2.getUserId());
}
Try the below code for your problem.
public class Testing {
public static void main(String[] args) {
Map<String, List<String>> map1 = new LinkedHashMap<String, List<String>>();
List<String> m1l1 = new LinkedList<String>();
m1l1.add("One");
m1l1.add("Two");
m1l1.add("Three");
m1l1.add("Four");
map1.put("1", m1l1);
List<String> m1l2 = new LinkedList<String>();
m1l2.add("One");
m1l2.add("Two");
m1l2.add("Three");
m1l2.add("Four");
map1.put("2", m1l2);
// Add more element into the map1 by creating more list.
Map<String, List<String>> map2 = new LinkedHashMap<String, List<String>>();
List<String> m2l1 = new LinkedList<String>();
m2l1.add("One");
m2l1.add("Two");
m2l1.add("Three");
m2l1.add("Four");
map2.put("1", m2l1);
// Add more element into the map2 by creating more list.
for (Entry<String, List<String>> entry : map1.entrySet()) {
if (map2.containsKey(entry.getKey())) {
if (entry.getValue().size() == map2.get(entry.getKey()).size()) {
} else {
System.out.println("UserId are same but list are different for userid: " + entry.getKey());
}
}
else {
System.out.println("Userid '"+entry.getKey()+"' exists in map1 but is not found in map2");
}
}
}
}
Hope this may help you.
if(classOneList.size() != classTwoList.size()){
throw new ErrorException();
}else{
classOneList = classOneList.stream().sorted(Comparator.comparing(MyClass1::getUserId)).collect(Collectors.toList());
classTwoList = classTwoList.stream().sorted(Comparator.comparing(MyClass2::getUserId)).collect(Collectors.toList());
for (int i = 0; i < classOneList.size(); i++){
if(!classOneList.get(i).getUserId().equals(classTwoList.get(i).getUserId())){
throw new AnotherErrorException();
}
}
}
I have a JSON which looks like,
{
"person": {
"name":"Sam",
"surname":"ngonma"
},
"car": {
"make":"toyota",
"model":"yaris"
}
}
I am writing this to Amazon SQS with the below lines,
ObjectMapper mObjectMapper = new ObjectMapper();
sqsExtended.sendMessage(new SendMessageRequest(awsSQSUrl, mObjectMapper.writeValueAsString(claim)));
I have a separate array of values, if the JSON has its value in that array I have to write the field as null.
If my string array is ["Sam", "Toyota"] my final JSON should look like this,
{
"person": {
"name":null,
"surname":"ngonma"
},
"car": {
"make":null,
"model":"yaris"
}
}
The string array is externalized. It may have additional values in future too. Could someone suggest me a good link or idea to address this ?
The most flexible way I could come up with is to use Jackson's JsonAnyGetter annotation. It allows you to provide Jackson with a Map representation of the state of your pojo. filtering values from a Map can be done in iterative way. filtering values from a Map that contains Maps can be done in recursive way.
Here is a solution I built from provided question
public class Claim {
Map<String, Object> properties = new HashMap<>();
public Claim() {
// may be populated from instance variables
Map<String, String> person = new HashMap<>();
person.put("name", "Sam");
person.put("surname", "ngonma");
properties.put("person", person);
Map<String, String> car = new HashMap<>();
car.put("make", "Toyota");
car.put("model", "yaris");
properties.put("car", car);
}
// nullify map values based on provided array
public void filterProperties (String[] nullifyValues) {
filterProperties(properties, nullifyValues);
}
// nullify map values of provided map based on provided array
#SuppressWarnings("unchecked")
private void filterProperties (Map<String, Object> properties, String[] nullifyValues) {
// iterate all String-typed values
// if value found in array arg, nullify it
// (we iterate on keys so that we can put a new value)
properties.keySet().stream()
.filter(key -> properties.get(key) instanceof String)
.filter(key -> Arrays.asList(nullifyValues).contains(properties.get(key)))
.forEach(key -> properties.put(key, null));
// iterate all Map-typed values
// call this method on value
properties.values().stream()
.filter(value -> value instanceof Map)
.forEach(value -> filterProperties((Map<String, Object>)value, nullifyValues));
}
// provide jackson with Map of all properties
#JsonAnyGetter
public Map<String, Object> getProperties() {
return properties;
}
}
test method
public static void main(String[] args) {
try {
ObjectMapper mapper = new ObjectMapper();
Claim claim = new Claim();
claim.filterProperties(new String[]{"Sam", "Toyota"});
System.out.println(mapper.writeValueAsString(claim));
} catch (Exception e) {
e.printStackTrace();
}
}
output
{"car":{"model":"yaris","make":null},"person":{"surname":"ngonma","name":null}}
It may be a bad practice, but I haven't been able to figure out any better solution for my problem. So I have this map
// Map<state, Map<transition, Map<property, value>>>
private Map<String, Map<String, Map<String, String>>> properties;
and I want to initialize it so I don't get NullPointerException with this
properties.get("a").get("b").get("c");
I tried this one but I didn't work (obviously)
properties = new HashMap<String, Map<String, Map<String,String>>>();
Other things I tried didn't compile.
Also if you have any ideas how to avoid this nested maps, I would appreciate it.
It seems to me that you need to create your own Key class:
public class Key {
private final String a;
private final String b;
private final String c;
public Key(String a, String b, String c) {
// initialize all fields here
}
// you need to implement equals and hashcode. Eclipse and IntelliJ can do that for you
}
If you implement your own key class, your map will look like this:
Map<Key, String> map = new HashMap<Key, String>();
And when looking for something in the map you can use:
map.get(new Key("a", "b", "c"));
The method above will not throw a NullPointerException.
Please remember that for this solution to work, you need to override equals and hashcode in the Key class. There is help here. If you don't override equals and hashcode, then a new key with the same elements won't match an existing key in the map.
There are other possible solutions but implementing your own key is a pretty clean one in my opinion. If you don't want to use the constructor you can initialize your key with a static method and use something like:
Key.build(a, b, c)
It is up to you.
You need to put maps in your maps in your map. Literally:
properties = new HashMap<String, Map<String, Map<String,String>>>();
properties.put("a", new HashMap<String, Map<String,String>>());
properites.get("a").put("b", new HashMap<String,String>());
If your target is lazy initialization without NPE you have to create your own map:
private static abstract class MyMap<K, V> extends HashMap<K, V> {
#Override
public V get(Object key) {
V val = super.get(key);
if (val == null && key instanceof K) {
put((K)key, val = create());
}
return val;
}
protected abstract V create();
}
public void initialize() {
properties = new MyMap<String, Map<String, Map<String, String>>>() {
#Override
protected Map<String, Map<String, String>> create() {
return new MyMap<String, Map<String, String>>() {
#Override
protected Map<String, String> create() {
return new HashMap<String, String>();
}
};
}
};
}
You could use a utility method:
public static <T> T get(Map<?, ?> properties, Object... keys) {
Map<?, ?> nestedMap = properties;
for (int i = 0; i < keys.length; i++) {
if (i == keys.length - 1) {
#SuppressWarnings("unchecked")
T value = (T) nestedMap.get(keys[i]);
return value;
} else {
nestedMap = (Map<?, ?>) nestedMap.get(keys[i]);
if(nestedMap == null) {
return null;
}
}
}
return null;
}
This can be invoked like this:
String result = get(properties, "a", "b", "c");
Note that care is required when using this as it is not type-safe.
The only way to do it with this structure is to pre-initialise the 1st and 2nd level maps with ALL possible keys. If this is not possible to do you can't achieve what you are asking with plain Maps.
As an alternative you can build a custom data structure that is more forgiving. For example a common trick is for a failed key lookup to return an "empty" structure rather than null, allowing nested access.
You can't initialize this in one go, since you normally don't know what keys you'll have in advance.
Thus you'd have to check whether the submap for a key is null and if so you might add an empty map for that. Preferably you'd only do that when adding entries to the map and upon retrieving entries you return null if one of the submaps in the path doesn't exist. You could wrap that in your own map implementation for ease of use.
As an alternative, apache commons collections' MultiKeyMap might provide what you want.
It's impossible to use properties.get("a").get("b").get("c"); and be sure to avoid null unless you make your own Map. In fact, you can't predict that your map will contains "b" key.
So try to make your own class to handle nested get.
I think a better solution is using an object as the only key to the map of values. The key will be composed of three fields, state, transition and property.
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public class Key {
private String state;
private String transition;
private String property;
public Key(String state, String transition, String property) {
this.state = state;
this.transition = transition;
this.property = property;
}
#Override
public boolean equals(Object other) {
return EqualsBuilder.reflectionEquals(this, other);
}
#Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
}
When you check for a value, the map will return null for a key that is not associated with a value
Map<Key, String> values = new HashMap<Key, String>();
assert values.get(new Key("a", "b", "c")) == null;
values.put(new Key("a", "b", "c"), "value");
assert values.get(new Key("a", "b", "c")) != null;
assert values.get(new Key("a", "b", "c")).equals("value");
To efficiently and correctly use an object as a key in a Map you should override the methods equals() and hashCode(). I have built thos methods using the reflective functionalities of the Commons Lang library.
I think, following is the easier way:
public static final Map<Integer, Map<Integer, Map<Integer, Double>>> A_Map = new HashMap<Integer, Map<Integer, Map<Integer, Double>>>()
{
{
put(0, new HashMap<Integer, Map<Integer, Double>>()
{
{
put(0, new HashMap<Integer, Double>()
{
{
put(0, 1 / 60.0);
put(1, 1 / 3600.0);
}
});
put(1, new HashMap<Integer, Double>()
{
{
put(0, 1 / 160.0);
put(1, 1 / 13600.0);
}
});
}
});
put(1, new HashMap<Integer, Map<Integer, Double>>()
{
{
put(0, new HashMap<Integer, Double>()
{
{
put(0, 1 / 260.0);
put(1, 1 / 3600.0);
}
});
put(1, new HashMap<Integer, Double>()
{
{
put(0, 1 / 560.0);
put(1, 1 / 1300.0);
}
});
}
});
}
};
Using computeIfAbsent/putIfAbsent makes it simple:
private <T> void addValueToMap(String keyA, String keyB, String keyC, String value) {
map.computeIfAbsent(keyA, k -> new HashMap<>())
.computeIfAbsent(keyB, k -> new HashMap<>())
.putIfAbsent(keyC, value);
}
Is there a version of BeanUtils.describe(customer) that recursively calls the describe() method on the complex attributes of 'customer'.
class Customer {
String id;
Address address;
}
Here, I would like the describe method to retrieve the contents of the address attribute as well.
Currently, all I have can see the name of the class as follows:
{id=123, address=com.test.entities.Address#2a340e}
Funny, I would like the describe method to retrieve the contents of nested attributes as well, I don't understand why it doesn't. I went ahead and rolled my own, though. Here it is, you can just call:
Map<String,String> beanMap = BeanUtils.recursiveDescribe(customer);
A couple of caveats.
I'm wasn't sure how commons BeanUtils formatted attributes in collections, so i went with "attribute[index]".
I'm wasn't sure how it formatted attributes in maps, so i went with "attribute[key]".
For name collisions the precedence is this: First properties are loaded from the fields of super classes, then the class, then from the getter methods.
I haven't analyzed the performance of this method. If you have objects with large collections of objects that also contain collections, you might have some issues.
This is alpha code, not garunteed to be bug free.
I am assuming that you have the latest version of commons beanutils
Also, fyi, this is roughly taken from a project I've been working on called, affectionately, java in jails so you could just download it and then run:
Map<String, String[]> beanMap = new SimpleMapper().toMap(customer);
Though, you'll notice that it returns a String[], instead of a String, which may not work for your needs. Anyway, the below code should work, so have at it!
public class BeanUtils {
public static Map<String, String> recursiveDescribe(Object object) {
Set cache = new HashSet();
return recursiveDescribe(object, null, cache);
}
private static Map<String, String> recursiveDescribe(Object object, String prefix, Set cache) {
if (object == null || cache.contains(object)) return Collections.EMPTY_MAP;
cache.add(object);
prefix = (prefix != null) ? prefix + "." : "";
Map<String, String> beanMap = new TreeMap<String, String>();
Map<String, Object> properties = getProperties(object);
for (String property : properties.keySet()) {
Object value = properties.get(property);
try {
if (value == null) {
//ignore nulls
} else if (Collection.class.isAssignableFrom(value.getClass())) {
beanMap.putAll(convertAll((Collection) value, prefix + property, cache));
} else if (value.getClass().isArray()) {
beanMap.putAll(convertAll(Arrays.asList((Object[]) value), prefix + property, cache));
} else if (Map.class.isAssignableFrom(value.getClass())) {
beanMap.putAll(convertMap((Map) value, prefix + property, cache));
} else {
beanMap.putAll(convertObject(value, prefix + property, cache));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return beanMap;
}
private static Map<String, Object> getProperties(Object object) {
Map<String, Object> propertyMap = getFields(object);
//getters take precedence in case of any name collisions
propertyMap.putAll(getGetterMethods(object));
return propertyMap;
}
private static Map<String, Object> getGetterMethods(Object object) {
Map<String, Object> result = new HashMap<String, Object>();
BeanInfo info;
try {
info = Introspector.getBeanInfo(object.getClass());
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method reader = pd.getReadMethod();
if (reader != null) {
String name = pd.getName();
if (!"class".equals(name)) {
try {
Object value = reader.invoke(object);
result.put(name, value);
} catch (Exception e) {
//you can choose to do something here
}
}
}
}
} catch (IntrospectionException e) {
//you can choose to do something here
} finally {
return result;
}
}
private static Map<String, Object> getFields(Object object) {
return getFields(object, object.getClass());
}
private static Map<String, Object> getFields(Object object, Class<?> classType) {
Map<String, Object> result = new HashMap<String, Object>();
Class superClass = classType.getSuperclass();
if (superClass != null) result.putAll(getFields(object, superClass));
//get public fields only
Field[] fields = classType.getFields();
for (Field field : fields) {
try {
result.put(field.getName(), field.get(object));
} catch (IllegalAccessException e) {
//you can choose to do something here
}
}
return result;
}
private static Map<String, String> convertAll(Collection<Object> values, String key, Set cache) {
Map<String, String> valuesMap = new HashMap<String, String>();
Object[] valArray = values.toArray();
for (int i = 0; i < valArray.length; i++) {
Object value = valArray[i];
if (value != null) valuesMap.putAll(convertObject(value, key + "[" + i + "]", cache));
}
return valuesMap;
}
private static Map<String, String> convertMap(Map<Object, Object> values, String key, Set cache) {
Map<String, String> valuesMap = new HashMap<String, String>();
for (Object thisKey : values.keySet()) {
Object value = values.get(thisKey);
if (value != null) valuesMap.putAll(convertObject(value, key + "[" + thisKey + "]", cache));
}
return valuesMap;
}
private static ConvertUtilsBean converter = BeanUtilsBean.getInstance().getConvertUtils();
private static Map<String, String> convertObject(Object value, String key, Set cache) {
//if this type has a registered converted, then get the string and return
if (converter.lookup(value.getClass()) != null) {
String stringValue = converter.convert(value);
Map<String, String> valueMap = new HashMap<String, String>();
valueMap.put(key, stringValue);
return valueMap;
} else {
//otherwise, treat it as a nested bean that needs to be described itself
return recursiveDescribe(value, key, cache);
}
}
}
The challenge (or show stopper) is problem that we have to deal with an object graph instead of a simple tree. A graph may contain cycles and that requires to develop some custom rules or requirements for the stop criteria inside the recursive algorithm.
Have a look at a dead simple bean (a tree structure, getters are assumed but not shown):
public class Node {
private Node parent;
private Node left;
private Node right;
}
and initialize it like this:
root
/ \
A B
Now call a describe on root. A non-recursive call would result in
{parent=null, left=A, right=B}
A recursive call instead would do a
1: describe(root) =>
2: {parent=describe(null), left=describe(A), right=describe(B)} =>
3: {parent=null,
{A.parent=describe(root), A.left=describe(null), A.right= describe(null)}
{B.parent=describe(root), B.left=describe(null), B.right= describe(null)}}
and run into a StackOverflowError because describe is called with objects root, A and B over and over again.
One solution for a custom implementation could be to remember all objects that have been described so far (record those instances in a set, stop if set.contains(bean) return true) and store some kind of link in your result object.
You can simple use from the same commom-beanutils:
Map<String, Object> result = PropertyUtils.describe(obj);
Return the entire set of properties for which the specified bean provides a read method.