Home > Back-end >  A collection of the map is divided into two
A collection of the map is divided into two

Time:09-16

Existing a map, please set the key for a month, how do I in according to the parity of the map set is divided into two map set??????? Excuse me, how to deal with? Won't solve, thinking for a long time

CodePudding user response:

Create a new map, the odd number is moved to the new

CodePudding user response:

refer to the original poster CCviktory response:
, please present a map set key for a month, how do I in according to the parity of the map set is divided into two map set??????? Excuse me, how to deal with? , thinking for a long time not solve


All the key corresponding to digital group, is to build an enumeration class emun

CodePudding user response:

This month you have if any key is January and February are the two defined in a single array directly put to work in die (write) facilitate the array according to the array data directly take and map the key comparing the same is monthly different is the bi-monthly can also put an array of data in the configuration file

CodePudding user response:

1, save the key in the map data for the month, shall be kept in map1 in for an even number, save month for odd number in map1
For (String key: map. KeySet ()) {//keySet for map set of key set then the traversal key
Int keyNum=Integer. ParseInt (key);
If (keyNum % 2==0) {
Map1. Put (key, map. Get (key));
} else {
Map2. Put (key, map. Get (key))
}
}

CodePudding user response:

Public static void main (String [] args) {

Map The map=new HashMap<> (a);
The map. The put (1, "January");
The map. The put (2, "February");
The map. The put (3, "march");
The map. The put (4, "April");

//even
List OushuList=map. EntrySet (). ParallelStream (). The filter (I - & gt; I.g etKey (% 2)==0). Collect (Collectors. ToList ());
//odd
List QishuList=map. EntrySet (). ParallelStream (). The filter (I - & gt; I.g etKey () % 2!=0). Collect (Collectors. ToList ());

System. The out. Println (JSON. ToJSONString (oushuList));
System. The out. Println (JSON. ToJSONString (qishuList));
}

CodePudding user response:

Traverse map, determine the key is odd or even number, separate into two new map
  • Related