A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 学习代码 中级黑马   /  2014-3-29 09:54  /  829 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;
  2. class Student
  3. {
  4.         private String name;
  5.         private String id;
  6.         Student(String name,String id)
  7.         {
  8.                 this.name = name;
  9.                 this.id = id;
  10.         }
  11.         public String toString()
  12.         {
  13.                 return name+";;;;;;;"+id;
  14.         }
  15. }
  16. class Demo
  17. {
  18.         public static void main(String[]  args)
  19.         {
  20.                 Map<String,List<Student>> map= new TreeMap<String,List<Student>>();
  21.                         List<Student> yure = new ArrayList<Student>();
  22.                         List<Student> jiuye = new ArrayList<Student>();
  23.                                 map.put("jiuyeban",jiuye);       
  24.                                 map.put("yureban",yure);       
  25.                                 yure.add(new Student("zhangsan","01"));        //既然是学生对象的话,可不可以这样添加呢?
  26.                                                                                                                         //现在外面把 学生对象建立好在往里面添加
  27.                                 yure.add(new Student("lisi","02"));                        //是不是也可以呢?这两种方式没有什么不同的吧?
  28.                                 jiuye.add(new Student("wangwu","03"));                //哪一种方式比较节省空间呢?
  29.                                 jiuye.add(new Student("zhaoliu","04"));
  30.                                 Set<String> set= map.keySet();
  31.                                 Iterator<String> it1= set.iterator();
  32.                                 while(it1.hasNext())
  33.                                 {
  34.                                         String s1= it1.next();
  35.                                         List<Student> rom= map.get(s1);
  36.                                         System.out.println(s1);
  37.                                         sop(rom);
  38.                                 }

  39.                                
  40.         }
  41.         public static void sop(List<Student> list)
  42.         {
  43.         Iterator<Student> it= list.iterator();
  44.                                 while(it.hasNext())
  45.                                 {
  46.                                         Student s= it.next();
  47.                                         System.out.println(s);
  48.                                 }
  49.         }
  50. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 神马都是浮云

查看全部评分

3 个回复

倒序浏览
一.yure.add(new Student("lisi","02"));         
二.Student s = new Student("lisi","02");  
    yure.add(s);
楼主问的是这两种方式都会创建student对象,而只要是创建对象(只要是使用new关键字都是在堆内存中创建),
而第二种还要在栈内存中创建引用s,多了一步。但就你这几条数据量的比较,两种方式的效率应该是一样的。不过就是第一种可以少写点代码啊,也算个好处吧。  

评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

回复 使用道具 举报
2种集合一个是map 你用的是TreeMap不知道是不是便于排序呢
还有一个是用的List集合中的ArrayList 这俩种都是集合只是内部的数据结构不一样 节省空间那要看你怎么看了 我觉得依据需求来定吧
回复 使用道具 举报
楼主你好,如果问题已解决,请将帖子改为提问结束,如果没有解决请楼主继续提问,谢谢合作
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马