views:

73

answers:

2
public class Student implements java.io.Serializable {

private long studentId;
private String studentName;
private Set<Course> courses = new HashSet<Course>(0);

public Student() {
}

public Student(String studentName) {
    this.studentName = studentName;
}

public Student(String studentName, Set<Course> courses) {
    this.studentName = studentName;
    this.courses = courses;
}

public long getStudentId() {
    return this.studentId;
}

public void setStudentId(long studentId) {
    this.studentId = studentId;
}

public String getStudentName() {
    return this.studentName;
}

public void setStudentName(String studentName) {
    this.studentName = studentName;
}

public Set<Course> getCourses() {
    return this.courses;
}

public void setCourses(Set<Course> courses) {
    this.courses = courses;
}

}

Here they are using Hashset to get the courses. My doubt is can i use a list to get the courses here. I read in internet that list get the vaues in a specified order and allows duplicates inside the list. whereas in set it doesnt have any order and wont allow duplicates. I want to know where i should uses sets and lists? Can anyone suggest?

+3  A: 

The fundamental difference between List and Set is (as you said) that Set does not allow duplicates, while List does. So in your case a Set is more appropriate, since a student should not be able to enroll in course twice. Actually, he should be able, but on in the same semester. So you may have each student have a set of CourseEnrollment objects, rather than Course objects.

Note that preserving order is not impossible for a Set - there are implementations (like LinkedHashSet) that preserve the order of elements, and other, like TreeSet, which keep to elements sorted.

Bozho
@bozho thanks i understood clearly and will follow it
Code 'N' Weed
@bozho if i get a collection using <address>private Address studentAddress;What does this mean? it is not a set it is another class addresss. How can i use this.What is this use?
Code 'N' Weed
I didn't understand.
Bozho
sorry i was not clear in my question.
Code 'N' Weed
+6  A: 

It feels like you answered your own question already. If you need a Collection of items, and you want the collection of items to have no duplicates, use a Set. You can use a SortedSet to impose ordering.

If your collection is allowed to have duplicates, then you can use a List. I think in your example, a Set works since a student would probably never take the same course twice.

SB
@SB thanks i understood clearly.
Code 'N' Weed
@SB if i get a collection using <address>private Address studentAddress;What does this mean? it is not a set it is another class addresss. How can i use this.What is this use?
Code 'N' Weed
Is it Collection<Address> or Set<Address> or is just Address studentAddress? If it's `private Address studentAddress` then you're just getting an Address object back. If it's Set<Address> then it's a Set of these Address objects, and if it's a Collection<Address> then it's just some collection (could be Set or List) and you're not supposed to care about the underlying implementation - just that it's a number of Address objects.
SB
@SB thanks i understood that i am using class name for getting objects. What is that "it's a number of Address objects" i cant get it.
Code 'N' Weed
When I say it's a number of Address objects, I mean it's a bunch of Address objects in some kind of container (or Collection as it's called in Java) - this could be either a List, or Set, or some other container. I highly suggest reading the tutorial posted in the comments of the question. It's a good read, and I think you would benefit from it.
SB