When to use Properties and when Map in Java?

前端 未结 9 645
抹茶落季
抹茶落季 2020-12-16 15:29

Difference between Map and Properties as both have key-value pair.

相关标签:
9条回答
  • 2020-12-16 15:36

    A Properties object is a Map. See for example http://java.sun.com/javase/6/docs/api/java/util/Properties.html. I.e. the Properties class implements the Map interface.

    0 讨论(0)
  • 2020-12-16 15:39

    Properties is mainly used for based configuration data and localization, while Map is more general-purpose.

    0 讨论(0)
  • 2020-12-16 15:41

    According to the docs,

    • Map is an interface
    • Properties is a class that implements that interface
    0 讨论(0)
  • 2020-12-16 15:44

    Properties IS-A Map which IS-A Hashtable. Whether it should or not is a different question - I think it should really get the Map through composition and not implementing the Map interface.

    Properties class is for Properties files - that's why it has load methods on it to read the file in. So if you're working with properties files e.g.

    propa = bob
    propb = jane
    

    then use Properties. Otherwise you'll want to create your own Map interface and pick an appropiate implementation e.g. HashMap

    0 讨论(0)
  • 2020-12-16 15:46

    The Properties Class implements the Map-Interface. The Properties Class has Methods to save its content to a Stream.

    look at: http://java.sun.com/j2se/1.3/docs/api/java/util/Properties.html

    If you do not need to save the content, stay with a "normal" Map Implementation like HashMap

    0 讨论(0)
  • 2020-12-16 15:49

    A map is meant for normal key-value pair usage in code. Properties are typically used for storing and loading configuration values from a file. The underlying implementation of a Properties uses a Map.

    See the link below for a quick tutorial on how and when to use Properties.

    http://docs.oracle.com/javase/tutorial/essential/environment/properties.html

    0 讨论(0)
提交回复
热议问题