Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Properties and when Map in Java?

Tags:

java

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

like image 774
Shashank T Avatar asked Jun 04 '10 19:06

Shashank T


People also ask

What is difference Map and property?

Properties class allows String key/value pairs to read from or write to a stream. You may have have several application specific parameters to be read at the application startup you can utilize Properties class. HashMap is a collection class which allows to use key/value pairs of objects.

Why do we use properties in Java?

Properties is a subclass of Hashtable. It is used to maintain a list of values in which the key is a string and the value is also a string i.e; it can be used to store and retrieve string type data from the properties file. Properties class can specify other properties list as it's the default.

What is the property of Map in Java?

A Map cannot contain duplicate keys and each key can map to at most one value. Some implementations allow null key and null values like the HashMap and LinkedHashMap, but some do not like the TreeMap. The order of a map depends on the specific implementations.

What is the use of property files?

Standard system properties files and custom properties files are used to configure user preferences and user customization. A Java properties file defines the values of named resources that can specify program options such as database access information, environment settings, and special features and functions.


2 Answers

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

like image 147
Tansir1 Avatar answered Sep 19 '22 12:09

Tansir1


Properties is a Facade for Map<String,String> + some I/O methods.

Do you need the I/O methods ? use it : don't.

like image 28
Carl Avatar answered Sep 18 '22 12:09

Carl