java - Class to define union of objects -
so.. have class, user, , user has set of books , want define class(es) , hierarchy between classes, if make union between 2 users, in result, want have new user, , set of books union between books 1 user, , books other.
thanks in advance
you can provide static method within user class, called unionuser(user first, user second) takes 2 user instances arguments , returns new user instance unioned book sets, accomplished invoking set#unionall() method.
public class user { private set<book> books; //provide accessors ... public static user unionuser(user first, user second) { user result = new user(); result.setbooks(first.getbooks().unionall(second.getbooks()); return result; } ... } there no duplicates in result book set, because, said in javadoc java.util.set interface:
a set collection cannot contain duplicate elements. models mathematical set abstraction.
Comments
Post a Comment