5 Replies - 304 Views - Last Post: 07 April 2011 - 10:29 PM Rate Topic: -----

#1 beandip408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-November 10

what are these short put/get methods?

Posted 07 April 2011 - 09:04 PM

so i dont understand how a put method works very well. what is this put method doing?
public Object put(Object arg0, Object arg1) {	
		KeyValuePair kv = new KeyValuePair(arg0, arg1);
		KeyValuePair old = mySet.contains(kv);
		if(old != null)
		{
			Object val = old.getValue();
			old.setValue(arg1);
			return val;
		}
		mySet.add(kv);
		return null;



and same with this get method:

	public Object get(Object arg0) { 
		KeyValuePair kv = new KeyValuePair(arg0, null);
		kv = ((HashSetImpl)mySet).contains(kv);
		if(kv == null)
			return null;
		return kv.getValue();
	}



Is This A Good Question/Topic? 0
  • +

Replies To: what are these short put/get methods?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10574
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: what are these short put/get methods?

Posted 07 April 2011 - 10:03 PM

Based on your last thread, I'm getting the vibe that these are homework questions. These methods correspond well with the existing Java Collections Framework tools. Do a litle research first, then try to answer the question. We will be happy to clarify any misconceptions from there. :)
Was This Post Helpful? 0
  • +
  • -

#3 beandip408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-November 10

Re: what are these short put/get methods?

Posted 07 April 2011 - 10:09 PM

View Postmacosxnerd101, on 07 April 2011 - 10:03 PM, said:

Based on your last thread, I'm getting the vibe that these are homework questions. These methods correspond well with the existing Java Collections Framework tools. Do a litle research first, then try to answer the question. We will be happy to clarify any misconceptions from there. :)

its actually past homework assignments that i got wrong and a now just trying to figure out what they are since i didnt get them right the first time
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10574
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: what are these short put/get methods?

Posted 07 April 2011 - 10:12 PM

Showing us your good faith efforts can help us better clarify any misconceptions. :)
Was This Post Helpful? 0
  • +
  • -

#5 beandip408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-November 10

Re: what are these short put/get methods?

Posted 07 April 2011 - 10:17 PM

View Postmacosxnerd101, on 07 April 2011 - 10:12 PM, said:

Showing us your good faith efforts can help us better clarify any misconceptions. :)

so this was my orignal:
	public Object put(Object arg0, Object arg1) {	
		KeyValuePair kv = new KeyValuePair(arg0, arg1);
			return mySet.add(kv);
		}
		return null;


i thought i was returning the old value but apparently i was returning the new value. so the code i gave first was what it should have been
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10574
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: what are these short put/get methods?

Posted 07 April 2011 - 10:29 PM

Does your mySet.contains() method search based on the key only? Or the key and value? If it is the latter, your if statement in the original isn't necessary, b/c the value will already be set for that key.

If it is the former, then the put() method checks to see if the given Key exists. If it does, it updates the corresponding element associated with it (the value). Otherwise, it adds the KeyValuePair.

Also, were you supposed to write the code or examine code? If you're just examining code, try to explain what the code does. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1