为什么要学双列集合?

因为单列集合无法处理映射关系,会有成对出现的数据

Map接口  如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复

  Map接口的方法:
      添加:
         put(K key, V value)
         putAll(Map<? extends K,? extends V> m) 
      删除
         remove(Object key)
         clear()

      获取:
         get(Object key)
         size() 
      判断:
         containsKey(Object key)
         containsValue(Object value)
         isEmpty()

    迭代:
        keySet()
        values()
        entrySet()

import java.util.HashMap;
import java.util.Map; public class Demo1 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
//添加
map.put("李杰","李英");
map.put("黎晨辉","王成娟");
map.put("蛮王","艾希");
map.put("zhangsan", "999999");
System.out.println(map.put("蛮王", "寡妇"));//寡妇把艾希替换了返回艾希,被替换的
System.out.println(map.put("zhangsan", "value"));//如果前面没有添加就返回null
System.out.println(map);
Map<String,String> map1 = new HashMap<String,String>();
map1.put("文章", "马伊琍");
map1.put("谢霆锋","张柏芝");
map1.put("成龙", "林凤娇");
map.putAll(map1);
System.out.println(map);
//删除
//System.out.println("被删除的数据: "+map.remove("蛮王"));//根据键值删除数据,返回的是删除的建对应的值
//System.out.println(map);
//map.clear();
//System.out.println(map); //获取
System.out.println(map.get("蛮王"));//根据指定的建获取值
System.out.println(map.size());//键值对个数 //判断
System.out.println(map.containsKey("蛮王"));//判断集合是否包含指定的建
System.out.println(map.containsValue("张柏芝"));//判断集合是否包含指定的值
System.out.println(map.isEmpty());
} }
//双列集合没有迭代器,所以在遍历双列集合的时候要返回单列集合,然后借用单列集合的迭代器
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; public class Demo2 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("李杰","李英");
map.put("黎晨辉","王成娟");
map.put("蛮王","艾希");
/*
//map集合中遍历方式一: 使用keySet方法进行遍历 缺点: keySet方法只是返回了所有的键,没有值
Set<String> keysView = map.keySet();
System.out.println(keysView); //keySet() 把Map集合中的所有键都保存到一个Set类型 的集合对象中返回。
Iterator<String> it = keysView.iterator();
while(it.hasNext()) {
//System.out.println(it.next());
String list = it.next();
System.out.print("{键:"+list+",值:"+map.get(list)+"}");
System.out.println();
} //map集合的遍历方式二: 使用values方法进行 遍历 缺点: values方法只能返回所有 的值,没有键
Collection<String> valuesView = map.values();
System.out.println(valuesView);
Iterator<String> ir = valuesView.iterator();
while(ir.hasNext()) {
System.out.print(ir.next()+" ");
}
*/
//map集合的遍历方式三: entrySet方法遍历
Set<Map.Entry<String,String>> entrylist = map.entrySet();
Iterator<Map.Entry<String,String>> io = entrylist.iterator();
while(io.hasNext()){
Map.Entry<String,String> entry = io.next();
System.out.println("键:"+ entry.getKey()+" 值:"+ entry.getValue());
}
}
}

HashMap和TreeMap和Hashtable(了解)

  HashMap  底层也是基于哈希表实现 的。
    HashMap的存储原理:
       往HashMap添加元素的时候,首先会调用键的hashCode方法得到元素 的哈希码值,然后经过运算就可以算出该
       元素在哈希表中的存储位置
       情况1: 如果算出的位置目前没有任何元素存储,那么该元素可以直接添加到哈希表中

      情况2:如果算出 的位置目前已经存在其他的元素,那么还会调用该元素的equals方法与这个位置上的元素进行比较
       ,如果equals方法返回 的是false,那么该元素允许被存储,如果equals方法返回的是true,那么该元素被视为
       重复元素,不允存储

import java.util.HashMap;

class person{
int id;
String name; public person(int id, String name) {
super();
this.id = id;
this.name = name;
} public String toString() {
return "[编号:"+this.id+" 姓名:"+ this.name+"]";
} @Override
public int hashCode() {
// TODO Auto-generated method stub
return this.id;
}
@Override
public boolean equals(Object obj) {
person p = (person) obj;
// TODO Auto-generated method stub
return this.id == p.id;
}
} public class Demo3 {
public static void main(String[] args) {
HashMap<person,String> map = new HashMap<person,String>();
map.put(new person(110,"李杰"), "lalala");
map.put(new person(120,"李英"), "hahaha");
map.put(new person(119,"李汉斯"), "xixixi");
map.put(new person(119,"李汉斯"), "qiqiqi");
System.out.println(map);
}
}

  TreeMap  TreeMap也是基于红黑树(二叉树)数据结构实现的    特点:会对元素的键进行排序存储

    TreeMap 要注意的事项:
       1.往TreeMap添加元素的时候,如果元素的键具备自然顺序,那么就会按照键的自然顺序特性进行排序存储
       2.往TreeMap添加元素的时候,如果元素的键不具备自然顺序特性, 那么键所属的类必须要实现Comparable接口,把键
        的比较规则定义在CompareTo方法上

      3. 往TreeMap添加元素的时候,如果元素的键不具备自然顺序特性,而且键所属的类也没有实现Comparable接口,那么就必须
          在创建TreeMap对象的时候传入比较器

import java.util.Comparator;
import java.util.TreeMap; class Person {
String name;
int salary;
public Person( String name,int salary) {
super();
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "[姓名:"+this.name+" 薪水:"+ this.salary+"]";
}
} class mycompare implements Comparator<Person>{ @Override
public int compare(Person o1, Person o2) {
// TODO Auto-generated method stub
return o1.salary - o2.salary;
} } public class Demo3 {
public static void main(String[] args) { /*TreeMap<Character, Integer> tree = new TreeMap<Character, Integer>();
tree.put('c',10);
tree.put('b',2);
tree.put('a',5);
tree.put('h',12);
System.out.println(tree);*/
TreeMap<Person,Integer> tree = new TreeMap<Person,Integer>(new mycompare());
tree.put(new Person("李杰",110), 001);
tree.put(new Person("李英",120), 002);
tree.put(new Person("李汉斯",119), 003);
System.out.println(tree); }
}
import java.util.Comparator;
import java.util.TreeMap; class Person implements Comparable{
String name;
int salary;
public Person( String name,int salary) {
super();
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "[姓名:"+this.name+" 薪水:"+ this.salary+"]";
} @Override
public int compareTo(Object o) {
Person a = (Person) o;
return this.salary - a.salary;
} } public class Demo3 {
public static void main(String[] args) { /*TreeMap<Character, Integer> tree = new TreeMap<Character, Integer>();
tree.put('c',10);
tree.put('b',2);
tree.put('a',5);
tree.put('h',12);
System.out.println(tree);*/
TreeMap<Person,Integer> tree = new TreeMap<Person,Integer>();
tree.put(new Person("李杰",110), 001);
tree.put(new Person("李英",120), 002);
tree.put(new Person("李汉斯",119), 003);
System.out.println(tree); }
}

作业: 定义一个TreeMap,键存储的是书对象,值存储的是字符串。 根据书的出版出版日期排序

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap; class Book<T> implements Comparable<Book>{ String name;
String date;
public Book(String name,String string) {
super();
this.name = name;
this.date = string;
}
/*
@Override
public int compareTo(Book o) { return this.date.compareTo(o.date);
}
*/
public int compareTo(Book o) {
SimpleDateFormat dataformat = new SimpleDateFormat("yyyy-mm-dd");
Date date1 = null;
Date date2 = null;
try {
date1 = dataformat.parse(this.date);
date2 = dataformat.parse(o.date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return date1.compareTo(date2);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "{书名:"+this.name+"出版日期:"+this.date+",出版社:}";
}
} public class Demo4 { public static void main(String[] args) {
TreeMap<Book, String> map = new TreeMap<Book, String>(); //值存储书名
map.put(new Book("《红楼梦》","2018-7-25"), "商务出版社");
map.put(new Book("《西游记》","2018-7-26"), "商务出版社");
map.put(new Book("《水浒传》","2018-7-27"), "商务出版社");
map.put(new Book("《三国演义》","2018-7-28"), "商务出版社");
System.out.println(map);
} }

HashTable  底层依赖哈希表存在的,实现方式与HashMap一直,但是他是线程安全的,操作效率低



Day 9:双列集合Map及实现该接口的类的常用方法的更多相关文章

  1. 双列集合Map

    1.双列集合Map,就是存储key-value的键值对. 2.hashMap中键必须唯一,值可以不唯一. 3.主要方法:put添加数据    getKey---通过key获取数据    keySet- ...

  2. 双列集合Map的嵌套遍历

    双列集合Map的嵌套使用,例如HashMap中还有一个HashMap,这样的集合遍历起来稍微有点儿复杂.例如一个集合:HashMap<Integer,HashMap<String,Inte ...

  3. (10)集合之双列集合Map,HashMap,TreeMap

    Map中的元素是两个对象,一个对象作为键,一个对象作为值.键不可以重复,但是值可以重复. 看顶层共性方法找子类特有对象. Map与Collection在集合框架中属并列存在 Map存储的是键值对 Ma ...

  4. 双列集合Map相关面试题

    一.了解Map集合吗?Map集合都有哪些实现 HashMap HashTable LinkedHashMap TreeMap ConcurrentHashMap 二.HashMap和HashTable ...

  5. 双列集合Map接口 & Collections工具类

    HashMap 常用方法 遍历方式 iterator迭代器  ITIT HashTable 继承字典 Hashtable--Properties 文件读写 总结 Collections工具类

  6. Map集合(双列集合)

    Map集合(双列集合)Map集合是键值对集合. 它的元素是由两个值组成的,元素的格式是:key=value. Map集合形式:{key1=value1 , key2=value2 , key3=val ...

  7. Map集合——双列集合

    双列集合<k, v> Map: Map 和 HashMap是无序的: LinkedHashMap是有序的: HashMap & LinkedHashMap: put方法: 其中,可 ...

  8. Java之Map接口(双列集合)

    Map集合概述 现实生活中,我们常会看到这样的一种集合:IP地址与主机名,身份证号与个人,系统用户名与系统用户对象等,这种一一对应的关系,就叫做映射.Java提供了专门的集合类用来存放这种对象关系的对 ...

  9. 56. Map(双列集合)

    在生活中有些数据是以映射关系存在的,也就是成对出现的,比如:老公  老婆(key-->value) 双列集合:-------------------| Map    如果是实现了Map接口的集合 ...

随机推荐

  1. centos7下yourcompleteme安装

    以前装过一回,没成功,现在再来一次 yourcompleteme git https://github.com/ycm-core/YouCompleteMe#installation 检查软件版本 v ...

  2. HDU 5523:Game

    Game  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) 问题描述 XY在玩一 ...

  3. jpa自定义sql语句

    /** * 查询还没生成索引的帖子 * @return */ @Query(value = "SELECT * FROM t_article WHERE index_state=0" ...

  4. HashMap之Hash碰撞源码解析

    转自:https://blog.csdn.net/luo_da/article/details/77507315 https://www.cnblogs.com/tongxuping/p/827619 ...

  5. C++连接sqlite数据库的坑

    新的第一次用vs2013搞 C++连接sqlite数据库,遇到了很多问题,我也不搞不懂~~~下面写点小体会 首先: 你要先配置好sqlite的环境 参考链接: https://blog.csdn.ne ...

  6. 传入sql语句,执行完提取内容赋值到控件上

    class procedure DBTools.FillStrings(ComboBoxEh: TDBComboBoxEh; sql: string; Default: Boolean = False ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-stop

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. POJO,JavaBean,entity的理解

    POJO本质是就是JavaBean JavaBean JavaBean实际上是指一种特殊的Java类,它通常用来实现一些比较常用的简单功能,并可以很容易的被重用或者是插入其他应用程序中去.所有遵循“一 ...

  9. Linux每日练习-awk命令的内外部变量传递20200225

  10. HYSBZ - 1588 营业额统计 (伸展树)

    题意:营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营 ...