1:put增加
代码如下
Map map=new HashMap<>();
map.put("zs", "12");
map.put("ls", "32");
map.put("ww", "14");
map.put("mz", "35");
//当集合中存在映射关系,后来的映射关系会覆盖前面的映射关系
map.put("zs", "32");
输出结果:
{ww=14, ls=32, zs=32, mz=35}
2:keyset
(1)拿到所有Key值,所以get(key)拿对应的value值
Map map=new HashMap<>();
map.put("zs", "12");
map.put("ls", "32");
map.put("ww", "14");
map.put("mz", "35");
map.put("zs", "32");
Set ketSet=map.keySet();
for (String key : ketSet) {
System.out.println(key+":"+map.get(key));
}
运行结果如下:
ww:14
ls:32
zs:32
mz:35
3:entrSet拿到所有映射关系
map.put("zs", "12");
map.put("ls", "32");
map.put("ww", "14");
map.put("mz", "35");
map.put("zs", "32");
Set> entry=map.entrySet();
for (Entry entry2 : entry) {
System.out.println(entry2.getKey()+":"+entry2.getValue());
}
运行结果如下:
ww:14
ls:32
zs:32
mz:35
(1)Treemap排序
HashMap map=new HashMap<>();
map.put(new student("zs", 12), "beij");
map.put(new student("maz", 22), "shanhai");
map.put(new student("ls", 32), "guangzhou");
map.put(new student("ww", 42), "shenzhen");
map.put(new student("zs", 12), "hanzhou");
System.out.println(map.size());
}
//输出结果为
//因为元素地址不同,还没有去重/所以输出5
(2)map集合去重
1:自然排序接口
package com.Liuyujian;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TretMap {
public static void main(String[] args) {
TreeMap hs=new TreeMap<>();
// map.put(new student("zs", 12), "beijin");
hs.put(new student("maz", 22), "shanhai");
hs.put(new student("ls", 32), "guangzhou");
hs.put(new student("ww", 42), "shenzhen");
hs.put(new student("zs", 12), "hanzhou");
// System.out.println(map.size());
Set> entrySet=hs.entrySet();
for (Entry entry2 : entrySet) {
System.out.println(entry2.getKey().getAge()+entry2.getKey().getName()+":"+entry2.getValue());
}
}
}
class student implements Comparable{
private String name;
private int 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;
}
public student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode()+this.getAge();
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
int num=this.getAge()-o.getAge();
if(num==0) {
return this.getName().compareTo(o.getName());
}
return num;
}
}
//调用hasCode,equals,conpareto,Comparable方法
//输出结果如下
//12zs:hanzhou
//22maz:shanhai
//32ls:guangzhou
//42ww:shenzhen
2比较排序接口
package com.Liuyujian;
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TretMap {
public static void main(String[] args) {
TreeMap hs=new TreeMap<>(new stuComp());
// map.put(new student("zs", 12), "beijin");
hs.put(new student("maz", 22), "shanhai");
hs.put(new student("ls", 32), "guangzhou");
hs.put(new student("ww", 42), "shenzhen");
hs.put(new student("zs", 12), "hanzhou");
// System.out.println(map.size());
Set> entrySet=hs.entrySet();
for (Entry entry2 : entrySet) {
System.out.println(entry2.getKey().getAge()+entry2.getKey().getName()+":"+entry2.getValue());
}
}
}
class student implements Comparable{
private String name;
private int 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;
}
public student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode()+this.getAge();
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
int num=this.getAge()-o.getAge();
if(num==0) {
return this.getName().compareTo(o.getName());
}
return num;
}
}
class stuComp implements Comparator{
@Override
public int compare(student o1, student o2) {
// TODO Auto-generated method stub
int num=o1.getName().compareTo(o2.getName());
if(num==0) {
return o1.getAge()-o2.getAge();
}
return num;
}
}
//应用比较排序接口,那么自然排序接口作废
//输出结果为:
//32ls:guangzhou
//22maz:shanhai
//42ww:shenzhen
//12zs:hanzhou
3运用mepj集合统计字符出现的次数
(1)将指定的字符装进一个容器进行筛选,将字符串转换成一个字符数组
(2)当字符第一次出现的时候,意味着map集合找不到对应的value值,给他赋值为1,当字符第二次出现的时候给他对应的值加1
package com.Liuyujian;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import com.sun.javafx.collections.MappingChange.Map;
public class Hreemap {
public static void main(String[] args) {
String str="sadfgfdsasdfghgfdsdfgjvcvfhgdczgfdvcx";
getRepts(str);
}
private static void getRepts(String str) {
char[] charArray=str.toCharArray();
TreeMap 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> entryset=map.entrySet();
for (Entry entry : entryset) {
sb.append(entry.getKey()+"("+entry.getValue()+")");
}
System.out.println(sb.toString());
}
}
//运行结果如下
// a(2)c(3)d(7)f(7)g(6)h(2)j(1)s(4)v(3)x(1)z(1)