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

extends in java [closed]
Matlab Javabuilder related issue
second css in content placeholder problem with javascript
java.lang.ClassNotFoundException: org.apache.catalina.connector.Response
facebook connect and jsf 2+restfb
Flipping an image
Background image slows down android app
Writing to File Exception?
joauth: facebook graph api when writing facebook applications
How can I find lat long of a point with a given lat long value and distance
Problem with using signed Applet with policy File
Java- Intersection point of a Polygon and Line
The application has stopped unexcepetedly
What is wrong with my JAVA do loop?
Facebook Android SDK randomly stopped working
how to autocomplete method name

Categories

HOME
yii2
winforms
gremlin
nullpointerexception
json.net
onedrive
electron
kalman-filter
adb
microservices
umd
callback
google-translate
offline
apache-cayenne
windows-server-2012
msp430
ghc
transformation
serilog
flux
shared-hosting
jprofiler
nas
javacv
facebook-access-token
virtualdub
fgetcsv
nat
binary-data
ecto
stringtemplate
repo
y86
mmenu
scaffold
sharefile
reportbuilder
sqlite2
cookiecutter-django
broadcastreceiver
avro4s
hibernate-tools
atomicity
errordocument
worksheet
python-webbrowser
recursive-datastructures
worker-thread
crypt
pdfclown
flutterwave
hittest
account-kit
yt-project
infix-notation
lttng
moveit
spim
citrus-pay
processmodel
iiviewdeckcontroller
oberon
dundas
javax.mail
cdt
article
xceed-datagrid
tidy
http-unit
labwindows
jspinclude
enter
zpt
mod-auth
propagation
visitor-statistic
asp.net-mvc-areas
forums
meego
iweb
aquaticprime
multiple-languages

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