Nagendra Reddy
2 min readNov 18, 2021

Read Custom Object data in java

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ReadCustomObject {

public static class Employee {
public Employee(int employeeId, String employeeName) {
this.employeeId = employeeId;
this.employeeName = employeeName;
}

private int employeeId;
private String employeeName;
public int getEmployeeId() {
return employeeId;
}

public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}

public String getEmployeeName() {
return employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
}

public static class Person {
private int personId;

public Person(int personId, Employee employee) {
this.personId = personId;
this.employee = employee;
}

private Employee employee;

public int getPersonId() {
return personId;
}

public void setPersonId(int personId) {
this.personId = personId;
}

public Employee getEmployee() {
return employee;
}

public void setEmployee(Employee employee) {
this.employee = employee;
}
}


public static void main( String[] args )
{
Employee emp1 = new Employee(101,"one");
Employee emp2 = new Employee(102,"two");
Employee emp3 = new Employee(103,"three");
Employee emp4 = new Employee(104,"four");

Person p1 = new Person(201, emp1);
Person p2 = new Person(202, emp2);
Person p3 = new Person(203, emp3);
Person p4 = new Person(204, emp4);

List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);

List<Person> personList = new ArrayList<Person>();
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);


List<Integer> numbers =new ArrayList<>();
List<Employee> e =new ArrayList<>();

List<Employee> list = employeeList.stream().filter(employee -> Boolean.parseBoolean(employee.getEmployeeName())).collect(Collectors.toList());

employeeList.stream()
.map(employee -> numbers.add(employee.getEmployeeId()))
.collect(Collectors.toList());
System.out.println(employeeList.stream().collect(Collectors.toList()));
personList.stream()
.map(employee -> e.add(employee.getEmployee()))
.collect(Collectors.toList());
List<Integer> numbers1= e.stream().map(employee -> employee.getEmployeeId()).collect(Collectors.toList());



System.out.println(numbers);
System.out.println(numbers1);
}
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response