How to Convert a Java Object (Bean) to Key-Value Pairs (And Vice Versa)

How to convert a Java object (bean) to key-value pairs (and vice versa)?

There is always apache commons beanutils but of course it uses reflection under the hood

How to convert a Java object to key-value pair without using reflection

I've done this before by using a MapSerialisation interface:

interface MapSerialisation {
Map<String, Object> toMap();
void fromMap(Map<String, Object>);
}

Implement that on each of your model objects and use that for translation.

Another method I've used is Google Collections' Transformers, and having model transformation functions to convert between a Java object and a structure that Javascript would like.

I'd probably be tempted to use Jackson to serialise into JSON though.

Convert a POJO to a K,V map

BeanMap from Apache Commons does the job

Converting a POJO to Map

Post a simple version:

public final static Map<String, Object> pojo2Map(Object obj) {
Map<String, Object> hashMap = new HashMap<String, Object>();
try {
Class<? extends Object> c = obj.getClass();
Method m[] = c.getMethods();
for (int i = 0; i < m.length; i++) {
if (m[i].getName().indexOf("get") == 0) {
String name = m[i].getName().toLowerCase().substring(3, 4) + m[i].getName().substring(4);
hashMap.put(name, m[i].invoke(obj, new Object[0]));
}
}
} catch (Throwable e) {
//log error
}
return hashMap;
}

How to convert POJO to Map and vice versa in Java?

My use case is to convert any arbitrary POJO to Map and back from Map to POJO.

You could use Jackson, a popular JSON parser for Java:

ObjectMapper mapper = new ObjectMapper();

// Convert POJO to Map
Map<String, Object> map =
mapper.convertValue(foo, new TypeReference<Map<String, Object>>() {});

// Convert Map to POJO
Foo anotherFoo = mapper.convertValue(map, Foo.class);

According to the Jackson documentation, this method is functionally similar to first serializing given value into JSON, and then binding JSON data into value of given type, but should be more efficient since full serialization does not (need to) occur. However, same converters (serializers and deserializers) will be used as for data binding, meaning same object mapper configuration works.

loading key and value pairs in a file into a Properties object and print out all key and value pairs using Properties.list(PrintWriter p) method

You can try implementing the code in the below way:

            Properties p1 = new Properties();
InputStream is1 = new FileInputStream("File.txt");
p1.load(is1);
PrintWriter pw1 = new PrintWriter(System.out);
System.out.println("printing property values");
p1.list(pw1);
System.out.println(p1.getProperty("1"));
System.out.println(p1.getProperty("2"));

further to add more to your code,if you wish to print all the keys and values togehter,you can also choose to use enumeration in the below way:

        Properties p1 = new Properties();
InputStream is1 = new FileInputStream("File.txt");
p1.load(is1);
PrintWriter pw1 = new PrintWriter(System.out);
System.out.println("printing property values");
p1.list(pw1);
Enumeration<?> e = p1.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = p1.getProperty(key);
System.out.println("Key : " + key + ", Value : " + value);
}

This will get you all the keys and respective values together on the console.

Best way to convert a huge collection of key value pairs to C# object

It sounds like you should just build a dictionary:

var dictionary = pairs.ToDictionary(pair => pair.Key, pair => pair.Value);

Then you can look up any entry by key very efficiently. Note that this requires that all the keys are distinct. If there can be multiple values for a key, use ToLookup instead.



Related Topics



Leave a reply



Submit