IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> JAVA-day18-Map集合遍历、HashMap、TreeMap、Collections -> 正文阅读

[Java知识库]JAVA-day18-Map集合遍历、HashMap、TreeMap、Collections

一:Map(掌握)

(1)Map

将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。 

(2)Map和Collection的区别?

	A:Map 存储的是键值对形式的元素,键唯一,值可以重复。夫妻对
	B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。光棍

(3)Map接口功能概述

	A:添加功能: V put(K key,V value)
	
	B:删除功能:
				void clear():移除所有的键值对元素
				V remove(Object key):根据键删除键值对元素,并返回值
	C:判断功能:
				boolean containsKey(Object key):判断集合是否包含指定的键
				boolean containsValue(Object value):判断集合是否包含指定的值
				boolean isEmpty():判断集合是否为空
	D:获取功能:
				V get(Object key):根据键获取值
				Set<K> KeySet():获取集合中所有键的集合
				Collection<V> values():获取集合中所有值得集合	
				Set<Map.Entry<K,V>> entrySet():返回的是键值对对象的集合	
	E:长度功能:int size():返回集合中键值对的对数

(4)Map集合的遍历

方法一:键找值
	a:获取所有键的集合
	b:遍历键的集合,得到每一个键
	c:根据键到集合中找值

代码体现

public class MapDemo {
	public static void main(String[] args) {
		Map<String,String> hm = new HashMap<String,String>();

		hm.put("001","hello");
		hm.put("002","world");
		hm.put("003","java");

		// 获取所有键的集合
		Set<String> set = hm.keySet();
		// 遍历键的集合
		for(String key : set) {
			// 根据键到集合中找值
			String value = hm.get(key);
			System.out.println(key+"-----"+value);
		}
	}
}

方法二:键值对对象找键和值
	a:获取所有键值对对象的集合
	b:遍历键值对对象的集合,获取每一个键值对对象
	c:根据键到集合中找值		

代码体现

public class MapDemo {
	public static void main(String[] args) {
		Map<String,String> hm = new HashMap<String,String>();

		hm.put("001","hello");
		hm.put("002","world");
		hm.put("003","java");
		
		// 获取所有键值对对象的集合
		Set<Map.Entry<String,String>> set2 = hm.entrySet();
		// 遍历键值对对象的集合,获取每一个键值对对象
		for(Map.Entry<String,String> me : set2) {
			String key = me.getKey();
			String value = me.getValue();
			System.out.println(key+"-----"+value);
		}

(5)HashMap集合的练习

A:HashMap<String,String>

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

public class HashMapTest {
	public static void main(String[] args) {
		// 创建集合对象
		HashMap<String, String> hm = new HashMap<String,String>();
		
		// 添加元素
		hm.put("001", "西施");
		hm.put("002", "貂蝉");
		hm.put("003", "王昭君");
		hm.put("004", "杨玉环");
		hm.put("001", "后裔");
		
		// 遍历
		Set<String> set = hm.keySet();
		for(String key : set) {
			String value = hm.get(key);
			System.out.println(key+"-----"+value);
		}
	}

}

B:HashMap<Integer,String>

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

/*
 * HashMap<Integer,String>
 * 键:Integer
 * 值:String
 */
public class HashMapTest2 {
	public static void main(String[] args) {
		HashMap<Integer, String> hm = new HashMap<Integer,String>();
		
		hm.put(30, "马云");
		hm.put(50, "马化腾");
		hm.put(60, "王健林");
		hm.put(40, "周星驰");
		hm.put(20, "吴亦凡");
		
		Set<Integer> set = hm.keySet();
		for(Integer key : set) {
			String value = hm.get(key);
			System.out.println(key+"-----"+value);
		}
	}
}

C:HashMap<String,Student>

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

/*
 * HashMap<String,Student>
 * 键:String	学号
 * 值:Student 学生对象
 */
public class HashMapTest3 {
	public static void main(String[] args) {
		HashMap<String, Student> hm = new HashMap<String,Student>();
		
		Student s1 = new Student("百里守约",27);
		Student s2 = new Student("百里玄策",29);
		Student s3 = new Student("后裔",30);
		Student s4 = new Student("嫦娥",18);
		
		hm.put("T001", s1);
		hm.put("T002", s2);
		hm.put("T003", s3);
		hm.put("T004", s4);
		
		Set<String> set = hm.keySet();
		for(String key : set) {
			Student value = hm.get(key);
			System.out.println(key+"---"+value.getName()+"---"+value.getAge());
		}
		
	}
}

D:HashMap<Student,String>

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

/*
 * HashMap<Student,String>
 * 键:Student
 * 		要求:如果两个对象的成员变量值都相同,则为同一个对象。
 * 值:String
 */
public class HashMapTest4 {
	public static void main(String[] args) {
		HashMap<Student, String> hm = new HashMap<Student, String>();
		
		Student s1 = new Student("刘备",45);
		Student s2 = new Student("关羽",40);
		Student s3 = new Student("张飞",38);
		Student s4 = new Student("赵云",25);
		Student s5 = new Student("刘备",45);
		
		hm.put(s1,"6666");
		hm.put(s2,"7777");
		hm.put(s3,"8888");
		hm.put(s4,"9999");
		hm.put(s5,"5555");
		
		Set<Student> set = hm.keySet();
		for(Student key : set) {
			String value = hm.get(key);
			System.out.println(key.getName()+"---"+key.getAge()+"---"+value);
		}
		
	}
}

(6)TreeMap集合的练习

A:TreeMap<String,String>

import java.util.Set;
import java.util.TreeMap;

/*
 * TreeMap:是基于红黑树的Map接口的实现。
 * 
 * HashMap<String,String>
 * 键:String
 * 值:String
 */
public class TreeMapDemo {
	public static void main(String[] args) {
		// 创建集合对象
		TreeMap<String, String> tm = new TreeMap<String, String>();

		// 创建元素并添加元素
		tm.put("hello", "你好");
		tm.put("world", "世界");
		tm.put("java", "爪哇");
		tm.put("world", "世界2");
		tm.put("javaee", "爪哇EE");

		// 遍历集合
		Set<String> set = tm.keySet();
		for (String key : set) {
			String value = tm.get(key);
			System.out.println(key + "---" + value);
		}
	}
}

B:TreeMap<Student,String>

import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;

/*
 * TreeMap<Student,String>
 * 键:Student
 * 值:String
 */
public class TreeMapTest {
	public static void main(String[] args) {
		TreeMap<Student, String> tm = new TreeMap<Student, String>(
				new Comparator<Student>() {
					@Override
					public int compare(Student s1, Student s2) {
						
						int num = s1.getAge() - s2.getAge();
						
						int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) :num;
						return num2;
					}
				});

		Student s1 = new Student("潘安", 30);
		Student s2 = new Student("柳下惠", 35);
		Student s3 = new Student("唐伯虎", 33);
		Student s4 = new Student("燕青", 32);
		Student s5 = new Student("唐伯虎", 33);
		Student s6 = new Student("啊三", 30);

		tm.put(s1, "宋朝");
		tm.put(s2, "元朝");
		tm.put(s3, "明朝");
		tm.put(s4, "清朝");
		tm.put(s5, "汉朝");
		tm.put(s6, "秦国");

		Set<Student> set = tm.keySet();
		for (Student key : set) {
			String value = tm.get(key);
			System.out.println(key.getName() + "---" + key.getAge() + "---"
					+ value);
		}
	}
}

(7)练习

A:统计一个字符串中每个字符出现的次数

import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

/*
 * 需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
 * 
 * 分析:
 * 		A:定义一个字符串(可以改进为键盘录入)
 * 		B:定义一个TreeMap集合
 * 			键:Character
 * 			值:Integer
 * 		C:把字符串转换为字符数组
 * 		D:遍历字符数组,得到每一个字符
 * 		E:拿刚才得到的字符作为键到集合中去找值,看返回值
 * 			是null:说明该键不存在,就把该字符作为键,1作为值存储
 * 			不是null:说明该键存在,就把值加1,然后重写存储该键和值
 * 		F:定义字符串缓冲区变量
 * 		G:遍历集合,得到键和值,进行按照要求拼接
 * 		H:把字符串缓冲区转换为字符串输出
 * 
 * 录入:linqingxia
 * 结果:result:a(1)g(1)i(3)l(1)n(2)q(1)x(1)
 */
public class TreeMapDemo {
	public static void main(String[] args) {
		// 定义一个字符串(可以改进为键盘录入)
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个字符串:");
		String line = sc.nextLine();

		// 定义一个TreeMap集合
		TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
		
		//把字符串转换为字符数组
		char[] chs = line.toCharArray();
		
		//遍历字符数组,得到每一个字符
		for(char ch : chs){
			//拿刚才得到的字符作为键到集合中去找值,看返回值
			Integer i =  tm.get(ch);
			
			//是null:说明该键不存在,就把该字符作为键,1作为值存储
			if(i == null){
				tm.put(ch, 1);
			}else {
				//不是null:说明该键存在,就把值加1,然后重写存储该键和值
				i++;
				tm.put(ch,i);
			}
		}
		
		//定义字符串缓冲区变量
		StringBuilder sb=  new StringBuilder();
		
		//遍历集合,得到键和值,进行按照要求拼接
		Set<Character> set = tm.keySet();
		for(Character key : set){
			Integer value = tm.get(key);
			sb.append(key).append("(").append(value).append(")");
		}
		
		//把字符串缓冲区转换为字符串输出
		String result = sb.toString();
		System.out.println("result:"+result);
	}
}

B:集合的嵌套遍历

二:Collections(理解)

(1)是针对集合进行操作的工具类

(2)面试题:Collection和Collections的区别

	A:Collection 是单列集合的顶层接口,有两个子接口List和Set
	B:Collections 是针对集合进行操作的工具类,可以对集合进行排序和查找等

(3)常见的几个小方法:

	A:public static <T> void sort(List<T> list)
	B:public static <T> int binarySearch(List<?> list,T key)
	C:public static <T> T max(Collection<?> coll)
	D:public static void reverse(List<?> list)
	E:public static void shuffle(List<?> list)
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-08-01 14:22:10  更:2021-08-01 14:23:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 7:52:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码