Hashmap的简单使用
来源:互联网 发布:雷蛇云驱动mac版怎么用 编辑:程序博客网 时间:2024/04/30 23:30
关于hashmap的一些使用
例一 用iterator迭代器查看内容
//功能: 尝试hashmap的使用package com_1;import java.util.HashMap;import java.util.Iterator;public class hashmap_test { /** * @param args */ public static void main(String[] args) { HashMap hs=new HashMap(); hs.put("name", "张三"); hs.put("sex", "男"); hs.put("age", "30"); hs.put("home", "河北"); //测试是否包含关键字"name" System.out.println(hs.containsKey("name"));//返回true System.out.println(hs.get("name"));//返回 张三 System.out.println(hs.entrySet());//返回 [home=河北, sex=男, age=30, name=张三] System.out.println(hs.hashCode()); //返回7960688 System.out.println(hs.keySet()); //返回 [home, sex, age, name] //1 测试entrySet().的用法 Iterator it=hs.entrySet().iterator();//迭代程序 System.out.println("\n测试entrySet().的用法"); while(it.hasNext()) { System.out.print(it.next() +"\t"); } //2 Set keySet()返回关键字各值的集合 it=hs.keySet().iterator(); System.out.println("\n\nSet keySet()返回关键字各值的集合"); while(it.hasNext()) { System.out.print(hs.get(it.next())+"\t"); } //3 测试 values()的用法 it=hs.values().iterator(); System.out.println("\n\n测试 values()的用法"); while(it.hasNext()) { System.out.print(it.next()+"\t"); } }}
例二 用tostring查看内容
//功能: 尝试hashmap的使用2package com_1;import java.util.Collection;import java.util.HashMap;import java.util.Map;import java.util.Set;public class hashmap_test { public static void main(String[] args) { Map<String, Employee> staff = new HashMap<String, Employee>(); staff.put("144-25-5464", new Employee("Amy",15000)); staff.put("567-24-2546", new Employee("Harry",23000)); staff.put("157-62-7935", new Employee("Gray",36000)); staff.put("456-62-5527", new Employee("France",67000)); System.out.println(staff); staff.remove("567-24-2546");//删除 System.out.println(staff); staff.put("456-62-5527", new Employee("Bob",76000));//替换 System.out.println(staff); System.out.println(staff.get("157-62-7935"));//查询 //取得map中所有的key和value for(Map.Entry<String, Employee> entry : staff.entrySet()) { String key = entry.getKey(); Employee value = entry.getValue(); System.out.println("key=" + key + ", value=" + value + ""); } //取得map中所有的key Set<String> keys = staff.keySet(); for(String key : keys) { System.out.println(key); } //取得map中所有的value Collection<Employee> values = staff.values(); for(Employee value : values) { System.out.println(value); } }}class Employee { public Employee(String name,double salary) { this.name = name; this.salary =salary; } //这句话很重要,println staff 的时候需要 public String toString() { return "[名字=" + name + ", \t薪水=" + salary + "]"; } private String name; private double salary;}
0 0
- Hashmap的简单使用
- HashMap的简单使用
- hashmap使用的简单案例
- HashMap的简单使用教程
- android-ListActivity与HashMap的简单使用
- HashMap的使用,简单小代码
- HashMap简单使用
- hashmap 的简单实现
- HashMap的简单示例
- HashMap 的简单介绍
- HashMap的简单实现
- java中HashMap简单使用
- ArrayList、TreeSet、TreeMap、HashSet和HashMap的简单使用示例
- JAVA 集合Map的简单使用(HashMap)
- 简单的hashmap的实现
- HashMap的一道简单例子
- 遍历 HashMap简单的方式
- java HashMap的简单实现
- 正则表达式使用
- struts2
- xcode7接入iOS友盟分享问题处理
- 欢迎使用CSDN-markdown编辑器
- struts2 配置文件
- Hashmap的简单使用
- Groovy笔记
- IOS加密api数据接口
- 推荐几款实用的Android Studio 插件
- Json学习
- iOS-全屏右滑返回手势
- 自定义AndroidStudio Locat的输出颜色与Log的级别
- Photoshop 扣图像制作PNG标志
- 两种方法实现Apache防盗链