When learning the usage of map collection in java, I found serveral beneficial methods that was encountered in the daily life. Now I made a summary:
-
import java.util.ArrayList;
-
import java.util.Collections;
-
import java.util.Comparator;
-
import java.util.HashMap;
-
import java.util.Iterator;
-
import java.util.List;
-
import java.util.Map;
-
import java.util.SortedMap;
-
import java.util.TreeMap;
-
-
public class MapUtil
-
{
-
-
private static final Map<String, String> contents = new HashMap<String, String>();
-
@SuppressWarnings("unchecked")
-
public static void initMap()
-
{
-
Map testMap = new HashMap<String, String>();
-
testMap.put("Albert", "Shao");
-
contents.putAll(testMap);
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
public static void listMap()
-
{
-
Map<String, String> testMap = new HashMap<String, String>();
-
testMap.put("Albert", "Shao");
-
for (Map.Entry<String, String> entry : testMap.entrySet())
-
{
-
System.out.println(entry.getKey() + ":" + entry.getValue());
-
}
-
-
for (String key : testMap.keySet())
-
{
-
System.out.println(key + ":" + testMap.get(key));
-
}
-
-
for (String value : testMap.values())
-
{
-
System.out.println(value);
-
}
-
-
Iterator<Map.Entry<String, String>> keyIt = testMap.entrySet()
-
.iterator();
-
while (keyIt.hasNext())
-
{
-
Map.Entry<String, String> entry = keyIt.next();
-
System.out.println(entry.getKey() + ":" + entry.getValue());
-
}
-
}
-
-
-
-
-
-
-
-
-
-
public static void sort()
-
{
-
SortedMap<String, String> sortMap = new TreeMap<String, String>();
-
sortMap.put("Pair", "four");
-
sortMap.put("Apple", "two");
-
sortMap.put("Grape", "one");
-
sortMap.put("Banana", "three");
-
sortMap.put("Apple", "five");
-
System.out.println(sortMap);
-
}
-
-
-
-
-
-
-
-
-
public static void sortByValue()
-
{
-
Map<String, Integer> testMap = new HashMap<String, Integer>();
-
testMap.put("Pair", 2);
-
testMap.put("Apple", 1);
-
testMap.put("Grape", 4);
-
testMap.put("Banana", 3);
-
-
List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
-
testMap.entrySet());
-
Collections.sort(entryList,
-
new Comparator<Map.Entry<String, Integer>>()
-
{
-
public int compare(Map.Entry<String, Integer> c1,
-
Map.Entry<String, Integer> c2)
-
{
-
return (c1.getValue() - c2.getValue());
-
}
-
});
-
-
System.out.println(entryList);
-
}
-
-
-
-
-
-
-
-
-
-
public static void sortByObject()
-
{
-
Map<String, String> testMap = new HashMap<String, String>();
-
testMap.put("Pair", "BB");
-
testMap.put("Apple", "AB");
-
testMap.put("Grape", "AF");
-
testMap.put("Banana", "XY");
-
-
List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(
-
testMap.entrySet());
-
Collections.sort(entryList,
-
new Comparator<Map.Entry<String, String>>()
-
{
-
public int compare(Map.Entry<String, String> c1,
-
Map.Entry<String, String> c2)
-
{
-
return (c1.getValue().compareTo(c2.getValue()));
-
}
-
});
-
-
System.out.println(entryList);
-
}
-
-
public static void main(String[] args)
-
{
-
MapUtil.listMap();
-
MapUtil.sort();
-
MapUtil.sortByValue();
-
MapUtil.sortByObject();
-
}
-
}
If you want to further know about the usage of list methods, you could view my another blogs.
http://blog.csdn.net/sxb0841901116/article/details/20635267