/*Map集合的两种 取出方式
 * 1、keySet()
 * 2、entrySet()
 * */

//定义一个学生类 重写了equals、hashcode三个方法,实现了comparable接口并覆盖comparato方法

package 集合;

public class Student implements Comparable<Student>{
    private String name;
    private int age;
    
    public Student(){}
    public Student(String name,int age){
        this.name= name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public int hashCode() {
        
        return this.name.hashCode()+age*39;
    }
    
    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Student))
            throw new ClassCastException("转换类型不匹配");
        Student student = (Student)obj;
        return this.name.equals(student.getName()) && this.age == student.getAge();
    }
    
    public int compareTo(Student stu) {
        int num = new Integer(this.age).compareTo(new Integer(stu.getAge()));
        if(num == 0)
            return this.name.compareTo(stu.getName());
        return num;
            
    }
    @Override
    public String toString() {
        return  name + age;
    }
    
}

//定义一个HashMAP类

import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapTest1 {

public static void main(String[] args) {
        HashMap<Student,String> hm = new HashMap<Student,String>();//不是treeMap调用不到comparato方法
        hm.put(new Student("aisi1",22), "上海");
        hm.put(new Student("disi2",24), "上海");
        hm.put(new Student("cisi4",21), "上海");
        hm.put(new Student("aisi3",25), "上海");
        
        
        //第一种取出方式 keyset
        Set<Student> keySet = hm.keySet();
        Iterator<Student> it = keySet.iterator();
        while(it.hasNext()){
            Student stu = it.next();
            String address = hm.get(stu);
            System.out.println(stu+":"+address);
        }
        System.out.println("===================");
        //第二种取出方式entrySet
        Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
        Iterator<Map.Entry<Student,String>> it1 = entrySet.iterator();
        while(it1.hasNext()){
            Map.Entry<Student, String> me = it1.next();
            Student stu = me.getKey();
            String addr = me.getValue();
            
            System.out.println(stu+":"+addr);
        }
    }

}

Map的keySet和entrySet的更多相关文章

  1. Map中keySet和entrySet的区别

    在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet():将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键, ...

  2. 【Java学习笔记】Map集合的keySet,entrySet,values的用法例子

    import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.M ...

  3. JAVA之旅(二十二)——Map概述,子类对象特点,共性方法,keySet,entrySet,Map小练习

    JAVA之旅(二十二)--Map概述,子类对象特点,共性方法,keySet,entrySet,Map小练习 继续坚持下去吧,各位骚年们! 事实上,我们的数据结构,只剩下这个Map的知识点了,平时开发中 ...

  4. keySet,entrySet用法 以及遍历map的用法

    Set<K> keySet() //返回值是个只存放key值的Set集合(集合中无序存放的)Set<Map.Entry<K,V>> entrySet() //返回映 ...

  5. map集合中value()、keySet()、entrySet()区别

    在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet():将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键, ...

  6. Map容器中keySet()、entrySet()

    1.定义 keySet(): 返回的是只存放key值的Set集合,使用迭代器方式遍历该Set集合,在迭代器中再使用get方法获取每一个键对应的值.使用get方法获取键对应的值时就需要遍历Map集合,主 ...

  7. 牛客网Java刷题知识点之Map的两种取值方式keySet和entrySet、HashMap 、Hashtable、TreeMap、LinkedHashMap、ConcurrentHashMap 、WeakHashMap

    不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...

  8. Map的常用方法keySet()、entrySet()

    Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是Map中key值的集合:en ...

  9. keyset与entryset

    1.基本概述 Set<Map.Entry<K,V>> entrySet()  返回此映射中包括的映射关系的 set 视图. Set<K>              ...

随机推荐

  1. 在一个aspx或ashx页面里进行多次ajax调用

    在用ajax开发asp.net程序里.利用ashx页面与前台页面进行数据交互.但是每个ajax交互都需要一个ashx页面.结果是项目里一大堆ashx页面.使项目难以管理.现在我们就想办法让一个ashx ...

  2. win10进入安全模式的方法(F8不管用/开不开机)

    win10默认不能进入安全模式,这时候开机黑屏怎么办?下面介绍强制进入安全模式的方法 1. 关机情况下,按开机键开机,等到出现徽标(下图),这时候长按开机键强制关机. 2. 再次开机,出现徽标,再次强 ...

  3. javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

    SSL握手失败:用JDK1.8做发邮件的功能遇到这种问题处理方式是:将目录 %JAVA_HOME%\...\jre\lib\security里的local_policy.jar,US_export_p ...

  4. IBM CLI 和 ODBC

    Installing and Configuring DB2 Clients Running CLI/ODBC Programs The DB2 Call Level Interface (CLI) ...

  5. Datazen介绍

    Datazen是移动端全平台的图表解决方案,基于HTML5的应用,实现了全平台的整合.此篇主要对其功能进行大体介绍. 这个平台最近刚被微软收购,相信微软看重的是其HTML5在全移动端平台的实现.Dat ...

  6. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  7. 如何托管ASP.NET Core应用到Windows Service中

    (此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...

  8. SharedPreferences 详解

    获取SharedPreferences的两种方式: 1 调用Context对象的getSharedPreferences()方法 2 调用Activity对象的getPreferences()方法 两 ...

  9. git学习(四):撤销修改和撤销删除

    修改有两种情况 在工作区修改但没有add到暂存区 git checkout -- <file> 在工作区修改了也add到暂存区 git reset HEAD <file> 先撤 ...

  10. 基于.NET平台常用的框架整理

    自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生了浓厚的兴趣,在工作和学习中也积累了一些开源的组件,就目前想到的先整理于此,如果再想到,就 ...