How to convert object array to user defined object in java8

Suraj Bhandari

I have object array which contain some values. I want to convert this array of object to user defined custom class.

EX : Object[] obj = new Object[4];
     obj[0] = "one";
     obj[1] = "two"; 

It is possible to convert this object to Employee object by setting obj[0] to setFName and obj[1] to setLName using stream api of java8. I tried couple of ways but getting error.

output would be

Employee e = Stream.of(Obj).map().....

something like above

Anton Balaniuc

If you need to convert Object[] to Employee - just do it directly:

Employee e  = new Employee();
e.setFirstName(obj[0]);
e.setLastName(obj[1]);

You probably need a special constructor which accepts Object[]

public Employee(Object[] that){
    firstName = obj[0];
    lastName = obj[1];
}

If you really want to use something from java-8 for some reason, you might use Optional

Employee e = Optional.of(obj).map(o -> { 
     Employee tmp = new Employee(); 
     tmp.setFirstName(o[0]);
     tmp.setLastName(o[1]);
     return tmp; 
}).get();

But this doesn't give you any advantage over constructor and it is just more confusing. Even if you have the copy constructor variant with Optional make sense only if obj might be null:

Employee e = Optional.of(obj).map(o -> new Employee(o))
                     .orElseGet(() -> some default value);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How Can I Convert a Byte Array into A User-Defined Object?

How can we convert a string to a user defined object in java

Convert Stream object to json object and then to user defined object

Convert an List<object>to user defined class

How to convert Object to array of object

Convert Object to Array For a User In MongoDB

How to convert object array to string array in Java

How to convert Object in Array

How to convert this Object into an Array?

How to convert an object to an array?

How to Convert an Array into an Object

How to convert Array into object

Convert List<Object[]> to a user defined List objects List<EcomDto> in java 8

User-Defined Object In Array VBA

How to convert convert object to array

how to read such XML format "<loca:user>" in php and convert to array or object

How do i convert a json array object of an object into a java 8 optional list of that object

How to convert array of object to array of model object

How to convert byte array to Mat object in Java

Java: how to convert HashMap<String, Object> to array

How to Convert String Array JSON in a Java Object

How to convert a text file to array object? Java

How do I return a user-defined object in java?

How to convert Array Of Object A to Array Of Object B with Constructor of A Using Java 8?

How to convert nested object to array of object in javascript?

how to convert object to complicated array of object

How to convert array with nested object to object?

How to convert List<Object[]> to Map<String,BigInteger> with Streams & Lambda Java8

How to convert Array of Object to Custom Class Object (that is array to object)?