How can I iterate over a map of ?

What about entrySet() HashMap<String, Person> hm = new HashMap<String, Person>(); hm.put(“A”, new Person(“p1”)); hm.put(“B”, new Person(“p2”)); hm.put(“C”, new Person(“p3”)); hm.put(“D”, new Person(“p4”)); hm.put(“E”, new Person(“p5”)); Set<Map.Entry<String, Person>> set = hm.entrySet(); for (Map.Entry<String, Person> me : set) { System.out.println(“Key :”+me.getKey() +” Name : “+ me.getValue().getName()+”Age :”+me.getValue().getAge()); }

JSON consumer of polymorphic objects

Thanks to Eric’s comment pointing me to programmerbruce I managed to crack it. Here’s the code I used (cut down to simplify). public static class Info { @JsonProperty(“Product”) public String product; // Empty in the 0d version, One entry in the 1d version, two entries in the 2d version. @JsonProperty(“Dimensions”) public String[] dimensions; } public … Read more

What’s the advantage of POJO?

Taken from Wikipedia: POJO is an acronym for Plain Old Java Object. The name is used to emphasize that a given object is an ordinary Java Object, not a special object. A POJO is usually simple so won’t depend on other libraries, interfaces or annotations. This increases the chance that this can be reused in … Read more

How to convert POJO to JSON and vice versa?

Take a look at https://www.json.org Imagine that you have a simple Java class like this: public class Person { private String name; private Integer age; public String getName() { return this.name; } public void setName( String name ) { this.name = name; } public Integer getAge() { return this.age; } public void setAge( Integer age … Read more

Jackson JSON Deserialization with Root Element

edit: this solution only works for jackson < 2.0 For your case there is a simple solution: You need to annotate your model class with @JsonRootName(value = “user”); You need to configure your mapper with om.configure(Feature.UNWRAP_ROOT_VALUE, true); (as for 1.9) and om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); (for version 2). That’s it! @JsonRootName(value = “user”) public static class User … Read more

What is java pojo class, java bean, normal class? [duplicate]

Normal Class: A Java class Java Beans: All properties private (use getters/setters) A public no-argument constructor Implements Serializable. Pojo: Plain Old Java Object is a Java object not bound by any restriction other than those forced by the Java Language Specification. I.e., a POJO should not have to Extend prespecified classes Implement prespecified interface Contain … Read more

What is the difference between a JavaBean and a POJO?

A JavaBean follows certain conventions. Getter/setter naming, having a public default constructor, being serialisable etc. See JavaBeans Conventions for more details. A POJO (plain-old-Java-object) isn’t rigorously defined. It’s a Java object that doesn’t have a requirement to implement a particular interface or derive from a particular base class, or make use of particular annotations in … Read more

How to create a POJO?

A POJO is just a plain, old Java Bean with the restrictions removed. Java Beans must meet the following requirements: Default no-arg constructor Follow the Bean convention of getFoo (or isFoo for booleans) and setFoo methods for a mutable attribute named foo; leave off the setFoo if foo is immutable. Must implement java.io.Serializable POJO does … Read more