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

Multithreading with lucene IndexWriter
HeapSort Code snipplet giving Index Out of bound exception
Retrofit2 error : Java.lang.IllegalArgumentException: URL query string “a={action}&type=json&key=15670403” must not have replace block
Spring Boot and PostgreSQL : Error creating bean with name 'entityManagerFactory' defined in class path resource
How do I write a hint for Netbeans?
querydsl generate Q classes in src/main/java
Restful Web service- adding List of Pojos
How to stop reading from a file by the end of a line in java
\n does not generate newline in AlertDialog
How can i get started on this method? [closed]
Android Leanback - How to add DetailsOverviewRow action icon
MediaPlayer android stream radio starts not immediately or does not start at all
How to create/initialize a Variable with Tensorflow 1.0 Java API
Find solutions for a set of quadratic equations
Not getting JSON response in java
Need help writing file to external storage

Categories

HOME
google-chrome-extension
magnific-popup
electron
setup-deployment
programming-languages
razor
c#-2.0
windows-server
serverspec
flyway4
datastax-java-driver
node-notifier
gorm
ssl-client-authentication
modelica
quickbooks
qt-creator
size
libtiff
fatal-error
aurigma
propel
google-pagespeed
jtextfield
social-media
bpel
uisplitview
catel
repo
subset-sum
atl
y86
swisscomdev
hot-module-replacement
android-fingerprint-api
document.write
lumberjack
jvm-languages
google-datalayer
fault
pubmed
multipeer-connectivity
jlink
alphabet
forever
font-size
r-forge
qtwebview
pdfclown
iso8601
color-profile
httplistener
multi-level
google-web-starter-kit
django-debug-toolbar
holder.js
fadeout
design-by-contract
energy
execute
twirl
nsight
ami
directoryservices
rake-task
jsonpickle
file-copying
codeigniter-routing
nsbutton
tld
starcluster
datagridviewcolumn
app42
srs
fmod
dotnetnuke-5
dataservice
back-stack
http-unit
itmstransporter
getmessage
nsdatecomponents
mod-auth
blackberry-playbook
xmlspy
tomcat-valve
imac
datareader
w3wp.exe
multi-tier
nintendo-ds
iweb
libs
mtj
wsdl.exe
windows-live-messenger
lzh

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