HashMap嵌套HashMap

43 篇文章 0 订阅

HashMap嵌套HashMap

import java.util.HashMap;

import java.util.Set;

 

/*

 *HashMap嵌套HashMap

 *

 * 传智播客

 *             jc      基础班

 *                               陈玉楼              20

 *                               高跃                   22

 *             jy      就业班

 *                               李杰                   21

 *                               曹石磊              23

 *

 * 先存储元素,然后遍历元素

 */

public class HashMapDemo2 {

         publicstatic void main(String[] args) {

                   //创建集合对象

                   HashMap<String,HashMap<String, Integer>> czbkMap = new HashMap<String,HashMap<String, Integer>>();

 

                   //创建基础班集合对象

                   HashMap<String,Integer> jcMap = new HashMap<String, Integer>();

                   //添加元素

                   jcMap.put("陈玉楼",20);

                   jcMap.put("高跃",22);

                   //把基础班添加到大集合

                   czbkMap.put("jc",jcMap);

 

                   //创建就业班集合对象

                   HashMap<String,Integer> jyMap = new HashMap<String, Integer>();

                   //添加元素

                   jyMap.put("李杰",21);

                   jyMap.put("曹石磊",23);

                   //把基础班添加到大集合

                   czbkMap.put("jy",jyMap);

                  

                   //遍历集合

                   Set<String>czbkMapSet = czbkMap.keySet();

                   for(StringczbkMapKey : czbkMapSet){

                            System.out.println(czbkMapKey);

                            HashMap<String,Integer> czbkMapValue = czbkMap.get(czbkMapKey);

                            Set<String>czbkMapValueSet = czbkMapValue.keySet();

                            for(StringczbkMapValueKey : czbkMapValueSet){

                                     IntegerczbkMapValueValue = czbkMapValue.get(czbkMapValueKey);

                                     System.out.println("\t"+czbkMapValueKey+"---"+czbkMapValueValue);

                            }

                   }

         }

}

运行结果:

jc

    高跃---22

    陈玉楼---20

jy

    曹石磊---23

    李杰---21

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
仿某团list里嵌套list\ package com.zl.listview; import java.util.ArrayList; import java.util.HashMap; import com.kjy.kjylistview.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; /** * @author zl * @time 2014年12月24日15:37:18 */ public class QianTaoListviewActivity extends Activity { private ListView listView; private ArrayList<HashMap<String, Object>> parentList, childList; private ParentAdapter parentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_qiantao); init(); } private void init() { listView = (ListView) findViewById(R.id.qiantao_lv); getList(); } private void getList() { parentList = new ArrayList<HashMap<String, Object>>(); for (int i = 1; i < 10; i++) { childList = new ArrayList<HashMap<String, Object>>(); for (int j = i < 7 ? i : 7; j < 9; j++) { HashMap<String, Object> ParentMap = new HashMap<String, Object>(); ParentMap.put("parent_title", "三木铁板烧" + i); ParentMap.put("parent_address", "8." + i); ParentMap.put("parent_distance", "1" + i); ParentMap.put("parent_distance1", "已售123" + i); childList.add(ParentMap); } HashMap<String, Object> map = new HashMap<String, Object>(); map.put("child_item_title", "九州八拖二火锅" + i); map.put("child_item_price_old", "4." + i + "分"); map.put("child_item_price_score", "(100人)"); map.put("parent_lv", childList); parentList.add(map); } parentAdapter = new ParentAdapter(parentList, QianTaoListviewActivity.this); listView.setAdapter(parentAdapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Intent intent = new Intent(QianTaoListviewActivity.this, Activity_1.class); Toast.makeText(QianTaoListviewActivity.this, "第" + arg2 + "个条目", Toast.LENGTH_LONG).show(); startActivity(intent); } }); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值