java.util。Vs java.util属性。Map (复制)

时间:2021-11-13 19:33:43

This question already has an answer here:

这个问题已经有了答案:

Just wanted to know. What are the differences between java.util.Properties Vs java.util.HashMap<String, String>? Which is preferable?

只是想知道。java.util之间的区别是什么?Vs java.util属性。HashMap <字符串,字符串> ?哪个最好?

6 个解决方案

#1


11  

They are similar, but from a design perspective, the Properties class is considered to be one of the "mistakes" of java, because it is a Hashtable (rather than using a Hashtable). It should have been an interface.

它们是相似的,但是从设计的角度来看,属性类被认为是java的“错误”之一,因为它是一个Hashtable(而不是使用Hashtable)。它应该是一个接口。

Always use the abstract where possible, so this is preferred:

在可能的情况下总是使用摘要,所以这是首选的:

Map<String, String> stuff = new HashMap<String, String>();

Tend to avoid using Properties unless you have to.

除非必要,尽量避免使用属性。

#2


18  

There are different purpose of these two utility class. Map, or in your case, HashMap is general purpose storage object where you have unique key, and values to which they point. HashMap can have any object type as their key and any object type as their values.

这两个工具类有不同的用途。映射,或者在您的例子中,HashMap是通用存储对象,其中有惟一的键和它们指向的值。HashMap可以将任何对象类型作为键,任何对象类型作为值。

java.util.Properties is, however, a special purpose map. It is developed to read/write from/to properties files. It has special methods to do so [see load(..)]. Map does not.

java.util。然而,属性是一种特殊用途的地图。它被开发为从/到属性文件的读/写。它有特殊的方法来这样做[参见load(..)]。地图上没有。

So, you have different situations to use them. Places where you need properties to read, you better go with Properties. And places where you want to have lookup values stored off some logic, you go with HashMap<String, String>.

所以,你有不同的情况来使用它们。在需要读取属性的地方,最好使用属性。在需要从逻辑中存储查找值的地方,可以使用HashMap ,>

There is no hard and fast rule, you can use HashMap<String, String> and Properties interchangeably. But as an engineer, use right tool for the task.

没有硬性和快速规则,您可以交替使用HashMap 和属性。但是作为一名工程师,使用正确的工具来完成任务。 ,>

#3


2  

Propeties extends Hashtable, which is synchronized by default. HashMap is not synchronized by default. You're trading built-in thread safety for a slight performance improvement, one that you're likely to be unable to measure.

Propeties扩展了Hashtable,默认情况下是同步的。HashMap默认不同步。您正在使用内置的线程安全性来进行轻微的性能改进,您可能无法测量。

Properties is an older class from Java 1.0. HashMap is part of Joshua Bloch's newer collections API.

Properties是Java 1.0中的一个旧类。HashMap是Joshua Bloch更新的集合API的一部分。

#4


2  

You have the modern-day ConcurrentHashMap<String, String> for thread safety. Don't really use Properties unless you are using it to read a .properties file.

您拥有现代的ConcurrentHashMap 用于线程安全。不要真正使用属性,除非您正在使用它来读取. Properties文件。 ,>

#5


1  

The Properties class is an extension of Hashtable, basically adding functionality to write + read from a disk in a set format (text value pairs like this):

Properties类是Hashtable的扩展,基本上添加了以设置格式从磁盘写入+读取的功能(像这样的文本值对):

key1=value1
key2=value2

If you want to store it to disk and use that format then use a Properties, otherwise use a HashMap or Hashtable.

如果要将其存储到磁盘并使用该格式,则使用属性,否则使用HashMap或Hashtable。

#6


0  

java.util.Properties is a Hashtable<Object,Object> therefore you can see it as a synchronized form of java.util.HashMap<String, String>

java.util。属性是Hashtable ,因此您可以将其视为java.util的同步形式。HashMap <字符串,字符串> ,object>

Properties is nice to handle... properties : ) If you use it for other purposes, then your choice will depend on the concurrency management in your program

属性很好处理……属性:)如果您将它用于其他目的,那么您的选择将取决于程序中的并发管理

#1


11  

They are similar, but from a design perspective, the Properties class is considered to be one of the "mistakes" of java, because it is a Hashtable (rather than using a Hashtable). It should have been an interface.

它们是相似的,但是从设计的角度来看,属性类被认为是java的“错误”之一,因为它是一个Hashtable(而不是使用Hashtable)。它应该是一个接口。

Always use the abstract where possible, so this is preferred:

在可能的情况下总是使用摘要,所以这是首选的:

Map<String, String> stuff = new HashMap<String, String>();

Tend to avoid using Properties unless you have to.

除非必要,尽量避免使用属性。

#2


18  

There are different purpose of these two utility class. Map, or in your case, HashMap is general purpose storage object where you have unique key, and values to which they point. HashMap can have any object type as their key and any object type as their values.

这两个工具类有不同的用途。映射,或者在您的例子中,HashMap是通用存储对象,其中有惟一的键和它们指向的值。HashMap可以将任何对象类型作为键,任何对象类型作为值。

java.util.Properties is, however, a special purpose map. It is developed to read/write from/to properties files. It has special methods to do so [see load(..)]. Map does not.

java.util。然而,属性是一种特殊用途的地图。它被开发为从/到属性文件的读/写。它有特殊的方法来这样做[参见load(..)]。地图上没有。

So, you have different situations to use them. Places where you need properties to read, you better go with Properties. And places where you want to have lookup values stored off some logic, you go with HashMap<String, String>.

所以,你有不同的情况来使用它们。在需要读取属性的地方,最好使用属性。在需要从逻辑中存储查找值的地方,可以使用HashMap ,>

There is no hard and fast rule, you can use HashMap<String, String> and Properties interchangeably. But as an engineer, use right tool for the task.

没有硬性和快速规则,您可以交替使用HashMap 和属性。但是作为一名工程师,使用正确的工具来完成任务。 ,>

#3


2  

Propeties extends Hashtable, which is synchronized by default. HashMap is not synchronized by default. You're trading built-in thread safety for a slight performance improvement, one that you're likely to be unable to measure.

Propeties扩展了Hashtable,默认情况下是同步的。HashMap默认不同步。您正在使用内置的线程安全性来进行轻微的性能改进,您可能无法测量。

Properties is an older class from Java 1.0. HashMap is part of Joshua Bloch's newer collections API.

Properties是Java 1.0中的一个旧类。HashMap是Joshua Bloch更新的集合API的一部分。

#4


2  

You have the modern-day ConcurrentHashMap<String, String> for thread safety. Don't really use Properties unless you are using it to read a .properties file.

您拥有现代的ConcurrentHashMap 用于线程安全。不要真正使用属性,除非您正在使用它来读取. Properties文件。 ,>

#5


1  

The Properties class is an extension of Hashtable, basically adding functionality to write + read from a disk in a set format (text value pairs like this):

Properties类是Hashtable的扩展,基本上添加了以设置格式从磁盘写入+读取的功能(像这样的文本值对):

key1=value1
key2=value2

If you want to store it to disk and use that format then use a Properties, otherwise use a HashMap or Hashtable.

如果要将其存储到磁盘并使用该格式,则使用属性,否则使用HashMap或Hashtable。

#6


0  

java.util.Properties is a Hashtable<Object,Object> therefore you can see it as a synchronized form of java.util.HashMap<String, String>

java.util。属性是Hashtable ,因此您可以将其视为java.util的同步形式。HashMap <字符串,字符串> ,object>

Properties is nice to handle... properties : ) If you use it for other purposes, then your choice will depend on the concurrency management in your program

属性很好处理……属性:)如果您将它用于其他目的,那么您的选择将取决于程序中的并发管理