How do I make a copy of java.util.Properties object?

Michal Kordas :

I have the following field and constructor:

private final Properties properties;

public PropertiesExpander(Properties properties) {
    this.properties = properties;
}

The good practice is to make copy of every mutable collection in constructor. I want to make a shallow, independent copy. How can I achieve that?

My first idea was to use putAll() method:

private final Properties properties = new Properties();

public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

Is there any simpler, more performant or more idiomatic way of doing that? Maybe there are some utils for that in Guava or Apache Commons?

durron597 :

Using putAll() is great... if you need to stay with Properties. It runs in O(number of elements) and has very little overhead. The only difference I would recommend is to stay away from Properties for performance reasons unless you need it, because it inherits from Hashtable. Also, don't use Properties because it doesn't actually conform to any interface, just Dictionary which is an abstract class; this will limit your options. See: What does it mean to "program to an interface"?

As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.

Whatever you do, do not use clone(), it is not secure and not performant. See: Java: Why shouldn't clone() be used for defensive copying?


You edited your question to ask about Guava and apache-commons. If it's purely a defensive copy, and it's immutable, I would recommend using Map<String, String> map = ImmutableMap.copyOf(properties). Note: again, this doesn't use an actual Properties object because Hashtable is not recommended unless you need it. From the wiki

When you don't expect to modify a collection, or expect a collection to remain constant, it's a good practice to defensively copy it into an immutable collection.

Important: Each of the Guava immutable collection implementations rejects null values. We did an exhaustive study on Google's internal code base that indicated that null elements were allowed in collections about 5% of the time, and the other 95% of cases were best served by failing fast on nulls. If you need to use null values, consider using Collections.unmodifiableList and its friends on a collection implementation that permits null. More detailed suggestions can be found here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I copy an object in Java?

How do I make the properties of a JS object non-enumerable?

how do i create a copy of an object in java, instead of a pointer

How do I combine a java.util.Date object with a java.sql.Time object?

How do you make a deep copy of an object?

In javascript, how do I copy all properties of an object into a block's variable scope?

How do I make java wait for input before assigning the input to a variable to avoid java.util.NoSuchElementException

Java: How to make a copy of an array of object?

How to make a customize object copy? Java

How to create hierarchical JSON from java.util.Properties object

Java How do i check for an arraylist object and make it true/false?

How do I make Java Object from this JSON with GSON?

How do I make a copy of LPCWSTR?

How do I make an object that has infinite depth through arbitrary properties in Javascript?

How can I make a copy of an iterator in Java?

How do I create a copy of an object in PHP?

How do I copy an object with presigned URL?

How do I create an empty copy of an object?

How do I copy an object using a pointer?

How do I change the properties of an object in state?

How do I enumerate the properties of a JavaScript object?

How do I sort an object with children properties?

How do I recursively read properties of an Object

Do I need to make a method in an object to call it's own properties?

Do multiple threads Using the same object in java make a copy of it?

How do I Sort Object Properties by one of their Properties (Not sorting an array)?

How do I display the obtained object properties in the properties sheet?

How does Bluebird's util.toFastProperties function make an object's properties "fast"?

Java graphics: How to make java remember the state and properties of an object

TOP Ranking

HotTag

Archive