JAVA需要的MAP存储方式,android同样适用!

(一)首先我们需要定义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();
           }
        }


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值