JAVA集合框架的总结(集合嵌套)



Java的集合类主要由两个接口派生而出:Collection和Map,Collection和Map是Java集合框架的根接口,这两个接口又包含了一些接口或实现类。Collection集合应用于储存单一对象,常用实现类ArrayList,TreeSet,HashSet.遍历方式:Iterator接口中的iterator方法遍历。Map集合用于储存具有映射关系的对象,常用实现类HashMap,TreeMap.另外Map的两种遍历方式,keySet和entrySet。集合之间在某些情况下可以进行嵌套,储存更多的额数据。根据现实中发生的情况,一个学校会有多个班级,每个班级会有多个学生。这时候就的映射关系是一对多的关系集合间的嵌套。第一种是HashMap内部嵌套HashMap集合第二种是HashMap内部嵌套List集合

第一种嵌套:

public static void ListProDemo()
{
HashMap<String,List<Student>> hmm=new HashMap<String,List<Student>>();
List<Student> yure1=new ArrayList<Student>();
List<Student> jiuye1=new ArrayList<Student>();
hmm.put("yure1",yure1);
hmm.put("jiuye1",jiuye1);
Student stu1=new Student();
Student stu2=new Student();
Student stu3=new Student();
Student stu4=new Student();
stu1.setName("张三");
stu1.setAge(12);
stu2.setName("虾米");stu2.setAge(16);
stu3.setName("苏哈");stu3.setAge(18);
stu4.setName("麦克斯");stu4.setAge(19);
yure1.add(stu1);
yure1.add(stu2);
jiuye1.add(stu3);
jiuye1.add(stu4);
Iterator<String> it1=hmm.keySet().iterator();
while (it1.hasNext())
{
String roomName=it1.next();
List li=hmm.get(roomName);
System.out.println(";;;;;"+roomName+";;;;;");
listPut(li);
}
}


public static void listPut(List<Student> list)
{
Iterator<Student> it=list.iterator();
while (it.hasNext())
{
Student s=it.next();
System.out.println(s.toString());
}
}

第二种嵌套:
public static void MapDemoPro()
{
HashMap<String,HashMap<String,String>> hm=new HashMap<String,HashMap<String,String>>();//大集合
HashMap<String,String> yure=new HashMap<String,String>();//小集合
HashMap<String,String> jiuye=new HashMap<String,String>();//小集合
hm.put("yure",yure);
hm.put("jiuye",jiuye);
yure.put("06","张三");
yure.put("02","小明");
yure.put("03","小红");
yure.put("04","李四");
jiuye.put("01","康辉");
jiuye.put("03","小王");
jiuye.put("02","康健");
jiuye.put("05","小亮");
//mapPut(yure);
//mapPut(jiuye);
Iterator<String> it=hm.keySet().iterator();
while (it.hasNext())//一次遍历
{
String mingcheng=it.next();
HashMap<String,String> hhmm=hm.get(mingcheng);
System.out.println("...."+mingcheng+"...");
mapPut(hhmm); //方法内有二次遍历
}
}
public static void mapPut(HashMap<String,String> hm)
{
Set<String> keySet=hm.keySet();
Iterator<String> it=keySet.iterator();
while (it.hasNext())
{
String id=it.next();
String name=hm.get(id);
System.out.println(id+".."+name);
}
}
}


总得来说,集合在开发过程中是经常使用,这部分会经常用到泛型,需要多下功夫。Map的第二种遍历方式会有一个特殊的接口Map.Entry接口,这个接口也就是Entry接口是Map接口的静态内部接口,很有意思。

Java的集合类主要由两个接口派生而出:Collection和Map,Collection和Map是Java集合框架的根接口,这两个接口又包含了一些接口或实现类。 Collection集合应用于储存单一对象,常用实现类ArrayList,TreeSet,HashSet. 遍历方式:Iterator接口中的iterator方法遍历。 Map集合用于储存具有映射关系的对象,常用实现类HashMap,TreeMap. 另外Map的两种遍历方式,keySet和entrySet。 集合之间在某些情况下可以进行嵌套,储存更多的额数据。 根据现实中发生的情况,一个学校会有多个班级,每个班级会有多个学生。 这时候就的映射关系是一对多的关系集合间的嵌套。 第一种是HashMap内部嵌套HashMap集合 第二种是HashMap内部嵌套List集合 第一种嵌套:
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值