java


Move a value from one key to another in HashMap


I've got an ArrayList that holds student object as follows:
List<Students> stdList = new ArrayList<Students>();
stdList.add(new Students(1,"std1","address1"));
stdList.add(new Students(2,"std2","address2"));
stdList.add(new Students(3,"std3","address3"));
stdList.add(new Students(4,"std4","address4"));
stdList.add(new Students(5,"std5","address5"));
stdList.add(new Students(6,"std6","address6"));
stdList.add(new Students(7,"std7","address7"));
stdList.add(new Students(8,"std8","address8"));
Now, I need to divide the stdList to two groups containing equal no of students say 4 in this case, and add them to hashMap which I achieved by:
int j=0;
HashMap<Integer,List<Students>> hm = new HashMap<>();
for (int i = 0; i < stdList.size(); i = i + 4)
{
j++;
hm.put(j,stdList.subList(i, i + 4));
}
The hashmap now contains key value pair as:
{1=[1 std1 address1, 2 std2 address2, 3 std3 address3, 4 std4 address4], 2=[5 std5 address5, 6 std6 address6, 7 std7 address7, 8 std8 address8]}
Now I need to move one value say "3 std3 address3" from "key 1" to "key 2" like:
{1=[1 std1 address1, 2 std2 address2, 4 std4 address4], 2=[5 std5 address5, 6 std6 address6, 7 std7 address7, 8 std8 address8,3 std3 address3]}
How can I achieve this?
List<Student> ls = hm.get(1);
Student st = ls.get(3);
ls.remove(st); hm.get(2).add(st);
you don't need to search the list if you could access it by the index.
Assume "someKey" is the key you're gonna remove, then
key1.put(someKey, key2.remove(someKey));
You can do like this;
Student stud3=myMap.get(1).remove(myMap.get(1).get(2));
List<Student> secondList=myMap.get(2);
secondList.add(stud3);
myMap.put(2,secondList);
where myMap is map formed by you.
The solution would be to get the list of students from the HashMap and remove the Student object you want to move. Then get the other list from the HashMap and simply just add the object.
I didn't run the below code, but it would be something like this
//Get the list for Key 1
List<Students> list = hm.get(Integer.valueOf(1));
//Remove the 3rd value, that would be your "3 std3 address3"
Students std = list.remove(2);
//Now get the list of Key 2
list = hm.get(Integer.valueOf(2));
//Add the value to that list
list.add(std);
I think you know how to search an element in list/map, and how to remove/add them. You have shown it in your codes. Your requirement is just another combination of those method calls, they won't be problem for you.
I guess you cannot go further because you got an exception:
ConcurrentModificationException
Because I see that you have used subList() method. It will return a view of backed list. You can change the elements in that list, but any modification of structure will throw that exception.
If this is the problem you are facing, simple solution would be creating a new list when you invoked subList, like new ArrayList(stdList.subList(i, i + 4)) then you can do structural modifications.
if this is not your problem, pls leave a comment, I will remove the answer.
P.S you may want to change your data-structure a bit, I don't know your exact requirement, but current structure is not so convenient..... you can check out the guava multi-map...

Related Links

How to implement a CSS template on JSF
Cucumber: You're not allowed to extend classes that define step definitions or hooks
How to control menuItems visibility through checkboxes in Android Studio
Tomcat server doesn't compile jsp
Java Hibernate delete cascade
Spring Authentication Required ERROR [duplicate]
Android Product Flavors for Java Web Projects
[Java][SQL] Hardcoded username and password on query
how to modify the value of a primitive data passed to a C++ routine using Java and JNI?
Java: test System output including “new lines” with assertEquals
How to ignore ' ' in Xpath?
Casting a type in a data structure
Spring Boot EnableCaching and Cacheable annotation not working
FATAL EXCEPTION: main NoSuchMethodError for VideoView.setOnPreparedListener() [duplicate]
Using SWIG to wrap c++ to java UnsatisfiedLinkError: HeaderJNI.new_Test()J
Java accessing object throught array

Categories

HOME
image
coq
cookies
drivers
tesseract
sd-card
octobercms
q
kalman-filter
mvvmcross
uber-api
directx
paradox
add
facebook-messenger-bot
imacros
windows-azure-storage
msp430
footer
openedx
firefox-webextensions
nodatime
riot.js
functional-testing
jndi
su
fluentvalidation
minitab
typo3-6.2.x
replaceall
jspm
kendo-datasource
perlin-noise
android-kernel
ghost4j
force-layout
io-redirection
hockeyapp
sqlite2
unoconv
unixodbc
apple-news
qsslsocket
dds
dandelion
blogengine.net
sage-one
drupal-6
sybase-asa
windows-mobile-6.5
qtwebview
migradoc
setuptools
make-install
rvest
tcpserver
0xdbe
sankey-diagram
p2
bridge.net
javax.mail
gadt
cctv
xcode-6.2
websocket4net
preferences
internet-connection
responsive-slides
ceil
article
datagridviewcolumn
app42
mbr
quantlib-swig
batterylevel
back-stack
yui-compressor
random-seed
ember-app-kit
robotics-studio
specification-pattern
selected
bitsharp
isnullorempty
funscript
labwindows
getmessage
reddot
removeclass
php-parser
recent-documents
w3wp.exe
sudzc
.nettiers
nsviewanimation
database-management
brewmp
adrotator
noscript
text-coloring
genealogy
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