简体   繁体   中英

Set and HashSet Java

I'm relatively new to the Java language and have a project I'm doing for school in which I have a Book class that has the normal setters/getters, constructors, and overrides for this class, nothing complicated. I have to change it so I can get multiple authors by utilizing Set and HashSet. The question I have is how would I go about doing this? So far, and correct me if I'm wrong, I have this

import java.util.*;

public class Book{

       private Set<String> authorSet;
       private String isbn;

       public Book(){
           authorSet = null;
           isbn = null;
       }

       public Book(String isbn, Set<String> authorSet){
           this.isbn = isbn;
           Set<String> s = new HashSet<String>();

           // Do I do anything else here?
       }

       public String getIsbn(){
            return isbn;
       }

       public void setIsnb(String isbn){
            this.isbn = isbn;
       }

       public Set<String> getAuthorSet(Set<String> newAuthorSet{
            return newAuthorSet;
       }

       public void setAuthorSet(Set<String> newAuthorSet){
            this.authorSet = newAuthorSet;
       }

Before moving on to the overrides, I want to make sure I get this properly. I've tried to look for similar examples so I can see what's going on, but I haven't had much luck as of yet. I'm sure it's very simple, but I'm just starting to learn the language. Thanks for the help

First of all, in your default constructor, get rid of

authorSet = null;

and instead initialize your authorSet variable to a new HashSet. The reason for this is that you want to create the authorSet container regardless of whether any authors are added to begin with.

You'll probably want a constructor that takes just an isbn String. Also consider a constructor that takes isbn String and a variable number of Author Strings.


Ah, I missed this:

  public Book(String isbn, Set<String> authorSet){
       this.isbn = isbn;
       Set<String> s = new HashSet<String>();

       // Do I do anything else here?
   }

Not good as you ignore both the parameter and the field! Instead, assign the set parameter to the existing field as you would with any other field.

  public Book(String isbn, Set<String> authorSet){
       this.isbn = isbn;
       this.authorSet = authorSet;
   }

Then give your class an addAuthor(String author) method. Better for you to code this since this is homework. I really don't think that there's a whole lot more that you need with regards to this problem.

I'd have taken away the default constructor. Also, why would you need to set the authorSet? Wouldn't it better to just add and remove from it? Also why would you need to set the isbn. Could you instead just take it in the constructor, as I don't think you'd ever have to change it. How about something like this?

import java.util.HashSet;
import java.util.Set;

public class Book {

    private final Set<String> authorSet;
    private final String isbn;

    public Book(String isbn) {
        this.isbn = isbn;
        this.authorSet = new HashSet<>();
    }

    public String getIsbn() {
        return isbn;
    }

    public Set<String> getAuthorSet() {
        return authorSet;
    }

    public void addAuthor(String author) {
        authorSet.add(author);
    }

    public void removeAuthor(String author) {
        authorSet.remove(author);
    }
}

For extra points, the practice of returning your actual collection (set) implementation can allow a caller to muck with your internals. Thus, this is a little hazardous:

public Set<String> getAuthorSet() {
    return authorSet;
}

safer:

public Set<String> getAuthorSet() {
    return Collections.unmodifiableSet(authorSet);
}

Similarly if you had need to accept a new set in your API, but did not want to trust the caller to not later violate your representation, then you might do this:

public void setAuthorSet(Set<String> newAuthorSet) {
    authorSet = new HashSet<String>(newAuthorSet);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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