package com.qinwenpeng.map;
import java.util.HashMap;
import java.util.Map;
/**
* 初始map
* put
* keySet
* 多表联查用map list<map>
*
*
*
*
*/
public class MaoDemo {
public static void main(String[] args) {
Map<String,String> map=new HashMap<>();
map.put("zs","12");
map.put("ls","22");
map.put("ww","32");
map.put("mazi","11");
// map.put("zs","32");
System.out.println(map);
}
}
package com.qinwenpeng.map;
import java.util.HashMap;
import java.util.Map;
/**
* 初始map
* put
* keySet
* 多表联查用map list<map>
*
*
*
*
*/
如果有相同的,,他會覆蓋。
put
1.往集合容器添加键值对映射关系
2。当集合中存在该键的映射关系,后来的映射关系会覆盖前面的映射关系;
public class MaoDemo {
public static void main(String[] args) {
Map<String,String> map=new HashMap<>();
map.put("zs","12");
map.put("ls","22");
map.put("ww","32");
map.put("mazi","11");
map.put("zs","32");
System.out.println(map);
}
}
package com.qinwenpeng.map;
import java.util.HashMap;
import java.util.Map;
/**
* 初始map
* put
* 1.往集合容器添加键值对映射关系
* 2。当集合中存在该键的映射关系,后来的映射关系会覆盖前面的映射关系;
* 3.会返回上一个映射关系对应的值
* keySet
* 多表联查用map list<map>
*
*
*
*
*/
public class MaoDemo {
public static void main(String[] args) {
Map<String,String> map=new HashMap<>();
map.put("zs","12");
map.put("ls","22");
map.put("ww","32");
map.put("mazi","11");
String put=map.put("zs","32");//1 put=2
System.out.println(put);
System.out.println(map);
}
}
画图思路
1.根据Key值去取Value值
Set<String> keySet=map.keySet();
for (String key : keySet) {
System.out.println(map+":"+map.get(key));
}
2.将所有关系先取出再一个一个找相对映射关系
Set<Entry<String,String>> entrySet=map.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+ ":" + entry.getValue());
}
自动排序
package com.qinwenpeng.map;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeMapDemo {
public static void main(String[] args) {
TreeSet<Integer> ts=new TreeSet<>();
ts.add(22);
ts.add(25);
ts.add(28);
ts.add(33);
ts.add(21);
Iterator<Integer> it=ts.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
地址不一样,所以不会覆盖。
package com.qinwenpeng.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeSet;
/**
*
* 应用一:
* 1.将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
* 2.最后按年龄进行排序
* 3,需求改变,按名字进行排序
* 分析:
* 1.1 封装出一个学生的实体类
* 1。2 将不同的学生作为ket 不同地址作为value,存放到Map集合
* 1.3 需要将重复的ket给剔除掉
*
* @author Administrator
*
*/
public class TreeMapDemo {
public static void main(String[] args) {
HashMap<Student, String> hm=new HashMap<>();
hm.put(new Student("zs",12),"beijing");
hm.put(new Student("ls",22),"shanghai");
hm.put(new Student("ww",21),"guangzhou");
hm.put(new Student("mazi",6),"shenzheng");
hm.put(new Student("zs",12),"hangzhou");
System.out.println(hm.size());
}
}
class Student{
public String name;
public 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) {
super();
this.name = name;
this.age = age;
}
public Student(){
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
1.将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
package com.qinwenpeng.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeSet;
/**
*
- 应用一:
- 1.将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
- 2.最后按年龄进行排序
- 3,需求改变,按名字进行排序
- 分析:
- 1.1 封装出一个学生的实体类
- 1。2 将不同的学生作为ket 不同地址作为value,存放到Map集合
- 1.3 需要将重复的ket给剔除掉
- @author Administrator
*/
public class TreeMapDemo {
public static void main(String[] args) {
HashMap<Student, String> hm=new HashMap<>();
hm.put(new Student("zs",12),"beijing");
hm.put(new Student("ls",22),"shanghai");
hm.put(new Student("ww",21),"guangzhou");
hm.put(new Student("mazi",6),"shenzheng");
hm.put(new Student("zs",12),"hangzhou");
System.out.println(hm.size());
}
}
class Student{
public String name;
public 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) {
super();
this.name = name;
this.age = age;
}
public Student(){
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@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;
}
```
2、最后按年龄进行排序
注意:排序需要用TreeMap集合 还要实现Comparable接口和重写CompareTo反法
package com.qinwenpeng.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeSet;
/**
*
- 应用一:
- 1.将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
- 2.最后按年龄进行排序
- 3,需求改变,按名字进行排序
- 分析:
- 1.1 封装出一个学生的实体类
- 1。2 将不同的学生作为ket 不同地址作为value,存放到Map集合
- 1.3 需要将重复的ket给剔除掉
- @author Administrator
*/
public class TreeMapDemo {
public static void main(String[] args) {
HashMap<Student, String> hm=new HashMap<>();
hm.put(new Student("zs",12),"beijing");
hm.put(new Student("ls",22),"shanghai");
hm.put(new Student("ww",21),"guangzhou");
hm.put(new Student("mazi",6),"shenzheng");
hm.put(new Student("zs",12),"hangzhou");
System.out.println(hm.size());
}
}
class Student{
public String name;
public 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) {
super();
this.name = name;
this.age = age;
}
public Student(){
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode()+this.age;
}
@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;
}
}
3、需求改变、按姓名进行排序
注意:排序需要用TreeMap集合,需求改变 还要实现Comparator接口和重写compare反法
package com.qinwenpeng.map;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Student,String> hm=new TreeMap<>(new StudentComp());
hm.put(new Student("zx",18), "shenzhen");
hm.put(new Student("ls",21), "guangzhou");
hm.put(new Student("ww",25), "beijing");
hm.put(new Student("mz",21), "shanghai");
hm.put(new Student("zxx",18), "hangzhou");
Set<Entry<Student, String>> entrySet = hm.entrySet();
for (Entry<Student, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+ entry.getValue());
}
System.out.println("长度为:"+hm.size());
}
}
class Student implements Comparable<Student>{
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;
}
public Student() {}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode() + this.age;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(obj instanceof Student) {
Student s=(Student)obj;
return this.getName().equals(s.getName()) && this.getAge()==s.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 StudentComp implements Comparator<Student>{
@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;
}
}
自然接口作废,以比较接口为主
package com.qinwenpeng.map;
import java.util.Comparator;
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;
/**
*
* 应用一:
* 1.将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
* 2.最后按年龄进行排序
* 3,需求改变,按名字进行排序
*
*
* 分析:
* 1.1 封装出一个学生的实体类
* 1。2 将不同的学生作为ket 不同地址作为value,存放到Map集合
* 1.3 需要将重复的ket给剔除掉
*
* @author Administrator
*
*/
public class TreeMapDemo {
public static void main(String[] args) {
// HashMap<Student, String> hm=new HashMap<>();
// TreeMap<Student, String> hm=new TreeMap<>();
TreeMap<Student, String> hm=new TreeMap<>(new StuComp());
hm.put(new Student("zs",12),"beijing");
hm.put(new Student("ls",22),"shanghai");
hm.put(new Student("ww",21),"guangzhou");
hm.put(new Student("mazi",6),"shenzheng");
hm.put(new Student("zss",12),"hangzhou");
hm.put(new Student("zss",13),"hangzhou");
// System.out.println(hm.size());
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>{
public String name;
public 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) {
super();
this.name = name;
this.age = age;
}
public Student(){
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode()+this.age;
}
@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<Student>{
@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;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190609214152870.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FpbndlbnBlbmdsaXl1YW4=,size_16,color_FFFFFF,t_70)
package com.qinwenpeng.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
- 应用二
- 统计字符串出现的次数
- 按次数排序
- sfasjfoiafafafasddal
- a(3)b(3)c(9)…
- 1.统计字符出现的次数?
- 2.从a到z进行统计
- 分析:
- 1.字符是唯一的,可以将其作为map集合的key,次数就是map集合的值value:
- 2.将指定的字符串装到一个集合的筛选,将字符串转成一个字符数组;
- 3.当字符第一次出现的时候。意味这map集合中找不到对应的value值,给他赋值为一
- 当字符第二次出现的时候,意味着map集合存在对应的值,给他对应的值加一
*/
public class HashSetDemo2 {
public static void main(String[] args) {
String s=“sfasjfoiafafafasddal”;
//{‘s’,‘f’,‘j’,…}
getRepeatTimes(s);
}
private static void getRepeatTimes(String s) {
// TODO Auto-generated method stub
char[] charArray=s.toCharArray();
Map<Character, Integer> map=new HashMap<>();
for (char c : charArray) {
Integer val=(Integer)map.get(c);
if(val==null) {
map.put(c,1);
}else {
map.put(c, ++val);
//i++ 先赋值后运算,++i 先运算后赋值
}
}
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());
}
}
4、集合框架工具类(Collections、Arrays)
Collections.reverseOrder() 反转
1.正常排序:
package com.qinwenpeng.map;
import java.util.Collections;
import java.util.Comparator;
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;
/**
*
* 应用一:
* 1.将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
* 2.最后按年龄进行排序
* 3,需求改变,按名字进行排序
*
*
* 分析:
* 1.1 封装出一个学生的实体类
* 1。2 将不同的学生作为ket 不同地址作为value,存放到Map集合
* 1.3 需要将重复的ket给剔除掉
*
* @author Administrator
*
*/
public class TreeMapDemo {
public static void main(String[] args) {
// String sss=new String[] {"zs","nv","35"};
// HashMap<Student, String> hm=new HashMap<>();
// TreeMap<Student, String> hm=new TreeMap<>();
TreeMap<Student, String> hm=new TreeMap<>(new StuComp());
// TreeMap<Student, String> hm=new TreeMap<>(Collections.reverseOrder(new StuComp()));
hm.put(new Student("zs",12),"beijing");
hm.put(new Student("ls",22),"shanghai");
hm.put(new Student("ww",21),"guangzhou");
hm.put(new Student("mazi",6),"shenzheng");
hm.put(new Student("zss",12),"hangzhou");
hm.put(new Student("zss",13),"hangzhou");
// System.out.println(hm.size());
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>{
public String name;
public 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) {
super();
this.name = name;
this.age = age;
}
public Student(){
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode()+this.age;
}
@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<Student>{
@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;
}
}
2.反转倒序:
package com.qinwenpeng.map;
import java.util.Collections;
import java.util.Comparator;
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;
/**
*
* 应用一:
* 1.将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
* 2.最后按年龄进行排序
* 3,需求改变,按名字进行排序
*
*
* 分析:
* 1.1 封装出一个学生的实体类
* 1。2 将不同的学生作为ket 不同地址作为value,存放到Map集合
* 1.3 需要将重复的ket给剔除掉
*
* @author Administrator
*
*/
public class TreeMapDemo {
public static void main(String[] args) {
// String sss=new String[] {"zs","nv","35"};
// HashMap<Student, String> hm=new HashMap<>();
// TreeMap<Student, String> hm=new TreeMap<>();
// TreeMap<Student, String> hm=new TreeMap<>(new StuComp());
TreeMap<Student, String> hm=new TreeMap<>(Collections.reverseOrder(new StuComp()));
hm.put(new Student("zs",12),"beijing");
hm.put(new Student("ls",22),"shanghai");
hm.put(new Student("ww",21),"guangzhou");
hm.put(new Student("mazi",6),"shenzheng");
hm.put(new Student("zss",12),"hangzhou");
hm.put(new Student("zss",13),"hangzhou");
// System.out.println(hm.size());
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>{
public String name;
public 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) {
super();
this.name = name;
this.age = age;
}
public Student(){
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode()+this.age;
}
@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<Student>{
@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;
}
}