56

The java.util.Properties class is meant to represent a map where the keys and values are both Strings. This is because Properties objects are used to read .properties files, which are text files.

So, why in Java 5 did they retrofit this class to implement Map<Object,Object> and not Map<String,String>?

The javadoc states:

Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail.

Since the keys and values are both supposed to be Strings then why not enforce that statically by using the proper generic type?

I guess making Properties implement Map<String,String> would not be fully backward compatible with code written for pre-Java 5. If you have older code that sticks non-strings into a Properties object then that code would no longer compile with Java 5. But... isn't that a good thing? Isn't the whole point of generics to catch such type errors at compile time?

5 Answers 5

56

Because they did it in a hurry in the early days of Java, and didn't realise what the implications would be four versions later.

Generics were supposed to be part of the design of Java from the beginning, but the feature was dropped as being too complicated and, at the time, unnecessary. As a result, lots of code in the standard libraries is written with the assumption of non-generic collections. It took the prototype language "Pizza" from Martin Odersky to show how they could be done fairly well while maintaining near perfect backwards compatibility, with both Java code and bytecode. The prototype led to Java 5, in which the collections classes were retrofitted with generics in a way that allowed old code to keep working.

Unfortunately, if they were to retroactively make Properties inherit from Map<String, String>, then the following previously valid code would stop working:

Map<Object, Object> x = new Properties()
x.put("flag", true)

Why anybody would do that is beyond me, but Sun's commitment to backwards compatibility in Java has gone beyond heroic into the pointless.

What's now appreciated by most educated observers is that Properties should never have inherited from Map at all. It should instead wrap around Map, exposing only those features of Map that make sense.

Since reinventing Java, Martin Odersky has gone on to create the new Scala language, which is cleaner, inherits fewer mistakes, and breaks new ground in a number of areas. If you're finding Java's niggles annoying, take a look at it.

3
  • 1
    Actually Map x = new Properties() work either way. Its people putting things like properties.put("flag", Boolean.TRUE); I have seen people put all kinds of data into a Properties object, but never a non-String key. ;) May 17, 2009 at 5:27
  • 6
    Wait a minute! Map<Object,Object> x = new Properties() is not legal pre-Java 5, the <Object,Object> syntax was introduced in Java 5. So the statement "previously valid code" is incorrect. Jan 27, 2010 at 22:31
  • 1
    Read my sentence again. What I said is that they aren't able to fix it, because doing so would break people's code. Dec 28, 2010 at 11:56
27

It was originally intended that Properties would indeed extends Hashtable<String,String>. Unfortunately the implementation of bridge methods caused a problem. Properties defined in such a way causes javac to generate synthetic methods. Properties should define, say, a get method that returns a String but needs to override a method that returns Object. So a synthetic bridge method is added.

Suppose you had a class written in the bad old 1.4 days. You've overridden some methods in Properties. But what you haven't done is overridden the new methods. This leads to unintended behaviour. To avoid these bridge methods, Properties extends Hashtable<Object,Object>. Similarly Iterable does not return a (read-only) SimpleIterable, because that would have added methods to Collection implementations.

0
13

A one-liner (two-liner for no warnings) for creating Map from Properties:

@SuppressWarnings({ "unchecked", "rawtypes" })
Map<String, String> sysProps = new HashMap(System.getProperties());
2
  • Does not answer the question. But does answer what caused the question in the first place. Straight to the point. Jun 11, 2011 at 15:37
  • 2
    Is "rawtypes" necessary here? (At least in my IntelliJ IDEA, "unchecked" is sufficient for supressing any warnings.)
    – Jonik
    Apr 25, 2013 at 9:18
3

Backwards compatibility.

2

The reason: Liskov substitution principle and backwards compatibility. Properties extends Hashtable and thus must accept all messages that Hashtable would accept - and that means accepting put(Object, Object). And it has to extend plain Hashtable instead of Hashtable<String, String> because Generics were implemented in the downwards-compatibe way via type erasure, so once the compiler has done its thing, there are no generics.

1
  • 3
    I don't think the Liskov substitution principle has anything to do with it. Java happily lets you refine generic types when you inherit a class. That's why the <T extends Sometype> and <T super Sometype> syntax exists. May 16, 2009 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.