09Java语法回顾map集合
来源:互联网 发布:seed相似的软件 编辑:程序博客网 时间:2024/05/22 15:35
Java语法回顾Map集合
读了那么多年的书让我明白一个道理。人要稳重,不要想到啥就做啥。做一行越久即使你不会,几年之后慢慢的你也会了,加上一点努力你或许你能成为别人眼中的专家。
Map集合的简介
/* * Map:最大的优点就是体现对应关系。 * Map是一个键值对形式的集合。它的数据不再是单个的了,必须同时有键和值组成。 * * Map和Collection的区别? * Map:是(键值对)双列形式的集合;键必须是唯一的,不能重复,值可以重复;看成是夫妻对的集合。 * Collection:是单列值的集合;Collection的List儿子,是可以重复,它的Set儿子是唯一的;看成是单身汉的集合。 * * Map接口的功能: * A:增加功能 * V put(K key,V value):当key在集合中不存在时,添加元素;当key在集合存在时候,替换元素。 * B:删除功能 * void clear():清除所有键值对数据。 * V remove(Object key):根据指定的键删除键值对。 * C:判断功能 * boolean containsKey(Object key):判断指定的键是否在集合中存在 * boolean containsValue(Object vlaue):判断指定的值是否在集合中存在 * boolean isEmpty():判断集合是否为空 * D:获取功能 * Set<Map.Entry<K,V>> entrySet():键值对对象的集合。 * Object get(Object key):根据键获取值 * Set<K> keySet():所有键的集合 * Collection<V> values():所有值的集合 * E:长度功能 * int size() * * 注意:Map集合中的具体实现类的数据结构,是针对键有效。 */
Map集合的遍历
/* * Set<K> keySet():所有键的集合 * Collection<V> values():所有值的集合 */
Map集合的遍历的代码测试
public class MapDemo2 { public static void main(String[] args) { // 创建集合对象 Map<Integer, String> map = new HashMap<Integer, String>(); // 创建并添加元素 map.put(1, "貂蝉"); map.put(2, "黄忠"); map.put(3, "赵云"); // 显示数据 // System.out.println(map); // Set<K> keySet():所有键的集合 Set<Integer> keySet = map.keySet(); for (Integer key : keySet) { System.out.println(key); } //所有值的集合 Collection<String> values = map.values(); for (String value : values) { System.out.println(value); }}
Map集合的遍历方式二的代码测试
/* * Object get(Object key):根据键获取值 * Map集合的遍历。 * 方式1:丈夫找妻子 * A:把所有丈夫给集中起来。Set<K> keySet() * B:遍历丈夫集合,获取到每一个丈夫。迭代器,增强for * C:让丈夫去找妻子。get(Object key) */ Code public class MapDemo3 { public static void main(String[] args) { // 创建集合对象 Map<String, String> map = new HashMap<String, String>(); // 创建并添加元素 map.put("杨过", "小龙女"); map.put("郭靖", "黄蓉"); map.put("梁山伯", "祝英台"); map.put("牛郎", "织女"); // 遍历 // A:把所有丈夫给集中起来。Set<K> keySet() Set<String> husbandSet = map.keySet(); Iterator<String> iterator = husbandSet.iterator(); whiel(iterable.hasNext){ String key = iterable.next; String value = map.get(key); System.out.println(key+"---"+value); } // B:遍历丈夫集合,获取到每一个丈夫。迭代器,增强for for (String husband : husbandSet) { // C:让丈夫去找妻子。get(Object key) String wife = map.get(husband); System.out.println(husband + "***" + wife); } }
Map集合的遍历方式(Map.Entry)的代码测试
/* * Map集合的遍历。 * 通过Map.Entry找key和value。 * A:获取所有Map.Entry的集合。Set<Map.Entry<K,V>> entrySet() * class Map.Entry<K,V> * { * private K key; * private V value; * * public Map.Entry(K key,V value) * { * this.key = key; * this.value = value; * } * * public K getKey() * { * return key; * } * * public V getValue() * { * return value; * } * } * B:遍历Map.Entry集合,获取到每一个Map.Entry对象。迭代器,增强for。 * C:通过Map.Entry对象获取key和value。 */ Code部分 public class MapDemo4 { public static void main(String[] args) { // 创建集合对象 Map<String, String> map = new HashMap<String, String>(); // 创建并添加元素 map.put("杨过", "小龙女"); map.put("郭靖", "黄蓉"); map.put("梁山伯", "祝英台"); map.put("牛郎", "织女"); // Set<Map.Entry<K,V>> entrySet():键值对对象的集合。 //map.entrySet Set<Entry<String,String>> entrySet = map.entrySet(); for (Entry<String, String> entry : entrySet) { String key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"****"+value); } }}
HashMap存储自定义对象并遍历
/* * HashMap存储键和值。并遍历。 * 键:String 学号 * 值:Student (name,age) */CODEpublic class HashMapDemo2 { public static void main(String[] args) { // 创建集合对象 HashMap<String, Student> hm = new HashMap<String, Student>(); // 创建元素对象 Student s1 = new Student("李世民", 30); Student s2 = new Student("朱元璋", 40); Student s3 = new Student("武则天", 50); // 添加元素 hm.put("it001", s1); hm.put("it002", s2); hm.put("it003", s3); // 遍历 Set<String> keySet = hm.keySet(); for (String key : keySet) { Student student = hm.get(key); System.out.println(key+"****"+student.getName()+student.getAge()); } // 遍历2 Set<Map.Entry<String, Student>> hmSet = hm.entrySet(); for (Map.Entry<String, Student> me : hmSet) { String key = me.getKey(); Student value = me.getValue(); System.out.println(key + "***" + value.getName() + "***" + value.getAge()); } }}
TreeMap集合(有序的)
/* * TreeMap存储自定义对象并遍历。 * 键:Student * 值:String * * 如果一个自定义对象做键,用TreeMap集合。 * 就必须要实现排序。 * 两种方式: * A:让自定义对象所属的类去实现Comparable接口 * B:使用带参构造方法,创建TreeMap,接收Comparator接口参数。 */ CODE public class TreeMapDemo2 { public static void main(String[] args) { // 创建集合对象 TreeMap<Student, String> tm = new TreeMap<Student, String>( new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { // int num = s1.getAge() - s2.getAge(); int num = s2.getAge() - s1.getAge(); int num2 = (num == 0) ? s1.getName().compareTo( s2.getName()) : num; return num2; } }); // 创建元素对象 Student s1 = new Student("李世民", 30); Student s2 = new Student("朱元璋", 40); Student s3 = new Student("武则天", 50); Student s4 = new Student("武则天", 50); Student s5 = new Student("武则天", 30); // 添加元素 tm.put(s1, "it001"); tm.put(s2, "it002"); tm.put(s3, "it003"); tm.put(s4, "it004"); tm.put(s5, "it005"); // 遍历 Set<Student> set = tm.keySet(); for (Student key : set) { String value = tm.get(key); System.out.println(key.getName() + "***" + key.getAge() + "***" + value); } }}
Hashtable和HashMap的区别
/* * Hashtable和HashMap的区别? * A:Hashtable线程安全,效率低;不允许null键和值。 * B:HashMap线程不安全,效率高;允许null键和值。 */
HashMap集合循环嵌套使用
/* * czbk: * yr 预热班 * jy 就业班 * 预热班: * 01 zhangsan * 02 lisi * 就业班: * 01 wangwu * 02 zhaoliu * * Map的嵌套。 */
HashMap集合循环嵌套使用代码测试
public class HashMapDemo { public static void main(String[] args) { //最外层HashMap czbk HashMap<String, HashMap<String, String>> czbk = new HashMap<String, HashMap<String, String>>(); //预热班HashMap yr HashMap<String, String> yr = new HashMap<String,String>(); yr.put("01", "zhangsan"); yr.put("02", "lisi"); //就业班HashMap jy HashMap<String, String> jy = new HashMap<String,String>(); jy.put("01", "wangwu"); jy.put("02", "zhaoliu"); czbk.put("就业班", jy); czbk.put("预热班", yr); //获取总集合 键的集合 Set<String> keySet = czbk.keySet(); for (String key : keySet) { System.out.println(key); //根据键的集合获取值 HashMap<String, String> czbkKey = czbk.get(key); // 内部还是一个集合。再获取内部集合的键 Set<String> bjKey = czbkKey.keySet(); for (String key2 : bjKey) { String value = czbkKey.get(key2); System.out.println("\t"+key2+" "+value); } } }}
0 0
- 09Java语法回顾map集合
- 08Java语法回顾collection之set集合
- JAVA集合体系回顾
- Java回顾之集合
- Java回顾之集合
- 东软JavaWeb实训记-DAY1-java语法、集合(List&Map)
- java集合09--Map架构
- java集合09--Map架构
- JAVA集合-Map集合
- java集合------Map集合
- Java集合----Map集合
- Java基础知识——集合体系回顾整理(List Set Map)
- JAVA基础回顾1 遍历List Set 和Map集合的所有方法
- Java基础回顾--jav集合2 Map,List与Set的区别
- 04Java语法回顾_collection
- 03Java语法语法回顾_Date
- Java集合简单回顾总结
- Java 回顾笔记_set集合
- NYOJ88汉诺塔(一)(快速幂&同余定理)
- iOS之实现倒计时功能
- 程序包管理yum&&编译篇
- iOS多线程开发其实很简单
- 孢子框架-互联网金融平台微服务架构设计
- 09Java语法回顾map集合
- 机器学习入门系列二(关键词:多变量(非)线性回归,批处理,特征缩放,正规方程)
- 惯性导航之Runge-Kunta法更新四元数(六)
- spring validate group分类校验
- 2015总结
- 首日
- JavaScript 浮点数及运算精度调整总结
- FM音乐播放器 基于豆瓣 api,swift,alamofire,sqlite.swift 等
- 文章标题