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

Jenkins thinks failing junit test is passing
ARToolKit marker detect java Method
Getting SQLiteException: no such column
Spring Boot test REST Controller - How to debug the TestRestTemplate exchange call?
Extracting html response from jsp of another web app [duplicate]
Java Swing: KeyBinding for KEY_TYPED event
Function to Convert Specific Date Range to HDFS Glob pattern
calculating shortest grid distance
Connect Selenium test to SauceLabs
Android - Java Programmatically populate gridview with square colour
When running Appium tests in parallel using driver.quit() causes the other test(s) to fail
Caused by: org.hibernate.TransactionException: JDBC begin transaction failed:
jruby: does enabling jruby.thread.pool.enabled means that multiple ruby threads will share same java thread
Displaying files and folder in jsp using hyperlink
Should i access spring properties outside spring context
retained size is less than shallow size

Categories

HOME
pdf
atom-editor
keras
server
plone
tizen-web-app
dxl
ip
offline
static-libraries
windows-server-2012
caml
tostring
oxyplot
google-cloud-speech
firefox-webextensions
lldb
opencover
physics-engine
sylius
crosstab
javacv
facet
phonegap
galsim
preg-match-all
disassembling
bitbucket-pipelines
squib
dism
quadratic-programming
glew
no-www
gpx
taffy
integrity
elgg
nodebb
cudafy.net
hittest
dwscript
minimization
gnome-shell-extensions
topbeat
pintos
nested-sets
parallel-data-warehouse
file-writing
multi-level
react-native-listview
python-stackless
whois
two-factor-authentication
kendonumerictextbox
qtableview
cannon.js
rgeo
responsive-images
event-bubbling
sailfish-os
device-orientation
titanium-modules
article
jquery-layout
htmlcleaner
kgdb
mbr
ccss
runtime.exec
eventual-consistency
seed
enterprisedb
itmstransporter
pvrtc
newtonscript
onsubmit
appconkit
code-cleanup
subgurim-maps
coredump
imac
remember-me
digest-authentication
memory-size
text-coloring
rtml

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