1

we have

class Student
{
    String name,
    int age, 
    String specialization 
}

and

class Students
{
    List<String> names,
    List<Integer> age, 
    List<String> specialization
}

Students object is basically a structure that holds field values of Student class,

What is the best way to fill Students object without using reflection.

Edit: we have a specific requirement of having Students class as it is, the reason for this is we don't always want all the information in Student class and if we have List it would allocate memory for the fields that we are not interested in.

11
  • 7
    I think what you actually want is List<Student> students Commented Mar 20, 2017 at 6:38
  • Yes even I bi leave you need to have a list of Student object inside Students class like List<Student> students = new ArrayList<>(); and then iterate over students to fetch each elements from Student object Commented Mar 20, 2017 at 6:41
  • 1
    make it List<Students> student, And That will be fine, Commented Mar 20, 2017 at 6:41
  • we have a specific requirement of having Students class as it is, the reason for this is we don't always want all the information in Student class and if we have List<Student> it would allocate memory for the fields that we are not interested in. Commented Mar 20, 2017 at 6:46
  • than use a decorator pattern or inheritance, which enhances your object with the fields you want Commented Mar 20, 2017 at 6:57

5 Answers 5

2

Don't create class Students. Hold a list of Student

List<Student> students = new ArrayList<Student>();

And to access a student data you can use

students.get(0).name;

As a side note, you should learn about getters and setters.

Sign up to request clarification or add additional context in comments.

4 Comments

again Guy, we have a specific requirement of having Students class as it is, the reason for this is we don't always want all the information in Student class and if we have List<Student> it would allocate memory for the fields that we are not interested in.
@venkatg So List<String> names in Students should hold all the name field from all the Student instances?
@venkatg You should check again if this is really the requirement. How can you connect name with age for instance if you have them in two different lists? the correct way to do it is list (or any other data structure) of Student.
That is the requirement guy, we can connect name and age from the list using indices, we will ensure that when null values are present in Student class for a field we will set a value in the corresponding collection which represents a empty entity, thereby maintaining index sanity.
2

I wouldn't recommend creating a class named "Students" for this purpose. Your intention is to create a collection to hold the Student objects.

In this case, do the following:

List<Student> students = new ArrayList();

Also, pay attention to the capitalization: class is a keyword and should be spelled all lower-case.

EDIT After seeing a comment from venkat: If you really need to create a class called Students then following should work (also similar answer provided above by another SO user):

class Students {
    List<Student> students = new ArrayList();
}

This should work, but I would highly recommend not to use these type of class with the plural names!

PS: I am a CS prof teaching programming languages in a university and a long time developer/consultant.

1 Comment

the class names are for example, and I have added comments to my question, please take a look at it.
1
Class Students {    
   List<Student> students;
}

Comments

0

Maybe you want to use a Decorator-Pattern (I don't think that i saves memory):

Implement a base class with the default field:

public class BaseClass implements INameGettable {

    protected String name;

    public BaseClass(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Add the default interface:

public interface INameGettable {
    String getName();
}

Add a decorator to for an additional field e.g. age:

public class Decorator implements INameGettable {

    protected INameGettable nameable;
    protected int age;

    public Decorator(INameGettable nameable, int age) {
        this.nameable = nameable;
        this.age = age;
    }

    public String getName() {
        return nameable.getName();
    }

    public int getAge() {
        return this.age;
    }
}  

Usage:

// First object contains only name
INameable namegettable = new BaseClass("Test1");
namegettable.getName();

// Second object contains name and age
Decorator agegettable = new Decorator(new BaseClass("Test2"), 77);
agegettable.getName();
agegettable.getAge();

Comments

0

Going for the obvious answer here.

class Students
{
    List<String> names;
    List<Integer> age;
    List<String> specialization;

    public Student(List<Student> students) {
        addStudents(students);
    }


    private void addStudents(List<Student> students) {
        names = students.stream
                        .map(Student::getName)
                        .collect(Collectors.toList())
        age = students.stream
                      .map(Student::getAge)
                      .collect(Collectors.toList())
        specialization = students.stream
                                 .map(Student::getSpecialization)
                                 .collect(Collectors.toList())
   }
}

2 Comments

problem is that Students class is already implemented and it can only contain Lists.
Alright, well then you have to pass in a List of students as the parameter. But then you won't be able to add more students later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.