(一)首先我们需要定义map集合添加数据:
1.1 /**
* 可以按照我们put的顺序进行存储map数据
*/
private static Map<String, String> map = new LinkedHashMap<>();
1.2
/**
* 往map里面添加数据
*
* @param
map
*/
public
static
void
addData
(Map<String, String> map) {
map.put(
"a"
,
"a"
);
map.put(
"b"
,
"b"
);
map.put(
"c"
,
"c"
);
map.put(
"d"
,
"d"
);
}
(二)取值方式
2.1 第一种取值方式取出所有的键,再取出对应的值
public
static
void
getMapVauleOneMethod
(Map<String, String> map) {
if
(map !=
null
) {
// 将map中的所有键去取出来,用迭代器进行读取
Set set = map.keySet();
if
(set !=
null
) {
Iterator iterator = set.iterator();
while
(iterator.hasNext()) {
// 取出单个的map键
String key = (String) iterator.next();
String value = map.get(key);
System.out.println(
"key = "
+ key +
"value"
+ value);
}
}
}
}
2.2第二种每个键值对取出来封装一个entry
public
static
void
getMapVauleTwoMethod
(Map<String, String> map) {
if
(map !=
null
) {
Set set = map.entrySet();
if
(set !=
null
) {
Iterator iterator = set.iterator();
while
(iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println(
"key = "
+ key +
"value"
+ value);
}
}
}
}
2.3第三种方法
public
static
void
getMapVauleThreeMethod
(Map<String, String> map) {
if
(map !=
null
) {
for
(Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(
"key = "
+ key +
"value"
+ value);
}
}
}
(三),根据指定的KEY取出VALUE
String resultVal;
String str = "固定key";
Map<String,String> map = new HashMap<String,String>();
map.put("key","value");//
for(Map.Entry<String,String> str : map.entrySet())
{
if(str.equals(str.getKey()))
{
resultKey = str.getValue();
}
}