Map<integer,string>,integer represents time pinch, String represents text information
deduplication function: Delete the same element of value in map, leaving only the element with the smallest key.
public static map<integer,string> Removerepfrommap (map<integer,string> Map) {
set<entry<integer,string>> set = Map.entryset ();
list<entry<integer,string>> list = new arraylist<entry<integer,string>> (set);
Collections.sort (list,new comparator<entry<integer,string>> () {
The overloaded compare function sorts the list collection, sorts them according to the value values,
public int Compare (entry<integer,string> entry1,entry<integer,string> entry2) {
Return integer.valueof (Entry1.getvalue (). Hashcode ()-entry2.getvalue (). Hashcode ());
}
});
for (int i=0;i<list.size (); i++) {//delete duplicate elements
Integer key = List.get (i). GetKey ();
String value = List.get (i). GetValue ();
The next in int j=i+1;//map
if (J<list.size ()) {
Integer Next_key = List.get (j). GetKey ();
String Next_value = List.get (j). GetValue ();
if (value = = Next_value) {
if (Key.hashcode () < Next_key.hashcode ()) {map.remove (Next_key); List.remove (j);
}else{
Map.Remove (key); List.remove (i);
}
i--;
}
}
return map;
}
Map<integer,string> The first feature is: key if the same, the original key will be overwritten, so key must not be repeated
A description of the function of several functions:
Map.entry
Represents a single mapping relationship that is a key+value
EntrySet () method
Returns a collection of set views of the mappings contained in this map, which is actually a collection of multiple key+value,
Explain that the set in this, only if the key and value are all corresponding to the same time, will not be repeated "and this situation will not be stored in the map, because he will overwrite the original key", if key is different, the key is the same, still in the entryset inside
Map de-weight, go to value the same element, retain the value of the smallest key