Sujal
Sujal

Reputation: 671

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?

Upvotes: 1

Views: 2892

Answers (5)

Kent
Kent

Reputation: 195059

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...

Upvotes: 0

george_h
george_h

Reputation: 1592

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);

Upvotes: 0

Shoaib Chikate
Shoaib Chikate

Reputation: 8975

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.

Upvotes: 0

Voidpaw
Voidpaw

Reputation: 920

Assume "someKey" is the key you're gonna remove, then

key1.put(someKey, key2.remove(someKey));

Upvotes: 2

user2173738
user2173738

Reputation:

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.

Upvotes: 0

Related Questions