0

Code

package com;

public class Employee {

    private String name;

    private String empId;

    public Employee(String name, String empId) {
        super();
        this.name = name;
        this.empId = empId;
    }

    // equals and hashcode

}


package com;

import java.util.LinkedHashSet;
import java.util.Set;

public class TestEmp {

    public static void main(String[] args) {
        Employee emp1 = new Employee("Luke", "11");
        Employee emp2 = new Employee("Luke2", "12");

        Set<Employee> empSet = new LinkedHashSet<>();

        empSet.add(emp1);
        empSet.add(emp2);

        Object obj[] = empSet.toArray();

        System.out.println(obj);
    }

}

When i print i get the response it is showing as [Ljava.lang.Object;@7852e922

I need response in form of an Employee Array , can you please help how to get this in form of an array

The response needs to form of an Array of Employees

Sri
  • 437
  • 1
  • 4
  • 13
Pavan
  • 77
  • 1
  • 9

3 Answers3

1

You would need to implement a toString method in the Employee class

public static class Employee {

        private String name;


        private String empId;

        public Employee(String name, String empId) {
            super();
            this.name = name;
            this.empId = empId;
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", empId='" + empId + '\'' +
                    '}';
        }
    }

Main

public static void main(String[] args){
        Employee emp1 = new Employee("Luke","11");
        Employee emp2 = new Employee("Luke2","12");

        Set<Employee> empSet = new LinkedHashSet<>();

        empSet.add(emp1);
        empSet.add(emp2);
        System.out.println(empSet);


    }

Output:

[Employee{name='Luke', empId='11'}, Employee{name='Luke2', empId='12'}]
bigbounty
  • 16,526
  • 5
  • 37
  • 65
  • maybe you should use `String.format();` for the `toString()` Method its more powerful and can be better readable. For this is case it would be: `return String.format( "Employee{name='%s', empId='%s'}", name, empId );` Its not necessary but its easier to understand whats happening because you instantly see the format of the output string and you can easily adapt the variable. Also you could add formatting like `%10s` so that this string no matter how many characters it is always takes 10 chars space – René Jul 22 '20 at 05:51
1

Override toString method in Employee class :

@Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", empId='" + empId + '\'' +
                '}';
    }

Use Arrays.toString to print in TestEmp class:

    System.out.println(Arrays.toString(obj));
Ankit Chauhan
  • 646
  • 6
  • 20
0

java.lang.Object;@7852e922 is the Object address of Object obj[] = empSet.toArray()

I give you an idea about using reflect to get value from Object Class and then store it into 2D arrays.

My idea does not need to overwrite toString() method but truely store value into an arrays.

I think empArray it is what you want.


import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

public class TestEmp {
    public static void main(String[] args) throws IllegalAccessException {
        // TODO Auto-generated method stub

        Employee emp1 = new Employee("Luke", "11");
        Employee emp2 = new Employee("Luke2", "12");

        Set<Employee> empSet = new LinkedHashSet<>();

        empSet.add(emp1);
        empSet.add(emp2);


        Object obj[] = empSet.toArray();

        Field[] fs = Employee.class.getDeclaredFields();
        String [][] empArray = new String[obj.length][fs.length];

        for(int i=0; i<obj.length; i++){
            Class a = (Class) obj[i].getClass();
            for(int j=0; j<fs.length; j++){
                Field f = fs[j];
                f.setAccessible(true);
                Object val = f.get(obj[i]);
                empArray[i][j] = (String) val;
            }
        }
        Arrays.asList(empArray).forEach(x -> System.out.println(x[0]+" "+x[1]));
    }
}

And the result is :

Luke 11
Luke2 12
dyy.alex
  • 514
  • 2
  • 4
  • 16