Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By jim829

Thread: Calling on an object in order to take the union

  1. #1
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Calling on an object in order to take the union

    How do I take the union between an object called on and the object passed in the parameter?

    I have an interface:

    Java Code:
    import java.util.Set;
    
        /**
         * An extended Set where additional functionality is added
         *
         * @version 3/23/14
        */
        public interface NewSet<T> extends Set<T> {
            /**
             * Calculates the set union with the given input
             *
             * @param set - the set to perform the operation along with
             * @return  the set representing the invoked set and the parameter
            */
             NewSet<T> union(NewSet<T> set);
    
             //and a bunch more methods ...
        }
    And I have a class that extends this interface:

    Java Code:
        import java.util.ArrayList;
        public class gSet<T> implements NewSet<T> {
            // ...
        }
    Now, the union method requires a NewSet object to be called on. The Union will be performed between the object called on and the object passed in the parameter, which is set. But I am confused on how to do this?
    How do I call on a NewSet object in order to take the union between the object called on and the object passed in?

    Do I just do:

    Java Code:
        public NewSet<T> union(NewSet<T> set) {
            NewSet<T> calledOn = new ArrayList<T>();
            // ...
        }
    Or is that totally wrong and I am not understanding what it means for an object to be called on?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Calling on an object in order to take the union

    There are two primary ways to do this (sort of like the Comparable and Comparator interfaces). One is to take two arguments (in your case of type Set or NewSet) and simply find the union of them and return a new set. The other is to take a single argument of a set. Then your NewSet comes into play. It has an internal set as part of the state of the NewSet class. It uses that and the passed in set to return a union of the two. It returns a NewSet or Set as required. Note that your interface should probably be called Union which is implemented by a class called NewSet. At least than makes more sense to me.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by jim829 View Post
    There are two primary ways to do this (sort of like the Comparable and Comparator interfaces). One is to take two arguments (in your case of type Set or NewSet) and simply find the union of them and return a new set. The other is to take a single argument of a set. Then your NewSet comes into play. It has an internal set as part of the state of the NewSet class. It uses that and the passed in set to return a union of the two. It returns a NewSet or Set as required. Note that your interface should probably be called Union which is implemented by a class called NewSet. At least than makes more sense to me.

    Regards,
    Jim
    But doesn't
    Java Code:
    NewSet<T> union(NewSet<T> set);
    just mean it returns the object NewSet? Also, what do you mean by "It has an internal set as part of the state of the NewSet class"?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by Robben View Post
    But doesn't
    Java Code:
    NewSet<T> union(NewSet<T> set);
    just mean it returns the object NewSet? Also, what do you mean by "It has an internal set as part of the state of the NewSet class"?
    The internal set was simply an instance of some set implementation (i.e. composition). You could also just extend HashSet. In either case you take the union of the this instance of set with the passed set. But I would recommend just creating a simple functional class that takes the union of two sets.

    Java Code:
    class Union {
        public static <T> Set<T> getUnion(Set<T> set1, Set<T> set2) {
         // details here
           return nSet;
        }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by jim829 View Post
    The internal set was simply an instance of some set implementation (i.e. composition). You could also just extend HashSet. In either case you take the union of the this instance of set with the passed set. But I would recommend just creating a simple functional class that takes the union of two sets.

    Java Code:
    class Union {
        public static <T> Set<T> getUnion(Set<T> set1, Set<T> set2) {
         // details here
           return nSet;
        }
    }
    Regards,
    Jim
    I can't use HashSet, : ( I can only use ArrayList. I also, cant create the functional class you stated. The subclass of the interface can't be changed.

    Regarding "take the union of the this instance of set with the passed set" is this what you mean?

    Java Code:
    public NewSet<T> union(NewSet<T> set) {
        NewSet<T> calledOn = new ArrayList<T>(); //instance of NewSet
        // ...
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Calling on an object in order to take the union

    Sorry about that. I may have led you astray so I will try to explain better, using your first post as a guide.

    Your class, gSet implements your interface NewSet<T> (which extends Set and adds the method, union). Note, that convention says that class names should start with upper case letters.

    So, gSet is the actual set implementation.

    So gSet might look like this.

    Java Code:
    public class gSet<T> implements NewSet<T> {
        private List<T> list = new ArrayList<>(); // gSet is backed by an ArrayList
    
        public NewSet<T> union (NewSet<T> set)  {
         NewSet<T> retSet = new gSet<T>();
              // in here, you take the union of set and this.
             // using your available methods. Don't worry about ArrayList here, as it's not
             // necessary.  Then return your union of set and this. 
         return retSet;
        }
    
        // other NewSet<T> methods implemented here.
    }
    Note that the union method depends on gSet behaving as a set. So the union method may use the existing gSet methods. That may be true of other gSet methods too.

    I hope this helps some.

    Regards,
    Jim
    Last edited by jim829; 04-06-2015 at 05:36 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by jim829 View Post
    Sorry about that. I may have led you astray so I will try to explain better, using your first post as a guide.

    Your class, gSet implements your interface NewSet<T> (which extends Set and adds the method, union). Note, that convention says that class names should start with upper case letters.

    So, gSet is the actual set implementation.

    So gSet might look like this.

    Java Code:
    public class gSet<T> implements NewSet<T> {
        private List<T> list = new ArrayList<>(); // gSet is backed by an ArrayList
    
        public NewSet<T> union (NewSet<T> set)  {
         NewSet<T> retSet = new gSet<T>();
              // in here, you take the union of set and this.
             // using your available methods. Don't worry about ArrayList here, as it's not
             // necessary.  Then return your union of set and this. 
         return retSet;
        }
    
        // other NewSet<T> methods implemented here.
    }
    Note that the union method depends on gSet behaving as a set. So the union method may use the existing gSet methods. That may be true of other gSet methods too.

    I hope this helps some.

    Regards,
    Jim
    Thank you very much, that is much more clear. Just a last few questions, what will I use list for? Also, you said to take the union of set and this, by "this" do you mean the keyword this or retSet?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by Robben View Post
    Thank you very much, that is much more clear. Just a last few questions, what will I use list for? Also, you said to take the union of set and this, by "this" do you mean the keyword this or retSet?
    list is the internal ArrayList that backs up the Set. You could also extend ArrayList since most of the methods of the Set interface are implemented by ArrayList. The "this" keyword, is talking about the current instance of gSet. Remember, you are passing a set to union. The other part of that union, gSet, is the set instance that is calling the union method. If you have ever implemented the equals() method it is the same thing. When you pass some object to equals, you are checking to see if the invoking instance of equals, is equal to the passed object.

    retSet is a new instance of NewSet<T>. The idea is to not alter the contents of either the passed in set or the invoking instance. So create a new Set and make the changes (i.e. create the union) there.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by jim829 View Post
    list is the internal ArrayList that backs up the Set. You could also extend ArrayList since most of the methods of the Set interface are implemented by ArrayList. The "this" keyword, is talking about the current instance of gSet. Remember, you are passing a set to union. The other part of that union, gSet, is the set instance that is calling the union method. If you have ever implemented the equals() method it is the same thing. When you pass some object to equals, you are checking to see if the invoking instance of equals, is equal to the passed object.

    retSet is a new instance of NewSet<T>. The idea is to not alter the contents of either the passed in set or the invoking instance. So create a new Set and make the changes (i.e. create the union) there.

    Regards,
    Jim
    Hm, I don't think I understand. Whenever I think of this, i think of constructors, instance variables and methods that need to be used in some specified method but I am not sure how this will be used here? What I was thinking before was that retSet was my calling object that I will take the union with the parameter set, but from what I understand from your current post is that retSet is just an empty set where the union of the passed in set and invoked instance will be placed in. So in order to take the union of a set you need at least two sets, here one of my sets will be the parameter set that is passed in, but then how do I create the invoking instance by using this?

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Calling on an object in order to take the union

    Here is what the call would look like. Let's assume your sets are Integers

    Java Code:
    NewSet<Integer> set1 = new gSet<>(); // pretend this is populated with some integers.
    NewSet<Integer> set2 = new gSet<>(); // pretend this is populated with some integers.
    
    NewSet<Integer> unionSet = set1.union(set2);
    unionSet is now a union of set1 and set2. Ideally, neither set1 nor set2 will have been (or needs to be) altered. It could also have been invoked the other way.

    Java Code:
    NewSet<Integer> unionSet = set2.union(set1);
    In each case above, one set is taking the union of itself and another set.

    Regards,
    Jim
    Last edited by jim829; 04-06-2015 at 10:02 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by jim829 View Post
    Here is what the call would look like. Let's assume your sets are Integers

    Java Code:
    NewSet<Integer> set1 = new gSet<>(); // pretend this is populated with some integers.
    NewSet<Integer> set2 = new gSet<>(); // pretend this is populated with some integers.
    
    NewSet<Integer> unionSet = set1.union(set2);
    unionSet is now a union of set1 and set2. Ideally, neither set1 nor set2 will have been (or needs to be) altered. It could also have been invoked the other way.

    Java Code:
    NewSet<Integer> unionSet = set2.union(set1);
    In each case above, one set is taking the union of itself and another set.

    Regards,
    Jim
    Hm, so I have to do a typecast, i.e. something like NewSet<T> otherSet = (NewSet<T>) set?
    Last edited by Robben; 04-06-2015 at 10:23 PM.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by Robben View Post
    Hm, so I have to do a typecast, i.e. something like NewSet<T> otherSet = (NewSet<T>) set?
    No. The generics takes care of that. As long as you implement generics correctly, you can assign an instance of a class to any interface type that class implements. The examples I gave in my previous post compile without errors or warnings and are typesafe.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by jim829 View Post
    No. The generics takes care of that. As long as you implement generics correctly, you can assign an instance of a class to any interface type that class implements. The examples I gave in my previous post compile without errors or warnings and are typesafe.

    Regards,
    Jim
    So I need to make a constructor that makes list null and then do something like?

    Java Code:
    public NewSet<T> union(NewSet<T> set) {
        NewSet<T> retSet = new gSet<T>();
        for(int i=0;i<set.size();i++) {
            if (this.list(set.contains.get(i))) {
                // ...      
            }
        }       
        return new retSet;
    }

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Calling on an object in order to take the union

    I recommend you simply get your basic gSet working. Either implement the Set interface or extend ArrayList. The key point is that Sets in Java don't allow duplicates so provide contructors to create an empty set and one that takes some list and removes the duplicates. Once you get that working, add the union method.

    Regards,
    Jim
    Robben likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Calling on an object in order to take the union

    I ran into an issue. When I was done implementing all the methods into class, the compiler told me that my class isn't abstract and I must implement the methods addAll(), clear(), retainAll() and more. But I don't want to override those methods. Is there any way I don't have to override them?

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by Robben View Post
    I ran into an issue. When I was done implementing all the methods into class, the compiler told me that my class isn't abstract and I must implement the methods addAll(), clear(), retainAll() and more. But I don't want to override those methods. Is there any way I don't have to override them?
    Yes. There are several ways. First, you could simple extend ArrayList. If you look at the Set interface and the List interface you will see they are similar (perhaps the same). And the contract for the interface is the same in most if not all cases. So you could just use the methods from the ArrayList. Where you need to be careful is where you detect duplicates.

    Another way is to use composition. Have the ArrayList as a private instance field. Then create a forwarding class that implements the Set interface and use your implemented methods to forward the call to the ArrayList method when appropriate. Here is an example of the later (the generic syntax may need to be tweaked as I did not test it).

    Java Code:
    public MySet<T> extends ForwardingClass<T> {
         private List<T> list = new ArrayList<>();
          // constructors and other methods     
         public int size() {
             return list.size();
         }
    }
    
    class ForwardingClass<T> implements Set<T> {
        // contains all the dummy methods for Set<T> here
        public int size() {}
    }
    What's nice about this method is that you only need to create the fowarding class once. And unlike directly implementing the interface, you don't need to provide your own dummy methods. So its like an adapter class. Note: this idea comes from Joshua Bloch's Effective Java.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Calling on an object in order to take the union

    Quote Originally Posted by jim829 View Post
    Yes. There are several ways. First, you could simple extend ArrayList. If you look at the Set interface and the List interface you will see they are similar (perhaps the same). And the contract for the interface is the same in most if not all cases. So you could just use the methods from the ArrayList. Where you need to be careful is where you detect duplicates.

    Another way is to use composition. Have the ArrayList as a private instance field. Then create a forwarding class that implements the Set interface and use your implemented methods to forward the call to the ArrayList method when appropriate. Here is an example of the later (the generic syntax may need to be tweaked as I did not test it).

    Java Code:
    public MySet<T> extends ForwardingClass<T> {
         private List<T> list = new ArrayList<>();
          // constructors and other methods     
         public int size() {
             return list.size();
         }
    }
    
    class ForwardingClass<T> implements Set<T> {
        // contains all the dummy methods for Set<T> here
        public int size() {}
    }
    What's nice about this method is that you only need to create the fowarding class once. And unlike directly implementing the interface, you don't need to provide your own dummy methods. So its like an adapter class. Note: this idea comes from Joshua Bloch's Effective Java.

    Regards,
    Jim
    I see, so in my case I will have:


    Java Code:
    public GSet<T> extends ForwardingClass<T> {
         private List<T> list = new ArrayList<>();
         /**
           * Calculates the set union with the given input
           *
           * @param set - the set to perform the operation along with
           * @return  the set representing the invoked set and the parameter
         */
         NewSet<T> union(NewSet<T> set) { }
     
             //and a bunch more methods ..    
    
         public int size() {
             return list.size();
         }
    }
    
    class ForwardingClass<T> implements NewSet<T> {
        // contains all the dummy methods for Set<T> here
        public int size() {}
    
        public boolean  add(T set) {
    	}
    }
    But what do I do inside the brackets of the dummy methods? Do I leave them be? E.g.

    Java Code:
    public boolean  add(T set) {
        //do I put nothing in here?
    }

Similar Threads

  1. calling an object method from another object
    By manudude03 in forum New To Java
    Replies: 4
    Last Post: 10-26-2014, 02:53 AM
  2. calling object
    By storm88 in forum JCreator
    Replies: 0
    Last Post: 10-05-2011, 08:23 PM
  3. Replies: 1
    Last Post: 06-24-2011, 08:27 AM
  4. Replies: 9
    Last Post: 05-12-2011, 10:51 PM
  5. Calling an Object
    By 67726e in forum New To Java
    Replies: 8
    Last Post: 08-10-2010, 01:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •