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

Are jackson web servlets thread safe?
Spring Boot get Primary Key in json answer
How to terminate continuous thread that detects new file
Windows shortcuts and links [closed]
Hibernate Criteria Returning Set
Java: MySQL connection won't close
How to properly update a transparent swing overlay?
Making sense of non-null and nullability annotations
Deserialize json in java with gson
“Connection is not open” FTP Android
getting error while executing hdfs namenode -format(Hadoop)
java.lang.IndexOutOfBoundsException: Invalid index 5, size is 1
Element of a Set randomly changing
How can I see the javac command IntelliJ IDEA uses to compile my code?
get/post web service from db in java
code for automatically sending push notifications in java

Categories

HOME
bluetooth
keycloak
image-processing
deezer
tizen
include
at-command
infragistics
wamp
add
virtualization
slurm
gorm
numeral.js
usergrid
jsprit
firefox-webextensions
jprofiler
swiftlint
tibco-mdm
su
internet-explorer-9
entitlements
wtx
compatibility
primitive
gsmcomm
typed.js
http-referer
force-layout
main
occlusion
quadratic-programming
filepicker
tizen-native-app
git-diff
svmlight
statsd
carrot
namecoin
python-cryptography
mako
zendesk-app
sqldf
chord-diagram
pycaffe
nested-sets
time-and-attendance
nxt
angstrom-linux
master-slave
livequery
php-internals
associative-array
execute
splash
graph-drawing
createprocessasuser
composite
skos
facebook-graph-api-v2.4
titanium-modules
markers
googlemock
starcluster
expected-exception
.aspxauth
referrer
bitsharp
frameset
venn-diagram
propertyeditor
appconkit
buildr
amazon-appstore
asyncfileupload
eclipse-templates
mercurial-server
heartbeat
locate
remember-me
subviews
wise
asp.net-profiles
phonon
nerddinner
zune

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App