Map中的HashMap集合
来源:互联网 发布:数据切片 编辑:程序博客网 时间:2024/05/29 07:26
Map集合:键值对对应的双链集合,分为:HashMap集合, Hashtable集合,TreeMap集合。
HashMap集合:采用接口多态的方式来创建对象。
HashMap集合的方法:
添加功能:
hashMap.put();
特点:第一次添加的数据返回的是Null值;而后面添加键相同的数据的时候会发生键相同值覆盖的现象!
HashMap的输入顺序与输入顺序是没有关系的即无序的。
程序举例:
package _11.homework;import java.util.HashMap;import java.util.Map.Entry;import java.util.Set;public class HashMapDemo1 { public static void main(String[] args) { HashMap<String, Integer> hashMap = new HashMap<String, Integer>(); hashMap.put("Lily", 21); hashMap.put("Lily", 23); hashMap.put("Sily", 22); hashMap.put("Sandy", 29); hashMap.put("Maria", 32); // Set<String> hashSet=hashMap.keySet(); Set<Entry<String, Integer>> set = hashMap.entrySet(); for (Entry<String, Integer> hashKey : set) { System.out.println(hashKey); } }
程序结果:
//输出结果与输入的顺序无关Sandy=29Lily=23//第二次输入相同键,覆盖了21 岁的LilyMaria=32Sily=22
删除功能:
方法:
void clear();//移除所有键值对元素V remove(Object key)://根据键删除相对应的值元素,并把该值返回String value=hashMap.remove();//根据键名移除相对应键值对并返回
程序举例:
package _11.homework;import java.util.HashMap;import java.util.Map.Entry;import java.util.Set;public class HashMapDemo1 { public static void main(String[] args) { HashMap<String, Integer> hashMap = new HashMap<String, Integer>(); hashMap.put("Lily", 21); hashMap.put("Lily", 23); hashMap.put("Sily", 22); hashMap.put("Sandy", 29); hashMap.put("Maria", 32); //hashMap.clear(); // Set<String> hashSet=hashMap.keySet(); Set<Entry<String, Integer>> set = hashMap.entrySet(); for (Entry<String, Integer> hashKey : set) { System.out.println(hashKey); } System.out.println("----------------------------"); System.out.println(hashMap.remove("Sily")); System.out.println("----------------------------"); Integer value=hashMap.remove("Sandy"); System.out.println(value); }}运行结果:Sandy=29Lily=23Maria=32Sily=22----------------------------22----------------------------29
判断功能:
方法:
boolean containsKey(object key)//判断集合是否包含指定的键boolean containsValue(object value)//判断集合是否包含该值boolean isEmpty()//判断集合是否为空
程序举例:
package _11.homework;import java.util.HashMap;public class HashMapDemo1 { public static void main(String[] args) { HashMap<String, Integer> hashMap = new HashMap<String, Integer>(); hashMap.put("Lily", 23); hashMap.put("Sily", 22); hashMap.put("Sandy", 29); hashMap.put("Maria", 32); // hashMap.clear(); System.out.println(hashMap.containsKey("Lily")); System.out.println("-------"); System.out.println(hashMap.containsValue(28)); System.out.println("-------"); System.out.println(hashMap.isEmpty()); }}运行结果:true-------false-------false
获取功能:
方法:
Set<Entry<k,v>>.entrySet();//返回一个键值对Set集合v.get(object key);//根据键获取值Set<k>keySet();//获取集合中所有键的集合Collection <v> values();//获取集合中所有值的集合
程序举例:
package _11.homework;import java.util.Collection;import java.util.HashMap;import java.util.Map.Entry;import java.util.Set;public class HashMapDemo1 { public static void main(String[] args) { HashMap<String, Integer> hashMap = new HashMap<String, Integer>(); hashMap.put("Lily", 23); hashMap.put("Sily", 22); hashMap.put("Sandy", 29); hashMap.put("Maria", 32); Set<Entry<String,Integer>> set=hashMap.entrySet(); for(Entry<String,Integer> setKey:set){ System.out.println(setKey); } System.out.println("---------------------"); Set<String> hashSet=hashMap.keySet(); for(String hashKey:hashSet){ System.out.println(hashKey+"----"+hashMap.get(hashKey)); } System.out.println("---------------------"); Collection<Integer> value=hashMap.values(); for(Integer keyValue:value){ System.out.println(keyValue); } }}输出结果://Set<Entry<k,v>>.entrySet();//返回一个键值对Set集合Sandy=29Lily=23Maria=32Sily=22//v.get(object key);//根据键获取值//Set<k>keySet();//获取集合中所有键的集合Sandy----29Lily----23Maria----32Sily----22//Collection <v> values();//获取集合中所有值的集合29233222
阅读全文
0 0
- Map中的HashMap集合
- Map集合HashMap嵌套
- Map集合HashMap,TreeMap
- Java集合-Map(HashMap)
- Lesson_for_java_day14--java中的集合——Map、HashMap、TreeMap
- 集合框架 Map之HashMap
- HashMap集合中的TreeMap集合
- java自学笔记12:java中的集合框架(中)Map&HashMap
- Map中的HashMap存储对象
- java中的Map、HashMap、HashTable
- 集合框架Map概述和特点,Map集合的基本功能,Map集合的遍历,HashMap集合,
- 集合框架map学习笔记一HashMap
- Java Map集合之HashMap、HashTable
- 集合(一):Map,HashMap,Hashtable简介
- 【Java总结-集合】Map接口,HashMap等
- Java 集合:Map 系列(HashMap,HashTable)
- java Map集合框架之HashMap
- Java-集合框架Map之HashMap、TreeMap
- 用MC写一个策略
- ifdef,=,+=:=,?=
- 模板方法模式
- OL4叠加显示天地图地图服务之XYZ实例
- canvas的一些基础分享
- Map中的HashMap集合
- html5知识点:DOM编程
- Android 6.0 7.0及以上 动态权限
- jvm学习记录--09 虚拟机类加载机制
- div模拟textarea实现高度随内容自增长
- SPY++用法
- 关于VC++6.0下出现error C2065: 'LPDIRECTDRAWSURFACE7' : undeclared identifier解决方法
- 摩尔定律还有效吗
- [PYTHON]文件系统操作os 和shutil