简体   繁体   English

用Java存储对象集合的最佳方法?

[英]Best way to store collection of objects in Java?

I am creating a dump Java app for student information system for learning and implementing OOPS Concepts like inheritance, abstraction, polymorphism and encapsulation. 我正在为学生信息系统创建一个转储Java应用程序,用于学习和实现OOPS概念,例如继承,抽象,多态性和封装。

What I am doing is, I have created Faculty Class, Student Class and a College Class. 我正在做的是,我创建了Faculty班, Student班和大学班。 Now i want to add new faculty in College. 现在,我想在学院中增加新的教师。 So my approach is to create a method in College class ie addFaculty(Faculty f) and fireFaculty(Faculty f) , now i want to add Faculties in College class. 所以我的方法是在大学班级中创建一个方法,即addFaculty(Faculty f)fireFaculty(Faculty f) ,现在我想在College班级中添加Faculties

Whats the best way to do it? 最好的方法是什么? How do i store list of Faculty Object in College Object. 如何将“ Faculty对象”列表存储在“ College对象”中。 Because i can add more than one faculty and more than one student in college. 因为我可以增加一个学院的一名以上教授和一名以上的学生。

Whats the best approach to solve this problem in OOPS? 解决OOPS中此问题的最佳方法是什么?

Here is College.java code which i have implemented, it works fine but is this the best way i can solve it? 这是我已经实现的College.java代码,可以正常工作,但这是我可以解决的最佳方法吗?

public class College 
{
String name;
String location;
String courses[];
HashMap<String,Faculty> faculties;
int noOfFaculties = 0;
int noOfStudents = 0;

public College(String name,String location,String courses[])
{
    this.name = name;
    this.location = location;
    this.courses = courses;
    faculties = new HashMap<>();
}

public void addFaculty(Faculty faculty)
{
    faculties.put(faculty.getName(),faculty);
}

public void printFaculties()
{
    Set<String> set = faculties.keySet();
    if(set.size()>0)
    {
        for(String s:set)
        {
            System.out.println(faculties.get(s).getName());
        }
    }
    else
    {
        System.out.println("No Faculties Currently Working");
    }
}

public void fireFaculty(Faculty faculty)
{
    faculties.remove(faculty.getName());
}
public String getName()
{
    return name;
}

public String getLocation()
{
    return location;
}

public String[] getCourses()
{
    return courses;
}
}

There's a ton of ways you can do it. 有很多方法可以做到。 Probably the easiest way to handle storing a collection of objects is by using one of the Collections provided by Java. 处理存储对象集合的最简单方法可能是使用Java提供的集合之一。 For beginners, probably the easiest one to understand is an ArrayList , which is basically an array that grows in size dynamically depending on the amount of objects in the collection. 对于初学者来说,可能最容易理解的是ArrayList ,它基本上是一个ArrayList ,其大小根据集合中对象的数量而动态增长。

So, as an axample, your code might be something like this: 因此,作为一个示例,您的代码可能是这样的:

public class College
{
    private ArrayList<Faculty> faculty;

    public College()
    {
        faculty = new ArrayList<Faculty>();
    }

    public void addFaculty(Faculty f)
    {
        faculty.add(f);
    }

    public void fireFaculty(Faculty f)
    {
        faculty.remove(f);
    }
}

If you cannot have duplicates use HashSet<Faculty> if you dont mind use a List<Faculty> . 如果您不能重复,则使用HashSet<Faculty>如果您不介意使用List<Faculty>

Example: 例:

class College {
    private List<Faculty> listFactories = new ArrayList<>();  // dupes allowed
    private Set<Faculty> setFactories = new HashSet<>(); // no dupes allowed
}

Check collections API . 检查集合API

imho It depends what kind of services College college offers. 恕我直言,这取决于大学所提供的服务类型。 If I were coding, I would start with:- 如果我正在编码,那么我将从:-

List<Faculy> faculties = new ArrayList<>();
....
public void addFaculty(Faculty f) {
  faculties.add(f);
 }
 //... etc 

And change to an altearnative later if needed. 并在需要时更改为替代品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM