Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Maps and trivia questions : java generics
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
hanasaki  
View profile  
 More options Oct 28, 1:25 pm
From: hanasaki <hanas...@hanaden.com>
Date: Wed, 28 Oct 2009 12:25:02 -0500
Local: Wed, Oct 28 2009 1:25 pm
Subject: Maps and trivia questions : java generics
Below is the code from interface java.util.Map pulled from my favorite
IDE.  Anyone have comments of interest on the method "get" in the
interface as it pertains to proper use of Generics?  JDK 1.6

I have an idea... Just bouncing it off the list blindly for a
confirmation / sanity check.

Thanks!

///////

package java.util;

import java.util.Map.Entry;

public interface Map<K extends Object, V extends Object> {

     public static interface Entry<K extends Object, V extends Object> {

         public K getKey();

         public V getValue();

         public V setValue(V v);

         public boolean equals(Object o);

         public int hashCode();
     }

     public int size();

     public boolean isEmpty();

     public boolean containsKey(Object o);

     public boolean containsValue(Object o);

     public V get(Object o);

     public V put(K k, V v);

     public V remove(Object o);

     public void putAll(Map<? extends K, ? extends V> map);

     public void clear();

     public Set<K> keySet();

     public Collection<V> values();

     public Set<Entry<K, V>> entrySet();

     public boolean equals(Object o);

     public int hashCode();


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hanasaki  
View profile  
 More options Oct 28, 1:31 pm
From: hanasaki <hanas...@hanaden.com>
Date: Wed, 28 Oct 2009 12:31:08 -0500
Local: Wed, Oct 28 2009 1:31 pm
Subject: Re: Maps and trivia questions : java generics
actually...
        get, containsKey and containsValue

--

=================================================================
= Management is doing things right;                             =
= Leadership is doing the right things.    - Peter Drucker      =
=================================================================


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan McCullough  
View profile  
 More options Oct 28, 1:38 pm
From: Ryan McCullough <rheag...@gmail.com>
Date: Wed, 28 Oct 2009 12:38:41 -0500
Local: Wed, Oct 28 2009 1:38 pm
Subject: Re: [kcjava] Maps and trivia questions : java generics

Are u asking why the get method accepts arg of Object insted of
K? Please be more specific.

On Oct 28, 2009 12:25 PM, "hanasaki" <hanas...@hanaden.com> wrote:

Below is the code from interface java.util.Map pulled from my favorite
IDE.  Anyone have comments of interest on the method "get" in the
interface as it pertains to proper use of Generics?  JDK 1.6

I have an idea... Just bouncing it off the list blindly for a
confirmation / sanity check.

Thanks!

///////

package java.util;

import java.util.Map.Entry;

public interface Map<K extends Object, V extends Object> {

    public static interface Entry<K extends Object, V extends Object> {

        public K getKey();

        public V getValue();

        public V setValue(V v);

        public boolean equals(Object o);

        public int hashCode();
    }

    public int size();

    public boolean isEmpty();

    public boolean containsKey(Object o);

    public boolean containsValue(Object o);

    public V get(Object o);

    public V put(K k, V v);

    public V remove(Object o);

    public void putAll(Map<? extends K, ? extends V> map);

    public void clear();

    public Set<K> keySet();

    public Collection<V> values();

    public Set<Entry<K, V>> entrySet();

    public boolean equals(Object o);

    public int hashCode();


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hanasaki  
View profile  
 More options Oct 28, 11:32 pm
From: hanasaki <hanas...@gmail.com>
Date: Wed, 28 Oct 2009 20:32:25 -0700 (PDT)
Local: Wed, Oct 28 2009 11:32 pm
Subject: Re: Maps and trivia questions : java generics
Bingo.  Your thoughts?

 public boolean containsKey(Object o);
 public V remove(Object o);
 public boolean containsValue(Object o);
 public V get(Object key)

vs

 public boolean containsKey(K o);
 public V remove(K o);
 public boolean containsValue(V o);
 public V get(K key)

On Oct 28, 12:38 pm, Ryan McCullough <rheag...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan McCullough  
View profile  
 More options Oct 28, 11:46 pm
From: Ryan McCullough <rheag...@gmail.com>
Date: Wed, 28 Oct 2009 22:46:54 -0500
Local: Wed, Oct 28 2009 11:46 pm
Subject: Re: [kcjava] Re: Maps and trivia questions : java generics

I'm not really sure, but I'll bet that it is because the key compared by
memory address, making it completely unnecessary, and possibly excessive to
have a type definition. it might also save the compiler from needing to
perform type erasure.  If you are iterating over a keyList, then I could see
why you would want to use generics. if you are iterating over a keyList that
is of a type you have control of, and you really don't want to so something
like Foo foo = (Foo)iter.next(); then you could possibly override the
.equals() method to serve your purpose. of course you would only want to do
this unless you are not isolating by memory address.

Ryan McCullough


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Shekleton  
View profile  
 More options Oct 29, 6:14 pm
From: Kevin Shekleton <kevin.shekle...@gmail.com>
Date: Thu, 29 Oct 2009 17:14:32 -0500
Local: Thurs, Oct 29 2009 6:14 pm
Subject: Re: [kcjava] Re: Maps and trivia questions : java generics

This came up between a colleague and myself and here's StackOverflow to the
rescue:
http://stackoverflow.com/questions/1455138/java-generics-why-does-map...

On Wed, Oct 28, 2009 at 10:46 PM, Ryan McCullough <rheag...@gmail.com>wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan McCullough  
View profile  
 More options Oct 29, 6:32 pm
From: Ryan McCullough <rheag...@gmail.com>
Date: Thu, 29 Oct 2009 17:32:18 -0500
Local: Thurs, Oct 29 2009 6:32 pm
Subject: Re: [kcjava] Re: Maps and trivia questions : java generics

please summarize your opinion, that thing has 4 different points of view

Ryan McCullough
(816) 2000-816 - Home & Mobile

On Thu, Oct 29, 2009 at 5:14 PM, Kevin Shekleton
<kevin.shekle...@gmail.com>wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Shekleton  
View profile  
 More options Oct 29, 11:25 pm
From: Kevin Shekleton <kevin.shekle...@gmail.com>
Date: Thu, 29 Oct 2009 22:25:28 -0500
Local: Thurs, Oct 29 2009 11:25 pm
Subject: Re: [kcjava] Re: Maps and trivia questions : java generics

Take a look at the accepted answer, which also has the most up votes.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan McCullough  
View profile  
 More options Oct 29, 11:58 pm
From: Ryan McCullough <rheag...@gmail.com>
Date: Thu, 29 Oct 2009 22:58:53 -0500
Local: Thurs, Oct 29 2009 11:58 pm
Subject: Re: [kcjava] Re: Maps and trivia questions : java generics

Please para-phrase your opinion in the email thread, or be brandeds a hard
headed jerk. Not everyone here is able to view this link.

On Oct 29, 2009 10:25 PM, "Kevin Shekleton" <kevin.shekle...@gmail.com>
wrote:

Take a look at the accepted answer, which also has the most up votes.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Mitchell  
View profile  
 More options Oct 30, 12:32 am
From: David Mitchell <david.mitch...@gmail.com>
Date: Thu, 29 Oct 2009 23:32:33 -0500
Local: Fri, Oct 30 2009 12:32 am
Subject: Re: [kcjava] Re: Maps and trivia questions : java generics

Easy there!

At least put a smiley down when you threaten to brand with the jerk iron.

Here is a quote for those of you viewing on a dial-up email only account:

<quote>

Uniformly, methods of the Java Collections Framework (and the Google
Collections Library too) never restrict the types of their parameters except
when it's necessary to prevent the collection from getting broken.

</quote>

--David

On Thu, Oct 29, 2009 at 10:58 PM, Ryan McCullough <rheag...@gmail.com>wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan McCullough  
View profile  
 More options Oct 30, 12:47 am
From: Ryan McCullough <rheag...@gmail.com>
Date: Thu, 29 Oct 2009 23:47:38 -0500
Local: Fri, Oct 30 2009 12:47 am
Subject: Re: [kcjava] Re: Maps and trivia questions : java generics

I don't know many technical ppl who smile when forced to repeat them
selves.  I hope this doesn't reflect poorly on my upcoming performance
review.

In what way is this relevant to generic parametrization and further, a
collection containing keys of a specific class.

I could see why in a scenario where the collection contains an assortment of
keys with different class names.

A thought to the origional propose of this thread, why would it be necessary
to have a maps generic keys set other
"? extends Object"

exp: HashMap<? extends Object, MyClass> foo = new HashMap<? extends Object,
MyClass>();

Its questionable why Sun even developed any generic key parametrization at
all since they are forcing us to cast extracted keys.

On Thu, Oct 29, 2009 at 11:32 PM, David Mitchell
<david.mitch...@gmail.com>wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
luke w patterson  
View profile  
 More options Oct 30, 11:33 am
From: luke w patterson <lukewpatter...@gmail.com>
Date: Fri, 30 Oct 2009 08:33:29 -0700 (PDT)
Local: Fri, Oct 30 2009 11:33 am
Subject: Re: Maps and trivia questions : java generics
On Oct 29, 11:32 pm, David Mitchell <david.mitch...@gmail.com> wrote:

> Easy there!

> At least put a smiley down when you threaten to brand with the jerk iron.

It wasn't too flagrant, at least no one got gaftered  :) :)
http://gafter.blogspot.com/2006/11/reified-generics-for-java.html?sho...
:) :)

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Bloch on Generics (was: Re: Maps and trivia questions : java generics)" by michael
michael  
View profile  
 More options Nov 3, 2:04 am
From: michael <michael.1...@gmail.com>
Date: Mon, 2 Nov 2009 23:04:23 -0800 (PST)
Local: Tues, Nov 3 2009 2:04 am
Subject: Bloch on Generics (was: Re: Maps and trivia questions : java generics)

No one had mentioned it, so I'll chime in with Josh Bloch's recent
addition to his (growing) list of "Effective Java" tips, specifically
referring to Generics.  Apropos, & might be of interest to some -- but
doesn't address the *specific* issue at hand (*).

There's a video & a set of slides to review. A few puzzlers are thrown
in for good measure.
 * http://www.youtube.com/watch?v=V1vQf4qyMXg&fmt=22
 * http://files.meetup.com/1381525/still-effective.ppt.pdf

Specifically, to summarize, the recommendation regards parameters to
methods (nb: don't use wildcard types as return types). He categorizes
a method parameter as either a "producer" (giving data to the method)
or "consumer" (the method inserts things into the argument).
Consider a "Stack" class with methods:  pushAll(Collection src) and
popAll(Collection target).

As per Josh, remember "PECS":  "Producer extends, Consumer
super" (...not sure if that helps, but...)
* For a T producer, use Foo<? extends T>
* For a T consumer, use Foo<? super T>

Say you had the aforementioned Stack w/ bulk add (push) and get (pop)
methods. Instead of this:
 void pushAll(Collection<E> src); // producer, src is read from
 void popAll(Collection<E> dst); // consumer, dst is populated

You'd use:
 void pushAll(Collection<? extends E> src);
 void popAll(Collection<? super E> dst);

If there's a method that takes as an argument something that is both a
producer and a consumer, then you'd just have to use Collection<E>
(...the intersection of <? extends E> and <? super E>).  If the
argument is neither a producer nor a consumer (e.g., a functor?) then
you don't care what type it is: use Foo<?>.   (Note that I had been
using a "Collection" just to follow the example from above; I could
just as easily used "Foo" all over the place. The "producer" and
"consumer" analogy works better with a collection.)

The talk also includes more "effective" tips on: enum types, varargs,
concurrency, and [more] on serialization.

And, for something perhaps new to any old patterns wonks out there (I
know, not trendy anymore, unless you make them in the Cloud or
something): check out the "Typesafe Heterogeneous Container
Pattern" (idiom?) from the talk and/or slides (or google it, for a
more complete treatment).

-michael

(*)  i.e., "Schrödinger's Key". When in the map, it's simultaneously
two types at once: the type you put in, and the type you *will* use
when calling "get(Object)" (et al.).

On Oct 28, 9:25 am, hanasaki <hanas...@hanaden.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google