Map集合map<k,v>(双向)
Map集合概述和特点
1.蒋健作为一个集合
2.将值作为一个集合
3.将键值对作为一个集合
Map和collection不同
Map是双列集合,collection是单列集合
map的键唯一,collection对子体系set是唯一的
Map集合的数据结构针对键有效,跟值无关,collection集合的数据是针对元素有效。
Map集合的功能概述
A:添加功能
V put(K key,V value);添加元素,如果键是第一次,就直接存储元素,返回NULL。如果不是第一次存储,就用值把以前的替换掉,返回以前的值。
B:删除功能
void clear();移除所有键值对元素
V remove (Object key);根据键删除值,并返回值
C判断功能
boolean containsKey(Object key);判断哪是否包含键
boolean containsValue(Object value);判断是否包含指定的值
boolean isEmpty();判断集合是否为空
D:获取功能
set<map.Entry<k,v>> entrySet();
V get(Object key);根据键获取值
Set<k> keySet();获取集合中所有的键的集合
collection<v> values();获取集合在所有值的集合
E:长度功能
int size();返回集合中的键值对的个数。
练习
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("张三", 11);
map.put("张四", 23);
map.put("张6", 11);
// System.out.println(map.containsKey("张三"));
// System.out.println(map.containsKey("张6"));
System.out.println(map.containsValue(11));
System.out.println(map.containsValue(22));
System.out.println(map);
}
1.存取不一致
2.可以用值的类型接收Map.pu();
3.用同一键输入不同的值时,会返回上一个值
4.删除,通过key删除一对,并返回值,通过key和value一起删除不返回值
5.判断是否包含,相同的值在第一个值判断完成后不会往下判断
map集合遍历之键找值
思路:
1.获取集合中所有键的集合
2.遍历键的集合,获取每一个键
3.根据键获取值
Map迭代通过keySet()和get()方法进行(没有迭代器)
//增强for循环遍历map的底层
public static void main(String[] args) {
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("张三", 23);
map.put("张四", 23);
//Integer i=map.get("张三");
// System.out.println(i);
//1.获取map中的所有键,形成一个集合
Set<String> keyset=map.keySet();//将所有的key从map中提前出来
//2.获取迭代器
Iterator<String> it=keyset.iterator();
//3.判断是否为空
while(it.hasNext()) {
//4.获取每一个键
String key=it.next();
//5.获取每一个值
Integer value=map.get(key);
System.out.println(key+"---"+value);
}
Map集合的遍历之键值对对象找键和值
1.获取所有键值对对象的集合
2.遍历键值对对象的集合,获取每一个键值对
3.根据键值对对象获取键和值
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("张三", 23);
map.put("张四", 23);
//1.获取所有的键值对对象的集合
Set<Map.Entry<String, Integer>> entryset=map.entrySet();
//2.获取迭代器
Iterator<Map.Entry<String, Integer>> itr=entryset.iterator();
//3判断是否有元素
while(itr.hasNext()) {
//4.获取每一个对象
Map.Entry<String, Integer> en=itr.next();
//5。根据键值获取键
String key=en.getKey();
//6.获取值
Integer value=en.getValue();
System.out.println(key+" "+value);
}
for (Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
Map集合键Student值String的案例
package com.map.cn;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("李四",24), "上海");
hm.put(new Student("张三",23), "北京");
hm.put(new Student("王五",25), "广东");
hm.put(new Student("张三",23), "深圳");
System.out.println(hm);
}
}
public class Student implements Comparable<Student>{
private String name;
private int age;
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 10;
}
@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;
return age == other.age && Objects.equals(name, other.name);
}
public int compareTo(Student o) {
// TODO Auto-generated method stub
int num=this.age-o.age;
return num==0? this.name.compareTo(o.name):num;
}
}
集合框架LinkedHashMap
特点:因为是链表形式,所以可以保证存取一致性
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedHashMap<Student,String> hm=new LinkedHashMap<Student,String>();
hm.put(new Student("李四",24), "上海");
hm.put(new Student("张三",23), "北京");
hm.put(new Student("王五",25), "广东");
hm.put(new Student("张三",23), "深圳");
System.out.println(hm);
}
Treemap
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeMap<Student,String> hm=new TreeMap<Student,String>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int num=o1.getName().compareTo(o2.getName());
return num==0? o1.getAge()-o2.getAge():num;
}
});
hm.put(new Student("李四",24), "上海");
hm.put(new Student("张三",23), "北京");
hm.put(new Student("王五",25), "广东");
hm.put(new Student("张三",23), "深圳");
System.out.println(hm);
}
用map集合去除aaaaabbcccccdd中重复的元素
public class Demo4{
public static void main(String[] args) {
String s="aaaaabbcccccd";
char[] c=s.toCharArray();
Map<Character,Integer> m=new HashMap<Character,Integer>();
for (char d : c) {
// if(!m.containsKey(d)) {
// m.put(d, 1);
// }else {
// m.put(d, m.get(d)+1);
// }
Integer i=!m.containsKey(d)? m.put(d, 1):m.put(d, m.get(d)+1);
}
for (Character key : m.keySet()) {
System.out.println(key+" "+m.get(key));
}
}
}