Java tips, observations, bugs and problems from the world of Spring, Weblogic, Oracle, MySQL and many other technologies...
Showing posts with label Collections. Show all posts
Showing posts with label Collections. Show all posts
Wednesday, 4 May 2011
Using Generic bounded wild-cards in an API
Yesterday’s blog touched on the strange aspect of bounded wild-card generics showing that you can create typed collections into which you can’t add anything except null. The example, covering Fruit classes, begs the question of why you can write ? extends Fruit.
Labels:
Collections,
Generics,
Java
Tuesday, 3 May 2011
Are Generics Overly Complicated?
Generics, introduced in Java 5, are widely used in Java programs today especially in the arena of collection classes. What are not so widely used and little understood by average programmers are generics that use bounded wildcards (and don’t forget that by definition nearly everyone is average).
Labels:
Collections,
Generics,
Java
Sunday, 1 May 2011
Null Collections and the Special Case Pattern
In my last blog I covered the Special Case Pattern demonstrating a special case NullEmployee object in an employee details scenario. What it doesn’t cover is the
method. This method must also adhere to the Special Case pattern and not return null when there’s no data available.
public List<Employee> getEmployeesBySurname(String surname)
Labels:
Collections,
Java,
Pattern
Tuesday, 22 March 2011
Collections that support the remove() method
The JSE Collection specification states that the implementation of remove() is optional and that if unimplemented an UnsupportedOperationException is thrown. So, I thought that it would be handy to know which collection classes support remove() and which don’t. Using the following code I get some interesting results...
Labels:
Collections,
Iterator,
Java
Monday, 21 March 2011
Iterator.remove() - a gotcha
The JSE Iterator specification states that the implementation of remove() is optional and that if unimplemented an UnsupportedOperationException is thrown. I suspect that this can, and does, lead to problems as best practice tells us that you should always used a collection interface as opposed to its concrete implementation...
Labels:
Collections,
encapsulation,
Iterator,
Java
Sunday, 20 March 2011
Collections that Support Iterator.remove()
The JSE Iterator specification states that the implementation of remove() is optional and that if unimplemented an UnsupportedOperationException is thrown. I suspect that this can, and does, lead to problems as best practice tells us that you should always used a collection interface as opposed to its concrete implementation...
Labels:
Collections,
Iterator,
Java
Saturday, 19 March 2011
CircularBuffer Unit Tests
Yesterday, I dumped the code for a CircularBuffer into this blog, with the idea being that a circular buffer is a collection class that’s missing from the JSE Collections framework.
Today, I’m adding the JUnit tests for that piece of code, just to prove it works! A few days ago I blogged about a Google Tech Talk given by Misko Hevery,
Today, I’m adding the JUnit tests for that piece of code, just to prove it works! A few days ago I blogged about a Google Tech Talk given by Misko Hevery,
Labels:
Collections,
Iterator,
Java,
JUnit,
Specifications,
Test Driven Development,
Unit Tests
Friday, 18 March 2011
A Circular Buffer
The other day, I came across one of those rare situations where I needed a collection class type that’s not supported by the JSE. I had the need to gather some statistics, and only needed to know the most recent 100 values... the sort of functionality that can easily be supplied by an old fashioned circular buffer.
Labels:
Collections,
Iterator,
Java,
Specifications
Monday, 7 February 2011
Using java.util.Collections - 2
Yesterday’s blog, contained a quick example of using Collections.reverse(), which, from experience, is something that you don’t have to do that often; so today’s post demonstrates a more useful example. Now, one thing you often need to do is to write a method that returns a Collection (eg a List, Set, Map etc.) to its caller.
Labels:
certification,
Collections,
encapsulation,
Java
Sunday, 6 February 2011
Using java.util.Collections - 1
One of the most useful classes is the java.util.Collections consisting of static methods that operate on or return collections. So if you need to reverse or sort a list, for example, then don't write the code yourself just look here!
Labels:
certification,
Collections,
Java
Subscribe to:
Posts (Atom)