Map集合框架

本文深入探讨Java中Map集合的特性与应用,包括HashMap、Hashtable、TreeMap的区别及使用场景,通过实例演示如何利用Map解决实际问题,如学生信息存储、字符串字符统计等,并介绍Collections工具类的高级用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、集合框架Map介绍

Hashtable:底层是哈希表数据结构,不可以存入null键null值,该集合石线程同步的,jdk1.0,效率低
HashMap:底层是哈希表数据结构,允许使用null值和null键,该集合是不同步的。将Hashtable替代;jdk1.2,效率高
TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键进行排序
(面试题):
Map、list、set 这三个接口,哪个不继承与Collection接口? Map

  • map集合中存放的都是一组组映射关系,每一个键只映射到一个值
    map特有的方法:
    a. put
    如果遇到多表联查,就用map
    1.特点:
    往集合容器中添加键值对应关系,当集合中存在该键的映射关系,后来的映射关系会覆盖的前面的映射关系
package com.qukang.map;

import java.util.HashMap;
import java.util.Map;

public class MapDemo {

	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("mz","11");
		map.put("zs","23");
		System.out.println(map);
	}
}

输出结果:
在这里插入图片描述
当你输出当前添加的键值对应关系的返回值的时候,如果键值对应关系中已存在,就会返回已存在的映射关系的值

package com.qukang.map;

import java.util.HashMap;
import java.util.Map;

public class MapDemo {

	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("mz","11");
		String put = map.put("zs","23");
		System.out.println(put);
		System.out.println(map);
	}
}

输出结果:
在这里插入图片描述

b. KeySet
KeySet是Map集合的第一种特有遍历方式
1.特点:
KeySet是根据键调用 get(key) 来拿相对隐射关系的值

package com.qukang.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo {

	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("mz","11");

		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			System.out.println(key +" " +map.get(key));
		}
	}
}

结果:
在这里插入图片描述

c. entrySet
entrySet是Map集合的第二种特有遍历方式
1.特点:
entrySet是把集合里的映射关系一组一组的拿出来

	package com.qukang.map;
	import java.util.HashMap;
	import java.util.Map;
	import java.util.Map.Entry;
	import java.util.Set;

	public class MapDemo {

	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("mz","11");
		
		Set<Entry<String, String>> entrySet = map.entrySet();
		for (Entry<String, String> entry : entrySet) {
			System.out.println(entry.getKey() +" "+ entry.getValue());
		}
	}
	}

结果:
在这里插入图片描述

2、TreeSet 二叉树数据结构

package com.qukang.map;

import java.util.Iterator;
import java.util.TreeSet;

public class TreexsMapDemo  {
	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);
		ts.add(24);
		ts.add(27);
		ts.add(36);
		ts.add(32);
		ts.add(18);
		Iterator<Integer> it = ts.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}
}

结果:
在这里插入图片描述
二叉树的数据排序结构:
在这里插入图片描述

3、集合框架Map的应用

应用一:

1、将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
注意:
需要重写HashCode和equals方法才能够打到去重效果
当你输出当前添加的键值对应关系的返回值的时候,如果键值对应关系中已存在,就会返回已存在的映射关系的值

package com.qukang.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) {
		HashMap<Student,String> hm=new HashMap<>();
		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("zx",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 {
	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;
	}
}

结果:
在这里插入图片描述

2、最后按年龄进行排序
注意:排序需要用TreeMap集合 还要实现Comparable接口和重写CompareTo反法

package com.qukang.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<>();
		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;
	}
}


结果:
在这里插入图片描述
3、需求改变、按姓名进行排序
注意:排序需要用TreeMap集合,需求改变 还要实现Comparator接口和重写compare反法

package com.qukang.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;
	}
	
}

结果:
在这里插入图片描述
应用二:

1.统计字符串中字符出现次数
2.按次数排序

  • 分析:
  • 1.字符是唯一的,可以将作为map集合的键Key,次数就是map集合的值Value.
  • 2.将指定的字符串装到一个容器中进行筛选,将字符串转成一个字符串组
  • 3.当字符第一次出现的时候,意味着在map集合中找不到对应的value值,给它赋值为1,
  • 当字符第二次出现的时候,意味着map集合中存在对应的值,就给它对应的值加一;
package com.qukang.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapDemo {

	
	public static void main(String[] args) {
		String str="wqongqiovnqubczmqevsaavawqrqgqverhejtyiyynt";
		getcharString(str);
	}

	private static void getcharString(String str) {
		// TODO Auto-generated method stub
		char [] charArray=str.toCharArray();
		Map<Character,Integer> mp=new HashMap<>();
		for (char c : charArray) {
			Integer num=(Integer)mp.get(c); 
			if(mp.get(c)==null) {
				mp.put(c,1);
			}else {
				mp.put(c,++num);
			}
		}
		StringBuffer sb=new StringBuffer();
		Set<Entry<Character, Integer>> entrySet = mp.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.qukang.map; 

import java.util.Collections;
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;
	}
	
}

结果:
在这里插入图片描述
2.反转倒序:

package com.qukang.map;

import java.util.Collections;
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<>(Collections.reverseOrder(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;
	}
	
}

结果:
在这里插入图片描述
Arrays.toString() 返回指定数组内容的字符串表示形式。

package com.qukang.map;

import java.util.Arrays;

public class ArraysDemo {
	public static void main(String[] args) {
		String [] str=new String[] {"QWE,RTY,UIO"};
		System.out.println(Arrays.toString(str));
	}
}

结果:
在这里插入图片描述

This book explores the concept of a map as a fundamental data type. It defines maps at three levels. The first is an abstract level, in which mathematic concepts are leveraged to precisely explain maps and operational semantics. The second is at a discrete level, in which graph theory is used to create a data model with the goal of implementation in computer systems. Finally, maps are examined at an implementation level, in which the authors discuss the implementation of a fundamental map data type in database systems. The map data type presented in this book creates new mechanisms for the storage, analysis, and computation of map data objects in any field that represents data in a map form. The authors develop a model that includes a map data type capable of representing thematic and geometric attributes in a single data object. The book provides a complete example of mathematically defining a data type, ensuring closure properties of those operations, and then translating that type into a state that is suited for implementation in a particular context. The book is designed for researchers and professionals working in geography or computer science in a range of fields including navigation, reasoning, robotics, geospatial analysis, data management, and information retrieval. Table of Contents Chapter 1 Concepts of Maps Chapter 2 A Formal Model of Maps as a Fundamental Type Chapter 3 PLR Partitions: Extending Maps to Include Point and Line Features Chapter 4 Foundational Operations for Maps Chapter 5 Constructing Map Operations Using the Fundamental Map Operations Chapter 6 Extended Operations Over Maps Chapter 7 Topological Relationships Between Maps Chapter 8 A Discrete Model of Maps Chapter 9 Implementing Maps: Map2D
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值