您的位置:首页 > 其它

Map的方法总结

2016-10-14 10:09 190 查看
/**
* 将map转换为list
*/
@Test
public void testKeyValueEntrySet(){
Map<String, String> map = new HashMap<String, String>();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");

//keySet,a,b,c
List list1 = new ArrayList<>(map.keySet());
//values:1,2,3
List list2 = new ArrayList<>(map.values());
//entrySet:a=1,b=2,c=3
//注意在list3中map.entrySet()返回的是一个<Map.Entry<K,V>>的泛型,
List list3 = new ArrayList<>(map.entrySet());
}

/**
* 遍历map
*/
@Test
public void testTraversalMap(){
Map<String, String> map = new HashMap<String, String>();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");

//使用Entry遍历Map
//a:1
//b:2
//c:3
for(Entry<String,String> entry : map.entrySet()){
System.out.print(entry.getKey() + ":");
System.out.println(entry.getValue());
}

//使用Iterator和Entry
//a:1
//b:2
//c:3
Iterator itr = map.entrySet().iterator();
while(itr.hasNext()){
Entry e = (Entry) itr.next();
System.out.print(e.getKey() + ":");
System.out.println(e.getValue());
}
}

/**
* 通过key来对Map进行排序
*/
@Test
public void sortMapByKey(){
Map<String, String> map = new HashMap<String, String>();
map.put("2", "b");
map.put("1", "a");
map.put("4", "d");
map.put("3", "c");

//1:a
//2:b
//3:c
//4:d
List<Map.Entry<String, String>> list = new ArrayList<Entry<String,String>>(map.entrySet());
Collections.sort(list, new Comparator<Entry<String,String>>() {
@Override
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});

for(Entry<String,String> entry: list){
System.out.println(entry.getKey() + ":" + entry.getValue());
}

//另外如果是treeMap类型,因为TreeMap默认是按Key升序的
Map<String,String> map1 = new TreeMap<String,String>();
map1.put("2", "b");
map1.put("1", "a");
map1.put("4", "d");
map1.put("3", "c");

//1:a
//2:b
//3:c
//4:d
for(Entry<String,String> entry : map1.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}

//现在我们按key的降序来将treeMap排序
Map<String,String> map2 = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});

map2.put("2", "b");
map2.put("1", "a");
map2.put("4", "d");
map2.put("3", "c");

//4:d
//3:c
//2:b
//1:a
for(Entry<String,String> entry : map2.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}

/**
* 通过value进行排序
*/
@Test
public void sortMapByValue(){
Map<String,String> map = new HashMap<String,String>();
map.put("2", "b");
map.put("1", "a");
map.put("4", "d");
map.put("3", "c");

List<Entry<String, String>> list = new ArrayList<Entry<String,String>>(map.entrySet());
Collections.sort(list, new Comparator<Entry<String, String>>() {
@Override
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});

//1:a
//2:b
//3:c
//4:d
for(Entry<String,String> entry : list){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}

/**
* 对Map进行复制
*/
@Test
public void copyMap(){
Map<String, String> map = new HashMap<String,String>();
map.put("1", "a");
map.put("2", "b");

Map<String,String> map1 = Collections.synchronizedMap(map);

//1:a
//2:b
for(Entry<String,String> entry : map1.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}

/**
* 设置Map为不可用
*/
@Test
public void emptyMap(){
Map<String,String> map = new HashMap<String,String>();
map = Collections.emptyMap();
//java.lang.UnsupportedOperationException
map.put("1", "a");
}


/**
* 创建一个今天不可变的常量map
* @author liwenbin
*
*/
public class MapTestTwo {

private static final Map<String,String> map;

static{
Map<String,String> map1 = new HashMap<>();
map1.put("a", "1");
map1.put("b", "2");
map = Collections.unmodifiableMap(map1);
}

public static void main(String[] args) {
try {
//当尝试put时会报异常
//java.lang.UnsupportedOperationException异常
MapTestTwo.map.put("c", "3");
} catch (Exception e) {
System.out.println(e + "异常");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  day2