Map de-weight, go to value the same element, retain the value of the smallest key

Source: Internet
Author: User
Tags sorts

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.