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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© _Water 中级黑马   /  2014-4-14 00:35  /  1018 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.Calendar;
  2. import java.util.Date;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map.Entry;

  6. /**
  7. * 测试keySet()与entrySet()的迭代时间
  8. * keySet():迭代后只能通过get()取key
  9. * entrySet():迭代后可以e.getKey(),e.getValue()取key和value。返回的是Entry接口
  10. * 最后说明下keySet()的速度比entrySet()慢了很多。看来以后要考虑用entrySet()了
  11. * @author JackeenLiu
  12. * @date 2014-4-14 星期一
  13. */
  14. public class HashMapTest        
  15. {
  16.         public static void main(String[] args)
  17.         {
  18.                 HashMap<String,String> kmap = new HashMap<String,String>();
  19.                 HashMap<String, String> emap = new HashMap<String, String>();
  20.                
  21.                 //装数据
  22.                 for (int i = 0; i < 100; i++)
  23.                 {
  24.                         kmap.put(""+i, "abcdefghijklmnopqrstuvwxyz");
  25.                 }
  26.                 for (int i = 0; i < 100; i++)
  27.                 {
  28.                         emap.put(""+i, "abcdefghijklmnopqrstuvwxyz");
  29.                 }
  30.                
  31.                 long stimes = System.currentTimeMillis();
  32.                 long ctimes = Calendar.getInstance().getTimeInMillis();
  33.                 long dtimes = new Date().getTime();
  34.                
  35.                 //初始时间 这里我用了三种取值方式 最后发现System.currentTimeMillis();是最直接的取值方法
  36.                 System.out.println(stimes+"        "+ctimes+"        "+dtimes);
  37.                
  38.                 Iterator<String> ktor = kmap.keySet().iterator();
  39.                 while(ktor.hasNext())
  40.                 {
  41.                         System.out.println(ktor.next());
  42.                 }
  43.                
  44.                 long stimes1 = System.currentTimeMillis();
  45.                 long ctimes1 = Calendar.getInstance().getTimeInMillis();
  46.                 long dtimes1 = new Date().getTime();
  47.                
  48.                 //结束世界并且也是entrySet的开始时间
  49.                 System.out.println((stimes1-stimes)+"        "+(ctimes1-ctimes)+"        "+(dtimes1-dtimes));
  50.                 System.out.println(stimes1+"        "+ctimes1+"        "+dtimes1);
  51.                
  52.                 Iterator<Entry<String, String>> itor = emap.entrySet().iterator();
  53.                 while(itor.hasNext())
  54.                 {
  55.                         Entry<String, String> e = itor.next();
  56.                         //System.out.println(e.getKey());
  57.                         System.out.println(e.getValue());
  58.                 }
  59.                
  60.                 long stimes2 = System.currentTimeMillis();
  61.                 long ctimes2 = Calendar.getInstance().getTimeInMillis();
  62.                 long dtimes2 = new Date().getTime();
  63.                 System.out.println(stimes2+"        "+ctimes2+"        "+dtimes2);
  64.                 System.out.println((stimes2-stimes1)+"        "+(ctimes2-ctimes1)+"        "+(dtimes2-dtimes1));
  65.         }
  66. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

3 个回复

倒序浏览
/*
对学生数据类的存取
*/

  1. import java.util.*;
  2. class Test
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 HashMap<Student,String> hs =new HashMap<Student,String>();

  7.                 hs.put(new Student("zhangsan",11),"bj");
  8.                 hs.put(new Student("lisi",31),"nj");
  9.                 hs.put(new Student("wangwu",21),"dj");


  10.                 //方式1.
  11.                 Set<Student> keySet=hs.keySet();

  12.                 for (Iterator<Student> it =keySet.iterator(); it.hasNext(); )
  13.                 {
  14.                         Student key =it.next();
  15.                         String addr =hs.get(key);
  16.                         sop(key+".."+addr);
  17.                 }

  18.                 //方式2
  19.                 Set<Map.Entry<Student,String>> entry =hs.entrySet();
  20.                 for (Iterator<Map.Entry<Student,String>> it =entry.iterator();it.hasNext() ; )
  21.                 {
  22.                         Map.Entry<Student,String> e =it.next();
  23.                         //Student key =it.next().getKey();
  24.                         //String value =it.next().getValue();
  25.                         Student key =e.getKey();
  26.                         String value =e.getValue();
  27.                         sop(key+"----++++-------"+value);
  28.                 }
  29.         }
  30.         public static void sop(Object obj)
  31.         {
  32.                 System.out.println(obj);
  33.         }
  34. }

  35. class Student implements Comparable<Student>
  36. {
  37.         private String name;
  38.         private String address;
  39.         private int age;

  40.         public int compareTo(Student s)
  41.         {
  42.                 int num =new Integer(this.age).compareTo(new Integer(s.age));
  43.                 if (num ==0)
  44.                 {
  45.                         return this.name.compareTo(s.name);
  46.                 }
  47.                 return num;
  48.         }

  49.         public int hashCode()
  50.         {
  51.                 return name.hashCode()+age*11;
  52.         }

  53.         public boolean equals(Object obj)
  54.         {
  55.                 if(!(obj instanceof Student))
  56.                         throw new ClassCastException("不是同一类");
  57.                 Student s =(Student)obj;
  58.                 return this.name.equals(s.name) && this.age==s.age;
  59.         }
  60.         Student(String name,int age)
  61.         {
  62.                 this.name=name;
  63.                 this.age=age;
  64.         }

  65.         public String getName()
  66.         {
  67.                 return name;
  68.         }
  69.         public int getAge()
  70.         {
  71.                 return age;
  72.         }

  73.         public String toString()
  74.         {
  75.                 return name+"---->"+age;
  76.         }
  77. }
复制代码
回复 使用道具 举报
  1. import java.util.*;

  2. /*
  3. 需求:对学生对象的年龄进行升序排序。
  4. 因为数据是以键值对形式存在的,
  5. 要使用可以进行排序的Map集合。TreeMap
  6. */

  7. class StuNameCompare implements Comparator<Student>//自定义比较器,先按名字排序
  8. {
  9.         public int compare(Student s1,Student s2)
  10.         {
  11.                 int num =s1.getName().compareTo(s2.getName());
  12.                 if (num ==0)
  13.                 {
  14.                         return new Integer (s1.getAge()).compareTo(new Integer(s2.getAge()));
  15.                 }
  16.                 return num;
  17.         }
  18. }
  19. class Test
  20. {
  21.         public static void main(String[] args)
  22.         {
  23.                 TreeMap<Student,String> tm =new TreeMap<Student,String>();

  24.                 tm.put(new Student("zhangsan",11),"bj");
  25.                 tm.put(new Student("lisi",31),"nj");
  26.                 tm.put(new Student("wangwu",21),"dj");
  27.                 tm.put(new Student("wangwu",21),"极乐世界");



  28.                 //方式1.
  29.                 Set<Student> keySet=tm.keySet();

  30.                 for (Iterator<Student> it =keySet.iterator(); it.hasNext(); )
  31.                 {
  32.                         Student key =it.next();
  33.                         String addr =tm.get(key);
  34.                         sop(key+".."+addr);
  35.                 }

  36.                 //方式2
  37.                 Set<Map.Entry<Student,String>> entry =tm.entrySet();
  38.                 for (Iterator<Map.Entry<Student,String>> it =entry.iterator();it.hasNext() ; )
  39.                 {
  40.                         Map.Entry<Student,String> e =it.next();
  41.                         //Student key =it.next().getKey();
  42.                         //String value =it.next().getValue();
  43.                         Student key =e.getKey();
  44.                         String value =e.getValue();
  45.                         sop(key+"----++++-------"+value);
  46.                 }
  47.         }
  48.         public static void sop(Object obj)
  49.         {
  50.                 System.out.println(obj);
  51.         }
  52. }

  53. class Student implements Comparable<Student>
  54. {
  55.         private String name;
  56.         private String address;
  57.         private int age;

  58.         public int compareTo(Student s)
  59.         {
  60.                 int num =new Integer(this.age).compareTo(new Integer(s.age));
  61.                 if (num ==0)
  62.                 {
  63.                         return this.name.compareTo(s.name);
  64.                 }
  65.                 return num;
  66.         }

  67.         public int hashCode()
  68.         {
  69.                 return name.hashCode()+age*11;
  70.         }

  71.         public boolean equals(Object obj)
  72.         {
  73.                 if(!(obj instanceof Student))
  74.                         throw new ClassCastException("不是同一类");
  75.                 Student s =(Student)obj;
  76.                 return this.name.equals(s.name) && this.age==s.age;
  77.         }
  78.         Student(String name,int age)
  79.         {
  80.                 this.name=name;
  81.                 this.age=age;
  82.         }

  83.         public String getName()
  84.         {
  85.                 return name;
  86.         }
  87.         public int getAge()
  88.         {
  89.                 return age;
  90.         }

  91.         public String toString()
  92.         {
  93.                 return name+"---->"+age;
  94.         }
  95. }
复制代码
回复 使用道具 举报
  1. import java.util.*;

  2. /*
  3. 需求:对字符串出现的次数进行统计:a[2]b[3]:
  4. TreeMap:可以对输出的字符进行排序
  5. */


  6. class Test
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.                 sop(charCount("dsafsadkfljsdfasd"));
  11.                 sop(avg());
  12.         }
  13.         public static void sop(Object obj)
  14.         {
  15.                 System.out.println(obj);
  16.         }
  17.         public static String charCount(String str)
  18.         {
  19.                 char[] chs =str.toCharArray();

  20.                 TreeMap<Character,Integer> tm =new TreeMap<Character,Integer>();

  21.                 for (int x =0;x<chs.length ;x++ )
  22.                 {
  23.                         if (!(chs[x]>='a' && chs[x]<='z' || chs[x]>='A' && chs[x]<='Z'))
  24.                                 continue;
  25.                         Integer value =tm.get(chs[x]);
  26.                         if (!(value==null))
  27.                         {
  28.                                 value =value +1;
  29.                                 tm.put(chs[x],value);
  30.                         }
  31.                         else{
  32.                                 tm.put(chs[x],1);
  33.                         }
  34.                 }

  35.                 StringBuilder sb  =new StringBuilder();
  36.                
  37.                 Set<Map.Entry<Character,Integer>> entry=tm.entrySet();
  38.                 for (Iterator<Map.Entry<Character,Integer>> it =entry.iterator();it.hasNext() ; )
  39.                 {
  40.                         Map.Entry<Character,Integer> me =it.next();
  41.                         Character key =me.getKey();
  42.                         Integer value =me.getValue();

  43.                         //sop(key+"---+++++----------"+value);
  44.                         sb.append(key+"["+value+"]\n");
  45.                 }

  46.                 return sb.toString();
  47.                
  48.         }
  49. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马