Map集合框架

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;
	}
	
	
}


在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值