一、集合框架Map介绍
Map集合的特性:
1、无序的;
2、以键值对的方式进行数据存储;
3、键相同,值被覆盖;(返回上一个映射关系对应的值)
4、它不继承Collection接口
遍历方法:
第一种:分别用Set和Collection去获取键和值
代码如下:
package com.leiyuanlin.map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 初识map
* put
* 1.往集合容器中添加键值对映射关系
* 2.当集合中存在该键的映射关系,后来的映射关系会覆盖前面的映射关系
* 3.会返回上一个映射关系对应的值
*
* keySet
* entrySet
*
* @author 2018101801
*
*/
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map=new HashMap<>();
map.put("zs", "22");
map.put("ls", "18");
map.put("ww", "24");
map.put("xl", "32");
String put = map.put("zs", "32");
/*遍历方式:
第一种:
获取所有的Key值*/
System.out.println("键:");
Set<String> set=map.keySet();
for (String str : set) {
System.out.println(str);
}
System.out.println("------------");
//获取所有的value值
System.out.println("值:");
Collection<String> coll=map.values();
for (String str : coll) {
System.out.println(str);
}
}
}
结果:
第二种:entrySet获取所有键值对
代码演示:
package com.leiyuanlin.map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* 初识map
* put
* 1.往集合容器中添加键值对映射关系
* 2.当集合中存在该键的映射关系,后来的映射关系会覆盖前面的映射关系
* 3.会返回上一个映射关系对应的值
*
* keySet
* entrySet
*
* @author 2018101801
*
*/
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map=new HashMap<>();
map.put("zs", "11");
map.put("zs", "22");
map.put("ls", "18");
map.put("ww", "24");
map.put("xl", "32");
//第二种:entrySet获取所有键值对
Set<Entry<String, String>> entry=map.entrySet();
for (Entry<String, String> entry2 : entry) {
System.out.println("键:"+ entry2.getKey() + ",值:" +entry2.getValue());
}
}
}
结果:
其他方法:
1、根据键获取值(以键取值)
代码:
结果:
2、remove方法(根据key删除)
代码:
结果:
3、清空所有数据
代码:
结果:
其他:
结果:
Map还有许多其他的方法,可以到在线文档里面自己去看,网址如下:
http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
小结:
Hashtable:底层是哈希表数据结构,不可以存入null键null值,该集合石线程同步的,jdk1.0,效率低
HashMap:底层是哈希表数据结构,允许使用null值和null键,该集合是不同步的。将Hashtable替代;jdk1.2,效率高
TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键进行排序
注意:
添加元素时,如果键已经在集合中存在,那么后添加的值会覆盖原来的值,并且put方法会将原有的值返回
集合框架Map应用
应用一:
1、将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
2、最后按年龄进行排序
3、需求改变、按姓名进行排序
package com.leiyuanlin.map;
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
/**
* 应用一:
1、将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
2、最后按年龄进行排序
3、需求改变、按姓名进行排序
分析:
1.1 封装出一个学生的实体类
1.2 将不同的学生作为key,不同地址作为value,存放到Map集合中
1.3 需要将重复的key剔除
* @author 2018101801
*
*/
public class TreeMapDemo {
public static void main(String[] args) {
// TreeMap<Student, String> hm=new TreeMap<>();
//如果使用的是比较器比较,那么自然条件接口则作废(功能不会叠加)
TreeMap<Student, String> hm=new TreeMap<>(new StuCom());
hm.put(new Student("zs", 18), "beijing");
hm.put(new Student("ls", 20), "shanghai");
hm.put(new Student("ww", 12), "guangzhou");
hm.put(new Student("xl", 23), "shenzhen");
hm.put(new Student("zs", 20), "hangzhou");
Set<Entry<Student, String>> entrySet = hm.entrySet();
for (Entry<Student, String> entry : entrySet) {
System.out.println("键:"+entry.getKey()+",值:"+entry.getValue());
}
}
}
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 String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
return this.getName().hashCode()+this.age;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Student) {
Student stu = (Student)obj;
return this.getName().equals(stu.getName()) && this.getAge()==stu.getAge();
}
return false;
}
@Override
public int compareTo(Student o) {
int num = this.getAge() - o.getAge();
if(num == 0) {
return this.getName().compareTo(o.getName());
}
return num;
}
}
class StuCom implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
int num = o1.getName().compareTo(o2.getName());
if(num == 0) {
return o1.getAge()-o2.getAge();
}
return num;
}
}
结果:
应用二:
统计字符串中字符出现次数,按次数排序
代码:
package com.leiyuanlin.map;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
/**
* 应用二:
统计字符串中字符出现次数
按次数排序
adsfsdeyrthytsagertbc
a(3) b(2) c(5)
1、统计字符出现的次数
2、从a到z进行统计
分析:
1、字符是唯一的,可以将其作为map集合的key,次数就是map集合的值value
2、将指定的字符串装到一个容器中进行筛选,将字符串转换成字符数组
3、当字符第一次出现的时候,意味着在map集合中找不到对应的value值,将它赋值为1
当字符第二次出现的时候,意味着map集合中存在对应的值,给它对应的值加一
* @author 2018101801
*
*/
public class HashMapDemo {
public static void main(String[] args) {
String s="afwetrevdertzconmtyhoytuj";
getRepeatTimes(s);
}
/**
* 统计次数的方法
* @param s
*/
private static void getRepeatTimes(String s) {
char[] charArray = s.toCharArray();
Map<Character, Integer> map=new TreeMap<>();
for (char c : charArray) {
Integer val = (Integer)map.get(c);
if(val == null) {
map.put(c, 1);
}else {
map.put(c, ++val);
}
}
StringBuffer sb = new StringBuffer();
Set<Entry<Character, Integer>> entrySet = map.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
sb.append(entry.getKey()+"("+entry.getValue()+")");
}
System.out.println(sb.toString());
}
}
结果:
集合框架中的工具类(Collections、Arrays)
1、Collections.reverseOrder 倒序
代码:
package com.leiyuanlin.map;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
//Collections.reverseOrder 倒序
TreeMap<Student, String> hm=new TreeMap<>(Collections.reverseOrder(new StuCom()));
hm.put(new Student("zs", 18), "beijing");
hm.put(new Student("ls", 20), "shanghai");
hm.put(new Student("ww", 12), "guangzhou");
hm.put(new Student("xl", 23), "shenzhen");
hm.put(new Student("zs", 20), "hangzhou");
/*
没有倒序之前的结果
键:Student [name=ls, age=20],值:shanghai
键:Student [name=ww, age=12],值:guangzhou
键:Student [name=xl, age=23],值:shenzhen
键:Student [name=zs, age=18],值:beijing
键:Student [name=zs, age=20],值:hangzhou
*/
Set<Entry<Student, String>> entrySet = hm.entrySet();
for (Entry<Student, String> entry : entrySet) {
System.out.println("键:"+entry.getKey()+",值:"+entry.getValue());
}
}
}
class 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 String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
return this.getName().hashCode()+this.age;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Student) {
Student stu = (Student)obj;
return this.getName().equals(stu.getName()) && this.getAge()==stu.getAge();
}
return false;
}
}
class StuCom implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
int num = o1.getName().compareTo(o2.getName());
if(num == 0) {
return o1.getAge()-o2.getAge();
}
return num;
}
}
结果:
2、Arrays.toString() 返回指定数组内容的字符串表示形式。
代码:
结果:
关于Collections、Arrays的更多方法,上面的在线文档里都有:
http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
感谢观看!