Home > Back-end >  Map <Long List <Long> the value in the Map -> List <Long> contain repeating elemen
Map <Long List <Long> the value in the Map -> List <Long> contain repeating elemen

Time:12-31

Public static void main (String [] args) {
Map The map=new HashMap<> (16);
List List1=new ArrayList<> (a);
List1. Add (1 l);
List1. Add (2 l);
List List2=new ArrayList<> (a);
List2. Add (2 l);
List2. Add (3 l);
List List3=new ArrayList<> (a);
List3. Add (3 l);
List3. Add (4 l);
List List4=new ArrayList<> (a);
List4. Add (5 l);
List4. Add (6 l);
List List5=new ArrayList<> (a);
List5. Add (6 l);
List5. Add (7 l);
The map. The put (1 l, list1);
The map. The put (2 l, list2);
The map. The put (3 l, list3);
The map. The put (4 l, list4);
The map. The put (5 l, list5);
Map List Map2=new HashMap<> (16);
List KeyList=new ArrayList<> (a);
List ValueList=new ArrayList<> (a);
Map. ForEach ((key, value) - & gt; {
System. The out. Println (" outer key: "+ key);
List List=new ArrayList<> (a);
Map. ForEach ((key1, value1) - & gt; {
System. The out. Println (" inner key: "+ key1);
if(! Collections. Disjoint (value, value1) & amp; & ! The key. The equals (key1)) {
System. The out. Println (" key: "+ key +" : "+ key1);
System. The out. Println (" value: "+ value +" : "+ value1);
KeyList. Add (key);
KeyList. Add (key1);
ValueList. AddAll (value);
ValueList. AddAll (value1);
/* else {}
System. The out. Println (" not join key: "+ key +" : "+ key1);
System. The out. Println (" not join value: "+ value +" : "+ value1);
} */
});
});
System. Out. Println (" keyList: "+ Arrays. ToString (keyList. The stream (). The distinct (). The toArray ()));
System. Out. Println (" valueList: "+ Arrays. ToString (valueList. The stream (). The distinct (). The toArray ()));
}

Example: list1 list2, list3 contain repeating elements, key in map2 - & gt; List put the map in list1 list2, list3 corresponding key collection of [1, 2, 3], the value - & gt; List put list1, list2, list3 to weight list [1, 2, 3, 4],
Expect results map2: key: value: [1, 2, 3] [1, 2, 3, 4]. Key: [4, 5] value: [5, 6]
  • Related