Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员
查看: 386|回复: 0

将ArrayList>转为ArrayList类型的解决方案

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-7-7 07:24:28 | 显示全部楼层 |阅读模式

    Bundle是一种利用键值对存储的数据格式,而我们在程序中通常利用HashMap存储数据。在开发中,通过Http请求得到JSONArray类型的返回值,我选择利用ArrayList<HashMap<String, String>>格式存储JSONArray对象。Bundle对象中putParcelabelArrayList方法可以将ArrayList格式数据传入Bundle,用于Intent在不同activity中传递。因此需要一种将ArrayList<HashMap<String, String>>格式转为ArrayList<Bundle>格式的方法。

    下面就贴出我实现的方法:

     1     /**
     2      * ArrayList中存储的Hashmap转为bundle
     3      * @param list
     4      * @return
     5      */
     6     public static ArrayList<Bundle> Map2Bundle(ArrayList<HashMap<String, String>> list){
     7         ArrayList<Bundle> bundleList = new ArrayList<>();
     8         for(HashMap map : list) {
     9             Bundle bundle = new Bundle();
    10             Iterator iter = map.entrySet().iterator();
    11             while (iter.hasNext()) {
    12                 Map.Entry entry = (Map.Entry)iter.next();
    13                 Object key = entry.getKey();
    14                 Object value = entry.getValue();
    15                 bundle.putString(String.valueOf(key), String.valueOf(value));
    16             }
    17             bundleList.add(bundle);
    18         }
    19         return bundleList;
    20     }

    这里遍历HashMap时采用的是效率最高的迭代器。欢迎大家多多批评指正!

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-1 09:15 , Processed in 0.055747 second(s), 28 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表