11

How to freeze a HashMap to avoid other changes?

 3 years ago
source link: https://www.codesd.com/item/how-to-freeze-a-hashmap-to-avoid-other-changes.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to freeze a HashMap to avoid other changes?

advertisements

The question is pretty much self-explanatory. I have a data structure (I mentioned a HashMap but it could be a Set or a List also) which I initially populate:

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

for( something ) {
  map.put( something );
}

After the structure has been populated, I never want to add or delete any items:

map.freeze();

How could one achieve this using standard Java libraries?


The best you can do with standard JDK libraries is Collections.unmodifiableMap().

Note that you must drop the original map reference, because that reference can still be accessed and changed normally. If you passed the old reference to any other objects, they still will be able to change your map.

Best practice:

map = Collections.unmodifiableMap(map);

and make sure you didn't share the original map reference.

Related Articles

How to browse a HashMap while avoiding ConcurrentModificationException

I have a HashMap, its of the type HashMap<String,HashMap<String,int>> now i need to iterate over this HashMap and delete the inner HashMaps that have a value of 0 for any key. If such a removal makes the inner HashMap empty then the inner Hash

How to freeze the repainted web browser while changing the visibility of items?

I'm by far no JS developer (in fact, hardly developer at all :) but I wanted to try to write a small Greasemonkey script to hide few elements on a certain webpage. Once I've started dabbing at it, I've decided to use jQuery, just to make it easier. E

How to sort a Hashmap by Key String?

I am using some holiday dates in a HashMap. this.holidayMap.put("01.05", new Event("1. Mai")); this.holidayMap.put("14.02", new Event("Valentinseday")); That is some of my input. I have a seperate method to add them

How to freeze the screen?

In Unity, how can I freeze the screen in one scene, change scene, and then unfreeze it in the other scene? With freezing the screen I simply mean that it doesn't get updated while it's frozen. With screen I simply mean this: I've tried to set Time.ti

How to freeze the rotation of a Rigidbody programmatically?

I am creating a 2D platformer game, and I already wrote the script that moves the sprite forwards and backwards successfully. However, I am extremely new to Unity and C#, so I have no idea how to freeze rotation of the sprite. I tried to do it progra

How to freeze numbers when you have reached a number in a matrix

I have a matrix, z: z = [0 0 0 0 0 0 0 0 0 0] for i = 2:20 z(i,:) = z(i-1,:) + 5; end So this loop will generate a matrix starting with a line of zeros, then adding an extra 5 to each line. I want to know how to freeze it when it reaches 30, so all v

How to freeze the header and footer in ListView

I have added header and footer in a ListView, I just don't know how to freeze them when the list is being scrolled. Please kindly help me. (The code below just for header only) listview.xml <?xml version="1.0" encoding="utf-8"?>

How to convert a HashMap of objects into an ArrayList of Strings

I have a HashMap like so: Hashmap<String, Object> map = new Hashmap<String, Object>(); map.put(1, {id_student:"1;2;3"}); map.put(2, {id_student:"4;5"}); I want to get the values and put it in an ArrayList like: array = [0]

How can I store HashMaps in mongodb using morphine?

How do i store HashMaps in mongodb using morphia? I'm not sure if this is a bug in morphia, or if i'm just doing it wrong. say i have this domain model @Entity("person") public class Person { private String property1; private String property2; p

How do I call this function in other parts of my code?

Pardon me if this is a very silly question. I'm brand new to JS and I was wondering how I can use this function in other parts of my code. I looked at tutorials and other websites, but the way they define functions seems to be different than what I h

How to freeze the GridView header?

As in a title, does anyone know how to freeze GridView header in ASP.NET ?Option (a) buy into a UI package that includes a souped-up GridView with this functionality built-in. Option (b) roll your own - it's not simple. Dino Esposito has one approach

Java - How much does a HashMap.get () call cost?

How expensive is a HashMap.get() call? The Map in question contains hundreds of mappings and I'm looking to be calling .get() anywhere between 60-1200 times a second.HashMap.get() is on average O(1), which means it's pretty efficient. This efficiency

How to make a SWF file, including other resources to a set of applications in osx?

I'm new to Mac OS X, so these questions may seem to be stupid. I have googled for nearly half a day, but can't find the exact answers. About applications on Mac OS X: Does any application must be placed in the /Applications folder to execute it? If n

In GIT, how do you delete a delete file from a commit with other changes?

I have made a GIT commit. This commit contains several changes, including a file delete. I have since made other commits. All the commits have been pushed, with a pull request open for review (not yet approved or merged). Now I would like to remove t

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK