Map保存的是二元偶对象,简单说就是两个值,key和value。使用中可以通过key查找到value数据,使用Map可以方便用户查询。常见的子类有HashMap、LinkedHashMap、Hashtable、TreeMap。
Map接口常用方法:
public V put(K key, V value) //向集合中保存数据,如果重复会返回替换前数据
public V get(Object key) //根据key查询数据
public boolean containsKey(Object key) //查询指定的key是否存在
public Set<K> keySet() //将Map集合中的key转换为Set集合
public V remove(object key) //根据key删除指定的数据
public Set<Map.Entry<k,v>> entrySet() //将map集合转化为set集合
HashMap
HashMap是Map接口中常用子类,主要特点是采用散列方式进行存储。
import java.util.HashMap;
import java.util.Map;
public class Hello {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("hello", 123);
map.put("one", 1);
map.put("haha", 2);
//在Map中提供的方法put设置数据时,如果设置的key不存在,则可以直接保存,并返回null;
//如果设置的key存在,则会发生覆盖,并返回覆盖前的value。
System.out.println(map.put("one",456));
System.out.println(map.get("one"));
System.out.println(map.get("hello"));
System.out.println(map);
}
}
//运行结果
1
456
123
{haha=2, one=456, hello=123}
TreeMap
TreeMap属于有序的Map集合类型,它可以按照key进行排序。
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class Hello {
public static void main(String[] args) {
Map<String, Integer> map1 = new TreeMap<String, Integer>();
map1.put("c", 123);
map1.put("a", 1);
map1.put("d", 2);
//按key的值从小到大输出
System.out.println(map1);
System.out.println("--------------------");
Comparator<String> cmp = new Comparator<String>() {
public int compare(String a, String b) {
return b.compareTo(a);
}
};
Map<String, Integer> map2 = new TreeMap<String, Integer>(cmp);
map2.put("c", 123);
map2.put("a", 1);
map2.put("d", 2);
//按key的值从大到小输出
System.out.println(map2);
}
}
//运行结果
{a=1, c=123, d=2}
--------------------
{d=2, c=123, a=1}
Map集合的遍历
Java 里的Map不支持Iterable接口,所以不能直接使用foreach对其内的所有元素进行遍历,需要先转成支持Iterable接口的set.
1、通过foreach循环输出Map集合
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Hello {
public static void main(String[] args) {
Map<String, Integer> map = new TreeMap<String, Integer>();
map.put("c", 123);
map.put("a", 1);
map.put("d", 2);
//按key的值从小到大输出
Set<Map.Entry<String, Integer>> set = map.entrySet(); //将map变为set
//key和value 都需要输出
for(Map.Entry<String, Integer> entry : set) {
String mapkey = entry.getKey();
Integer mapValue = entry.getValue();
System.out.println(mapkey+" "+mapValue);
}
System.out.println("---------------");
//输出key
for(String key : map.keySet()){
System.out.print(key + " ");
}
System.out.println();
System.out.println("---------------");
//输出value
for(Integer value : map.values()){
System.out.print(value + " ");
}
}
}
//运行结果
a 1
c 123
d 2
---------------
a c d
---------------
1 123 2
2、使用Iterator输出Map集合
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Hello {
public static void main(String[] args) {
Map<String, Integer> map = new TreeMap<String, Integer>();
map.put("c", 123);
map.put("a", 1);
map.put("d", 2);
//按key的值从小到大输出
Set<Map.Entry<String, Integer>> set = map.entrySet(); //将map变为set
Iterator<Map.Entry<String, Integer>> it = set.iterator(); //获取Iterator
while(it.hasNext()) {
Map.Entry<String, Integer> me = it.next(); //获取Map.Entry
System.out.println(me.getKey() + " " + me.getValue());
}
}
}
//运行结果
a 1
c 123
d 2
本文深入讲解了Java中Map接口的基本概念、常见子类及其使用方法,包括HashMap、TreeMap等,探讨了Map的遍历技巧,是理解Java集合框架的重要资源。
545

被折叠的 条评论
为什么被折叠?



