Results 1 to 4 of 4
  1. #1
    Sliks is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default referring a unknown object in its subclass

    I am still quite new to java. So this maybe stupid, but I can't get over it. I am trying to override a method and a constructor from a class (HashMap). My problem is the following:

    In my constructor, I have a super(Map M) that take an argument M to call the constructor of HashMap(Map M). This superclass will use this M and create a hashmap. Now in my constructor, I want to copy this hashmap to a buffer. So I used:

    NewConstructor{
    super(M);
    HashMap Mbuf = new HashMap();
    Mbuf = super.putAll(???);
    .
    .
    .
    }

    then I don't know what to put in the argument of super.putAll(). It should be the hashmap created by the super(M) function, but I don't know how to reference it. I keep having trouble to referring this newly created hashmap. Is there any way to get over it.

    The reason I try to do this is that I would like to verify the map M has no duplicate values nor key, i.e. 1-1 mapping. And if there are some duplicated values, the constructor will eliminate them.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That code snippet you created is very incorrect. If you have Access to Map m, why not just do Mbuf.putAll(M)?

    You also may want to look into naming conventions, Mbuf should be mBuf.

    If this isn't your actual code, it would be helpful to see exactly what your code looks like.

  3. #3
    Sliks is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    Thank you for your reply. Mbuf.putAll(M) does work. but then it has other issues. the constructor UHM seems to fail to eliminate the duplicate value. Here is my code:

    public UHM(Map M){
    super(M);
    HashMap mBuf = new HashMap();
    mBuf.putAll(M);
    Set ss = mBuf.entrySet();
    Iterator iterr = ss.iterator();
    while (iterr.hasNext()) {
    Map.Entry entry = (Map.Entry) iterr.next();
    this.put(entry.getKey(), entry.getValue());
    }
    }

    public Object put(Object ke, Object va) {
    //if the value-key pair exist, remove that pair first
    if (containsValue(va)){
    Set s = entrySet();
    Iterator iter = s.iterator();
    while (iter.hasNext()) {
    Map.Entry entry = (Map.Entry) iter.next();
    if (entry.getValue().equals(va)){
    iter.remove();
    }
    }
    }
    return super.put(ke, va);
    }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I feel like it would be easiest to call super with no arguments, this will set up an empty hash map, the remove all duplicates from m, then use this.putAll(m)

    There may be better approaches, but I am unfamiliar withthe exact details of hashmap.

    Java Code:
    public UHM(Map m){
      super();
      loop
        remove duplicates from m
      end loop
      this.putAll(m);
    }

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  2. Object is a Subclass of
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 10:42 AM
  3. Adding unknown subclass
    By happy_hippie in forum AWT / Swing
    Replies: 5
    Last Post: 09-03-2009, 02:39 PM
  4. Replies: 0
    Last Post: 07-08-2009, 09:10 AM
  5. Replies: 1
    Last Post: 07-22-2008, 05:27 PM

Posting Permissions

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