- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
The Real WTF is that to get an array of type T you have to pass an array of type T to the toArray function.
Java generic types are just an over-engineered after thought.
Admin
Some of the Java library classes have methods where the caller must allocate their own array then pass it in to tell said method to write into it, like this was C or something. Perhaps this was intended to allow certain efficient or non-allocating usage of the API. (The Arrays class is full of this.)
Admin
Sad that getting the keys and values doesn't guarantee that they're in the same order. Languages like Python do guarantee this (as long as the mapping isn't mutated in between).
Admin
That's an awfully big assumption you have there.
Admin
Oh come on! We get the idiotic java code but not the mindboggling straight from hell in house programming language??? Please someone submit that instead!!!!
Admin
Ok the method which fills a supplied array should be called fill or copyTo, not toArray. Also the whole requirement has a code smell. Countries are a domain entity. This special built logic creating some custom in memory storage model is very suspect.
Admin
It's vacuously true. Every test that exists wasn't checking that. Every test that exists assumed incorrect behavior.
Admin
Leave it to Java to make C++ templates look elegant and simple.
Admin
Just one correction: The "entrySet" method returns a collection of entries, not sets.
Admin
You're straying into a contentious debate in the philosophy of logic and argument here.
Question: does "All X are Y" imply "There is at least one X"?
That debate.
In context: the awfully big assumption is "There were tests."
Admin
"Easy Reader Version: TRWTF is Java's toArray method, which- if you pass it an undersized buffer, WILL create an entirely new array, but if you pass it an accurately sized buffer, it won't"
How is that a WTF? What would you expect it to do with an undersized buffer? Do like C++ and overrun the buffer resulting in security flaws?
Admin
Not allowing the elements of the empty set to have a given property is like requiring 1 to be a prime number.
Admin
How about throw an exception?
Admin
All the built-in implementations do guarantee that (because the two collections are actually implemented as views over the collection of entries in the map) but some crazy mofo could do something else. The only saving grace is that they probably didn't as it's a lot more work to do it that way.
Admin
You are of course assuming the code was EVER tested ;-)
Admin
Java Generics were an afterthought, added in Java 5 (internal version 1.5). Since the class file / byte code format already had to be changed with that change, they should have done better than "reify" generic classes. Reification was a compromise to maintain source code backward compatibility.
It is possible to add a generic type on a method:
public Z[] toArray<Z>()
The problem is trying to allocate a new array of generic type Z (in Java you need an instance of Class to create an array, and Type is not quite the same thing). Another problem is that previous versions declared that toArray without any arguments would return an array of type Object.
They could have worked around this by passing in the class:
public Z[] toArray<Z>(Class<Z> clazz)
A smart implementation would return an array of type T, where T is the type of the Collection's elements:
public T[] toArray()
Alas, we only have a Type and not a Class, so we can't dynamically create the array!