您的位置 首页 golang

java集合框架-Map集合

1:Map(掌握)

(1)将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。

(2)Map和Collection的区别?

(3)Map接口功能概述

1、添加:

1、V put(K key, V value) (可以相同的key值,但是添加的value值会覆

盖前面的,返回值是前一个,如果没有就返回null)

2、putAll(Map<? extends K,? extends V> m) 从指定映射中将所有映射关

系复制到此映射中(可选操作)。

2、删除

1、remove() 删除关联对象,指定key对象

2、clear() 清空集合对象

3、获取

1:value get(key); 可以用于判断键是否存在的情况。当指定的键不存在的时候,返

回的是null。

3、判断:

1、boolean isEmpty() 长度为0返回true否则false

2、boolean containsKey(Object key) 判断集合中是否包含指定的key

3、boolean containsValue(Object value) 判断集合中是否包含指定的value

4、长度:

Int size()

(4)Map集合的遍历

A:键找值

a:获取所有键的集合

b:遍历键的集合,得到每一个键

c:根据键到集合中去找值

B:键值对对象找键和值

a:获取所有的键值对对象的集合

b:遍历键值对对象的集合,获取每一个键值对对象

c:根据键值对对象去获取键和值

代码体现:

Map<String,String> hm = new HashMap<String,String>();

hm.put(“it002″,”hello”);

hm.put(“it003″,”world”);

hm.put(“it001″,”java”);

//方式1 键找值

Set<String> set = hm.keySet();

for(String key : set) {

String value = hm.get(key);

System.out.println(key+”—“+value);

}

//方式2 键值对对象找键和值

Set<Map.Entry<String,String>> set2 = hm.entrySet();

for(Map.Entry<String,String> me : set2) {

String key = me.getKey();

String value = me.getValue();

System.out.println(key+”—“+value);

}

(5)HashMap集合的练习

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;

}

@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;

}

}

  • A:HashMap<String,String>

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”, “比尔盖茨”);

// 遍历

Set<String> set = hm.keySet();

for (String key : set) {

String value = hm.get(key);

System.out.println(key + “—” + value);

}

}

}

  • B:HashMap<Integer,String>

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>

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>

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);

}

}

}

(6)TreeMap集合的练习

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;

}

}

  • A:TreeMap<String,String>

import java.util.Set;

import java.util.TreeMap;

/*

* TreeMap:是基于红黑树的Map接口的实现。

*

* HashMap<String,String>

* 键:String

* 值:String

*/

public class TreeMapDemo {

public static void main(String[] args) {

// 创建集合对象

TreeMap<String, String> tm = new TreeMap<String, String>();

// 创建元素并添加元素

tm.put(“hello”, “你好”);

tm.put(“world”, “世界”);

tm.put(“java”, “爪哇”);

tm.put(“world”, “世界2”);

tm.put(“javaee”, “爪哇EE”);

// 遍历集合

Set<String> set = tm.keySet();

for (String key : set) {

String value = tm.get(key);

System.out.println(key + “—” + value);

}

}

}

  • B:TreeMap<Student,String>

import java.util.Comparator;

import java.util.Set;

import java.util.TreeMap;

/*

* TreeMap<Student,String>

* 键:Student

* 值:String

*/

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 num2 = num == 0 ? s1.getName().compareTo(

s2.getName()) : num;

return num2;

}

});

// 创建学生对象

Student s1 = new Student(“潘安”, 30);

Student s2 = new Student(“柳下惠”, 35);

Student s3 = new Student(“唐伯虎”, 33);

Student s4 = new Student(“燕青”, 32);

Student s5 = new Student(“唐伯虎”, 33);

// 存储元素

tm.put(s1, “宋朝”);

tm.put(s2, “元朝”);

tm.put(s3, “明朝”);

tm.put(s4, “清朝”);

tm.put(s5, “汉朝”);

// 遍历

Set<Student> set = tm.keySet();

for (Student key : set) {

String value = tm.get(key);

System.out.println(key.getName() + “—” + key.getAge() + “—”

+ value);

}

}

}

2:Collections(理解)

(1)是针对集合进行操作的工具类

(2)面试题:Collection和Collections的区别

A:Collection 是单列集合的顶层接口,有两个子接口List和Set

B:Collections 是针对集合进行操作的工具类,可以对集合进行排序和查找等

(3)常见的几个小方法:

A:public static <T> void sort(List<T> list)

B:public static <T> int binarySearch(List<?> list,T key)

C:public static <T> T max(Collection<?> coll)

D:public static void reverse(List<?> list)

E:public static void shuffle(List<?> list)

文章来源:智云一二三科技

文章标题:java集合框架-Map集合

文章地址:https://www.zhihuclub.com/87199.shtml

关于作者: 智云科技

热门文章

网站地图