KBD

Keith Devens .com

Friday, September 5, 2008 Flag waving
Mistakes were made. – Ronald Reagan
← tagfsIBM developerWorks: Why FreeBSD →

Daily link icon Sunday, July 24, 2005

Java's (typed/generic) collections suck (update: no they don't)

Java's typed/generic collections suck are ok, see below:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TokenTest {
    public static void main(String[] args) {
        Map<String, List<String[]>> tokenMap = new HashMap<String, List<String[]>>();
        List tokenList = tokenMap.get("foo");
        for (String[] tokenTerms : tokenList){
            System.out.println(tokenTerms);
        }
    }
}

Then:

$>javac TokenTest.java
TokenTest.java:10: incompatible types
found   : java.lang.Object
required: java.lang.String[]
        for (String[] tokenTerms : tokenList){
                                   ^
1 error

Am I doing something wrong or is Java's typing stupid? Also note that Java didn't understand that ArrayList is a subtype of List when I tried to create a new HashMap<String, ArrayList<String[]>>();.

Update: Jürgen pointed out in a comment that List tokenList should have been List<String[]> tokenList. Problem solved. Thanks!

← tagfsIBM developerWorks: Why FreeBSD →

Comments XML gif

JK (http://www.jkdev.de) wrote:

tokenMap.get() returns not a List, but a List<String[]>. So that line should read

List<String[]> tokenList = tokenMap.get("foo");

if I am not completely mistaken. (but I am new to generics ... so ...:-)

∴ JK | 25-Jul-2005 1:21am est | http://www.jkdev.de | #7991

Keith (http://keithdevens.com/) wrote:

Yes! Thanks for pointing out my error. Just needed another pair of eyes. Thanks again Smiley

Keith | 25-Jul-2005 1:39am est | http://keithdevens.com/ | #7992

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

September 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
282930 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

new⇒I hate ASP.NET

CF, why pick that piece of trash?​Cold Confusion. Is it finally​really a OO...

ColdConfusion: Sep 5, 8:36pm

new⇒Maps of Iraq

This is for Linda, I will be​visiting that site some time in the​near futur...

Bob: Sep 5, 1:20pm

Girls, please don't get breast implants

Well alright I just read my above​comment and I wanted to add​this...I shou...

76.66.140.8: Sep 4, 7:31pm

Spider solitaire

I don't think the question was​necessarily if there are unbeatable​games.  ...

Jared: Sep 4, 12:44pm

Convert Pantone Colors to RGB and Hex - Color Conversion Chart

The colors on those website don't​seem to relate to the pantone data​we hav...

blah: Sep 3, 10:12am

Generated in about 0.177s.

(Used 8 db queries)

mobile phone