java------集合Map
来源:互联网 发布:centos中文输入法 编辑:程序博客网 时间:2024/05/19 17:03
Map: 以键值对的形式存储对象,和数组非常相似,在数组中存在的索引,它们本身也是对象。
1,HashMap :基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。
2,TreeMap :底层 二叉树。TreeMap是按照key进行排序的。用法跟HashMap基本一致
HashMap与TreeMap区别:
HashMap性能优于TreeMap。通常使用HashMap,在我们需要对其中元素排序的时候才使用TreeMap。
下面是常用方法:
<span style="font-size:14px;">/** * Map集合: 该集合存储键值对,一对一对往里存,而且要保证键的唯一性。 * 1,添加。 * put(K key, V value) * putAll(Map<? extends K,? extends V> m) * * 2,删除. * clear(); * remove(Object key) * 3,判断。 * containsValue(Object value) * containsKey(Object key) * * 4,获取 * get(Object key) * size() * values() * * entrySet() * keySet() * Map * |---Hashtable:底层是哈希表数据结构,不可以存入null键null值,该集合线程同步,效率低 * |---HashMap:底层是哈希表数据结构,允许使用null键和null值,该集合线程不同步,效率高 * |---TreeMap:底层是二叉树数据结构,线程不同步,可以用于给map集合中的键进行排序 * * 和Set很像。 * 其实,Set底层就是使用了Map集合 * */import java.util.*;public class HashMapDemo{ public static void sop(Object obj) {System.out.println(obj); } public static void main(String[] args) {HashMap<String, String> map = new HashMap<String, String>();// 1 添加元素 put()方法map.put("01", "zhangsan1");map.put("02", "zhangsan2");map.put("03", "zhangsan3");map.put("04", "zhangsan4");sop(map);//1.2添加元素 允许添加nullmap.put("05", null);//1.4 添加同样键 新的值会覆盖旧的值,并put方法会返回被覆盖的值sop("put:" + map.put("06", "zhangsan06"));sop("put:" + map.put("06", "wangwu06"));//2,删除元素sop("remove:" +map.remove("04"));//删除的时候会根据键找到值sop(map);//3,获取元素sop("get:" + map.get("02"));//获得"01"键对应的值//也可以通过get方法的返回值来判断一个键是否存在//3.2 获取map所有元素的值Collection<String> coll = map.values();sop(coll); }}</span>
下面是Map中 的两种取出方式:
第一种:
<span style="font-size:14px;">/** * map集合的两种取出方式 * * 1, keySet:Key键 Set集合: 将map中所有的键存入到Set集合。 * 因为set具备迭代器,所以可以迭代方式去除所有的键,再根据get方法, * 获取每一个键对应的值 * * 2, entrySet:将map集合中的映射关系存入到Set集合中, * 而这个类的关系数据类型为: Map.Entry * */import java.util.*;public class HashMapDemo1{ public static void sop(Object obj) {System.out.println(obj); } public static void main(String[] args) {Map<String,String> map = new HashMap<String,String>(); map.put("02", "zhangsan02");map.put("01", "zhangsan01");map.put("03", "zhangsan03");map.put("04", "zhangsan04");//现获取map集合中所有Set集合,keySet()Set<String> keySet = map.keySet();//有了 Set集合,就可以获取器迭代器。Iterator<String> it = keySet.iterator();//迭代器也要泛型while(it.hasNext()){ String key = it.next(); //有了键可以通过map集合中get方法获取其对应的值 String Value = map.get(key); sop("key:" + key + " Value:" + Value);} }}</span>
下面是第二种取出方式:
import java.util.*;public class HashMapDemo2{ public static void main(String[] args) {HashMap<String, String> map = new HashMap<String,String>();map.put("02", "lisi02");map.put("01", "lisi01");map.put("03", "lisi03");map.put("04", "lisi04");//将Map集合中的映射关系取出,存入到Set集合中。Set<Map.Entry<String,String>> entrySet = map.entrySet();Iterator <Map.Entry<String, String>> it = entrySet.iterator();while(it.hasNext()){ Map.Entry<String,String> me = it.next(); String key = me.getKey(); String value = me.getValue(); System.out.println("key:" + key +" value:" + value);} }}
下面是Map应用:
<span style="font-size:14px;">/** * Map 应用: 需求 每一个学生都有对应的归属地。 学生Student,地址String 学生属性: 姓名, 年龄 注意:姓名和年龄相同的视为同一个学生。 * 保证学生的唯一性。 * * 1, 描述学生的唯一性。 * * 2,定义map容器,将学生作为键,地址作为值,存入。 * * 3,获取map集合中的元素 * */import java.util.*;class Student implements Comparable<Student>{ private String name; private int age; Student(String name, int age) {this.name = name;this.age = age; } public int compareTo(Student s)// 比较的 {int num = new Integer(this.age).compareTo(new Integer(s.age));if (num == 0){ return this.name.compareTo(s.name);}return num; } public int hashCode()// 需要复写哈希码 {return name.hashCode()+age*34; } public boolean equals(Object obj)// 判断相等 复写 equals方法 {if (!(obj instanceof Student)){ throw new ClassCastException("类型不匹配");}Student s = (Student) obj;return this.name.equals(s.name) && this.age == s.age; } public String getName() {return name; } public int getAge() {return age; } public String toString()//重写 tuString方法的 这样在打印 Student对象时 打印的 {return name + ":" + age;//是 姓名 和年龄 而不是 哈希码 }}public class MapTest{ public static void main(String[] args) {/*Student st1 = new Student("liu01", 23);Student st2 = new Student("liu02", 24);Student st3 = new Student("liu03", 22);Student st4 = new Student("liu04", 21); */HashMap<Student, String> hm = new HashMap<Student, String>();hm.put(new Student("liu01", 24), "beijing");//hm.put(new Student("liu01", 22), "sichuan");//存入这个元素后覆盖上面的那个元素 因为复写了hm.put(new Student("liu03", 23), "shanghai");hm.put(new Student("liu04", 24), "wuhan");hm.put(new Student("liu02", 22), "nanjing");// 第一种取出方式 keySetSet<Student> keySet = hm.keySet();// 尖括号 泛型Iterator<Student> it = keySet.iterator();// 这里也用到泛型while (it.hasNext()){ Student stu = it.next(); String add = hm.get(stu); System.out.println(stu + "...." + add);//这里打印学生对象的时候也可以使用getName() getAge() //来获取学生的姓名和年龄}System.out.println("下面是第二种取出方式");//第二种取出方式 entrySet 数据类型为Map.EntrySet <Map.Entry<Student, String>> entrySet = hm.entrySet();Iterator<Map.Entry<Student, String>> itera = entrySet.iterator();while(itera.hasNext()){ Map.Entry<Student, String> me = itera.next(); Student stu = me.getKey(); String add = me.getValue(); System.out.println(stu + "...." + add); } }}</span>
下面是分别打印字符串个数
/** * 练习: * “asdfzxcvasdfzxcvf"获取该字符串出现的次数。 * 希望打印结果 :a(1) c(2)。。。。。 * 通过结果发信,每一个字母出现都有对应的次数。 * 说明字母和次数之间都有映射关系。 * * 当发现有映射关系时,可以选择map集合。 * 因为map集合中存放就是映射关系。 * * * 为什么使用map集合呢? * 当数据之间存在这种映射时,就要先想map集合 * 思路: * 1,将字符串转换成字符数组。因为要对每一个最进行操作。 * * 2, 定义一个Map集合,因为打印结果的字母有顺序,所以使用tremap集合。 * * 3,遍历字符数组。 * 将每一个字母作为键去查map集合, * 如果返回null,将该字母和1存入到Map集合。 * 如果返回不是null,说明该字母在map集合已经存在并有对应次数。 * 那么就获取该次数并进行自增。 然后将该字母和自增后的次数存入到map集合中 * * 4,将map集合中的数据变成指定的字符串形式返回。 * */import java.util.*;public class MapTest2{ public static void main(String[] args) {String s =charCount("asdsfasdf");System.out.println(s); } public static String charCount(String str) {char[] chs = str.toCharArray();TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();for(int x = 0; x<chs.length; x++){ Integer value = tm.get(chs[x]); if(value == null) {tm.put(chs[x], 1); } else {value = value +1;tm.put(chs[x],value); }}//System.out.println(tm);StringBuilder sb = new StringBuilder();//缓冲区Set<Map.Entry<Character, Integer>> entrySet = tm.entrySet();Iterator<Map.Entry<Character, Integer>> it = entrySet.iterator();while(it.hasNext()){ Map.Entry<Character, Integer> me = it.next(); Character ch = me.getKey(); Integer value = me.getValue(); sb.append(ch + "("+ value +")");}return sb.toString(); } }
**********************************************************************************************************************************************************************************
TreeMap:
import java.util.*;/** * 需求 :对学生对象年龄进行排序 * 因为集合是以键值对形式存在的 * 所以需要使用可以排序的集合TreeMap * * */public class TreeMapTest{ public static void main(String[] args) {TreeMap<Student, String> tm = new TreeMap<Student, String>();//这里的Student是上个程序的tm.put(new Student("lisi01",21), "beijing");tm.put(new Student("lisi03",23), "tianjin");tm.put(new Student("lisi02",22), "wuhan");tm.put(new Student("lisi05",24), "shanghai");Set<Map.Entry<Student, String>> entry = tm.entrySet();Iterator<Map.Entry<Student, String>> it = entry.iterator();while(it.hasNext()){ Map.Entry<Student, String> me = it.next(); Student stu = me.getKey(); String addr = me.getValue(); System.out.println(stu + "....." + addr);} }}
*************
和上题类似,改变排序的要求:
import java.util.Comparator;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.TreeMap;/** * 需求 :对学生对象姓名进行排序 * 因为集合是以键值对形式存在的 * 所以需要使用可以排序的集合TreeMap * 按照姓名 是自身不具备的 所以要建立比较器 * */class BiJiao implements Comparator<Student>//和前面的例子类似 建立比较器{ public int compare(Student s1,Student s2) {int num = s1.getName().compareTo(s2.getName());if(num == 0){ return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));}return num; } }public class TreeMapTest2{ public static void main(String[] args) {TreeMap<Student, String> tm = new TreeMap<Student, String>(new BiJiao());//这里的Student是上程序的//还要把 比较器传进来 就是 New BiJiaotm.put(new Student("lisi01",21), "beijing");tm.put(new Student("lisi03",23), "tianjin");tm.put(new Student("lisi02",22), "wuhan");tm.put(new Student("lisi05",24), "shanghai");Set<Map.Entry<Student, String>> entry = tm.entrySet();Iterator<Map.Entry<Student, String>> it = entry.iterator();while(it.hasNext()){ Map.Entry<Student, String> me = it.next(); Student stu = me.getKey(); String addr = me.getValue(); System.out.println(stu + "....." + addr);} }}
- JAVA集合-Map集合
- java集合------Map集合
- Java集合----Map集合
- java集合------Map集合总结
- Java集合之-Map集合
- Java集合—Map集合
- java集合----------Map
- java笔记 Map集合
- java------集合Map
- java遍历Map集合
- java Map集合练习
- java集合(Map)
- java 遍历map集合
- java之Map集合
- Java 集合(Map)
- java 循环map 集合
- Java-Map集合
- java集合类Map
- gnuplot shell-script for example
- 找单词 + 母函数
- IOS 仿网易 banner scrollview 到头后 手势 事件提交到下级 拉开界面的效果
- Spring MVC学习(六)-------注解式控制器详解5
- IOS开发(二)之程序首次启动时加载关于程序应用的简介
- java------集合Map
- 里面有qq农场的WZ132
- SSH 框架下OSCache配置
- 基于大规模语料的新词发现算法
- mysql的TIMESTAMP类型,是否在insert时自动标记时间日期?
- Windows Phone 7 – 写一个会Record Audio的录音程式
- ICE Manual(Documentation for Ice 3.5)---The Ice Protocol(Protocol Compression)
- mysql 远程连接
- MC新手入门(四十五)------游戏网络功能二