一、HashMap
常用函数:
添加功能
V put(K key,V value):添加元素。这个其实还有另一个功能
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
删除功能
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回
判断功能
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
获取功能
Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
V get(Object key):根据键获取值
Set keySet():获取集合中所有键的集合
Collection values():获取集合中所有值的集合
长度功能
int size():返回集合中的键值对的对数
Map的初始化:
HashMap<K,V> mp=new HashMap<>();//hashmap(底层由hash表实现)
TreeMap<K,V> mp=new TreeMap<>();//treemap(底层由红黑树实现,键有序)
Map的遍历(利用keyset遍历):
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
class stu1{
int age;
String name;
public stu1(int ii,String na){
this.age=ii;
this.name=na;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
HashMap<Integer, stu1> mp=new HashMap<>();
mp.put(1, new stu1(12, "张三"));
mp.put(2, new stu1(14, "李四"));
mp.put(3, new stu1(15, "王五"));
Set<Integer> set=mp.keySet();
Iterator<Integer> iterator=set.iterator();
while(iterator.hasNext()) {
int idd=iterator.next();
stu1 ss=mp.get(idd);
System.out.println(idd+" "+ss.getAge()+" "+ss.getName());
}
}
}
Map的遍历(entrySet遍历):
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
class stu1{
int age;
String name;
public stu1(int ii,String na){
this.age=ii;
this.name=na;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
HashMap<Integer, stu1> mp=new HashMap<>();
mp.put(1, new stu1(12, "张三"));
mp.put(2, new stu1(14, "李四"));
mp.put(3, new stu1(15, "王五"));
Set<Map.Entry<Integer, stu1> > entries=mp.entrySet();
for(Map.Entry<Integer, stu1> ent : entries) {
System.out.println(ent.getKey()+" "+ent.getValue().getAge()+" "+ent.getValue().getName());
}
}
}
二、TreeMap
根据Key由小到大排序;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
class stu1{
int age;
String name;
public stu1(int ii,String na){
this.age=ii;
this.name=na;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
TreeMap<Integer, stu1> mp=new TreeMap<>();
mp.put(1, new stu1(12, "张三"));
mp.put(4, new stu1(10, "李四"));
mp.put(3, new stu1(15, "王五"));
Set<Map.Entry<Integer, stu1> > entries=mp.entrySet();
for(Map.Entry<Integer, stu1> ent : entries) {
System.out.println(ent.getKey()+" "+ent.getValue().getAge()+" "+ent.getValue().getName());
}
}
}
如果自定义类为Key,自定义类需要实现Comparable接口(其中的compareTo方法):
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
class stu1 implements Comparable<stu1>{
int age;
String name;
public stu1(int ii,String na){
this.age=ii;
this.name=na;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int compareTo(stu1 s) {//根据年龄降序排序
return s.age-this.age;
}
}
public class Main {
public static void main(String[] args) {
TreeMap<stu1, Integer> mp=new TreeMap<>();
mp.put( new stu1(12, "张三"),1);
mp.put (new stu1(1, "李四"),2);
mp.put( new stu1(15, "王五"),3);
Set<Map.Entry< stu1,Integer> > entries=mp.entrySet();
for(Map.Entry<stu1, Integer> ent : entries) {
System.out.println("age: "+ent.getKey().age+" name: "+ent.getKey().name+" id: "+ent.getValue());
}
}
}
注意:
如果根据年龄降序,this.age在后面;
public int compareTo(stu1 s) {
return s.age-this.age;
}
如果根据年龄升序,this.age在前面;
public int compareTo(stu1 s) {//根据年龄降序排序
return this.age-s.age;
}