java


Removing hashmap key but getting value in previously saved reference


I am using Java 1.7
public static void main(String[] args) {
HashMap<Integer, String> testMap = new HashMap<Integer, String>();
testMap.put(1, "a");
String testString = testMap.get(1);
System.out.println("Before remove = " + testString);
testMap.remove(1);
System.out.println("After Remove " + testString);
}
The output is:
Before remove = a
After Remove a
Can anyone explain?
As already mentioned in comments, you are printing stored value of testString again.
If you want to see updated value in testString then you have to assign it
again. like:
HashMap<Integer, String> testMap = new HashMap<Integer, String>();
testMap.put(1, "a");
String testString = testMap.get(1);
System.out.println("Before remove = " + testString);
testMap.remove(1);
testString = testMap.get(1);
System.out.println("After Remove " + testString);
Now output will be:
Before remove = a
After Remove null
Removing a element from a collection doesn't change the value of the referenced object.
It changes only the map content :
String content = "test";
testMap.add(1, content);
testMap.remove(1);
System.out.println("After Remove " + testMap.get(1)); // "null" is printed
System.out.println("content"= + content) // content has not changed. So "test" is printed

Related Links

Java TCP server - android client packet loss
Does locking happen when doing inserts with AUTO_INCREMENT column in MySQL
How can I launch VI from within Java under commons-exec?
Java - error implementing logging code
Save a the text from a jTextArea (ie Save As) into a new .txt file
How do I stop Android ProgressBar Circle?
cannot find symbol with isLowerCase
Android VPN authentication and encryption
#RequestMapping not mapping correctly
How to disable position change of JSlider on mouse right click
Getting records by groups in JPA
Are there any tools to re-namespace a Java library and its references?
Java SMS gateway can connect with the comm port but doesn't send message
Java - Simple plane of inclusion/exclusion
Call a function within a thread and publish intermediate results: how to it using SwingWorker?
Solr Out of memory exception

Categories

HOME
twitter
listview
debugging
programming-languages
leon
ckeditor
in-app-purchase
facebook-php-sdk
quicklook
static-libraries
uitypeeditor
opentracing
seaborn
graphlab
ef-migrations
solaris-10
http-status-code-504
nas
autocad-plugin
riot.js
physics-engine
intentfilter
excel-2007
h2db
qhull
fish
facet
selectedindexchanged
strncpy
quote
bpel
socialengine
http-status-code-503
twilio-api
column-family
normal-distribution
overlap
libvpx
dism
.net-4.6.2
btrace
executenonquery
jquery-nestable
theming
nodebb
knpmenubundle
orthogonal
blacklist
objective-c-swift-bridge
firmata
grails-tomcat-plugin
android-textview
bonobo
ctest
websitepanel
jwplayer7
livequery
design-by-contract
cannon.js
pundit
microbenchmark
sonarqube5.1.2
dereference
umbraco6
directoryservices
xc16
cdt
asp.net-dynamic-data
relocation
codeigniter-routing
client-side-templating
device-manager
article
starcluster
phpthumb
wp-query
meteor-velocity
jsctypes
ember-app-kit
jelly
selected
reddot
asyncfileupload
adk
stage
coercion
jmock
gallio
gamma
mozilla-prism
iweb
photoshop-cs4
mtj
simpletest
temporal-database
anti-piracy
.net-1.0

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App