您的位置:首页 > 其它

集合框架-HashMap集合的案例

2017-04-30 10:02 337 查看
(5)HashMap集合的练习

A:HashMap<String,String>

package cn.itcast_02;

import java.util.HashMap;
import java.util.Set;

/*
* HashMap:是基于哈希表的Map接口实现。
* 哈希表的作用是用来保证键的唯一性的。
*
* HashMap<String,String>
* 键:String
* 值:String
*/
public class HashMapDemo {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, String> hm = new HashMap<String, String>();

// 创建元素并添加元素,这样添加写可以,但是一般还是按下面的写法
// String key1 = "it001";
// String value1 = "马云";
// hm.put(key1, value1);

hm.put("it001", "马云");
hm.put("it003", "马化腾");
hm.put("it004", "乔布斯");
hm.put("it005", "张朝阳");
hm.put("it002", "裘伯君"); // 一个人开发了wps,第一代程序员
hm.put("it001", "比尔盖茨");
//加了比尔盖茨,马云就被替换了
//因为键是唯一的,String重写了hashcode()和equals()

// 遍历
Set<String> set = hm.keySet();
for (String key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
}
}
}



B:HashMap<Integer,String>

package cn.itcast_02;

import java.util.HashMap;
import java.util.Set;

/*
* HashMap<Integer,String>
* 键:Integer
* 值:String
*/
public class HashMapDemo2 {
public static void main(String[] args) {
// 创建集合对象
HashMap<Integer, String> hm = new HashMap<Integer, String>();

// 创建元素并添加元素
// Integer i = new Integer(27);
// Integer i = 27;
// String s = "林青霞";
// hm.put(i, s);

hm.put(27, "林青霞");
hm.put(30, "风清扬");
hm.put(28, "刘意");
hm.put(29, "林青霞");

// 下面的写法是八进制,但是不能出现8以上的单个数据
// hm.put(003, "hello");
// hm.put(006, "hello");
// hm.put(007, "hello");
// hm.put(008, "hello");

// 遍历
Set<Integer> set = hm.keySet();
for (Integer key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
}

// 下面这种方式仅仅是集合的元素的字符串表示.不是遍历
// System.out.println("hm:" + hm);
}
}

C:HashMap<String,Student>

package cn.itcast_02;

import java.util.HashMap;
import java.util.Set;

/*
 * HashMap<String,Student>
 * 键:String    学号
 * 值:Student 学生对象
 */
public class HashMapDemo3 {
    public static void main(String[] args) {
        // 创建集合对象
        HashMap<String, Student> hm = new HashMap<String, Student>();

        // 创建学生对象
        Student s1 = new Student("周星驰", 58);
        Student s2 = new Student("刘德华", 55);
        Student s3 = new Student("梁朝伟", 54);
        Student s4 = new Student("刘嘉玲", 50);

        // 添加元素
        hm.put("9527", s1);
        hm.put("9522", s2);
        hm.put("9524", s3);
        hm.put("9529", s4);

        // 遍历
        Set<String> set = hm.keySet();
        for (String key : set) {
            // 注意了:这次值不是字符串了
            // String value = hm.get(key);
            Student value = hm.get(key);
            System.out.println(key + "---" + value.getName() + "---"+ value.getAge());
        }
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------

D:HashMap<Student,String>

package cn.itcast_02;

import java.util.HashMap;
import java.util.Set;

/*
* HashMap<Student,String>
* 键:Student
* 要求:如果两个对象的成员变量值都相同,则为同一个对象。
* 值:String
*/
public class HashMapDemo4 {
public static void main(String[] args) {
// 创建集合对象
HashMap<Student, String> hm = new HashMap<Student, String>();

// 创建学生对象
Student s1 = new Student("貂蝉", 27);
Student s2 = new Student("王昭君", 30);
Student s3 = new Student("西施", 33);
Student s4 = new Student("杨玉环", 35);
Student s5 = new Student("貂蝉", 27);

// 添加元素
hm.put(s1, "8888");
hm.put(s2, "6666");
hm.put(s3, "5555");
hm.put(s4, "7777");
hm.put(s5, "9999");

// 遍历
Set<Student> set = hm.keySet();
for (Student key : set) {
String value = hm.get(key);
System.out.println(key.getName() + "---" + key.getAge() + "---"+ value);
}
}
}

package cn.itcast_02;

public class Student {
private String name;
private int age;

public Student() {
super();
}

public Student(String name, int age) {
super();
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() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}

//因为键相同,值覆盖,键的底层依赖hashcode和equals,注意要重写equals
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐