java学习笔记之Map、HashMap、TreeMap、Collections

Map

 

HashMap


遍历map:

方法一:使用keySet()方法

package com.mcq;

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

public class HashMapDemo {
	public static void main(String[] args) {
		Map<String, Integer> map = new HashMap();
		map.put("小明",18);
		map.put("小花",19);
		map.put("小丽",16);
		map.put("小花", 15);
		Set<String> keySet=map.keySet();
		for(String key:keySet){
			System.out.println(key+":"+map.get(key));
		}
	}
}

/*
小明:18
小丽:16
小花:15
*/
//Student.java
package com.mcq;
public class Student{
	private String name;
	private int age;
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = 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;
	}
	@Override
	public int hashCode() { //右键source点hashCode()和equals()快捷生成
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}	

//HashMapDemp.java
package com.mcq;

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

public class HashMapDemo {
	public static void main(String[] args) {
		Map<Student, Integer> map = new HashMap();
		map.put(new Student("小明", 18),99);
		map.put(new Student("小花",19),98);
		map.put(new Student("小丽",16),97);
		map.put(new Student("小花",20),101);
		Set<Student> keySet=map.keySet();
		for(Student key:keySet){
			System.out.println(key.getName()+" "+key.getAge()+":"+map.get(key));
		}
	}
}

方法二:使用entrySet方法():
 

package com.mcq;

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) {
		Map<Student, Integer> map = new HashMap();
		map.put(new Student("小明", 18), 99);
		map.put(new Student("小花", 19), 98);
		map.put(new Student("小丽", 16), 97);
		map.put(new Student("小丽", 16), 97);
		map.put(new Student("小丽", 16), 97);
		Set<Entry<Student, Integer>> entrySet = map.entrySet();
		for (Entry<Student, Integer> entry : entrySet) {
			System.out.println(entry.getKey().getName()+" "+entry.getKey().getAge()+" "+entry.getValue());
		}
	}
}

TreeMap

匿名内部类方法:

package com.mcq;

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

public class TreeMapDemo {
	public static void main(String[] args) {
		Map<Student, Integer> map=new TreeMap(new Comparator<Student>() {

			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				int t=o1.getAge()-o2.getAge();
				return t==0?o1.getName().compareTo(o2.getName()):t;
			}
			
		});
		map.put(new Student("小明", 18), 99);
		map.put(new Student("小花", 19), 98);
		map.put(new Student("小丽", 16), 97);
		map.put(new Student("小丽", 16), 97);
		map.put(new Student("小丽", 16), 97);
		Set<Entry<Student, Integer>> entrySet = map.entrySet();
		for (Entry<Student, Integer> entry : entrySet) {
			System.out.println(entry.getKey().getName()+" "+entry.getKey().getAge()+" "+entry.getValue());
		}
	}
}

Collections工具类

包含处理集合的一些静态方法

Arrays工具类

包含处理数组的静态方法

实例:

package com.mcq;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionsDemo {
	public static void main(String[] args) {
		List<String> list=new ArrayList<String>();
		list.add("hello");
		list.add("world");
		list.add("haha");
		System.out.println(list);
		
		Collections.sort(list);//排序
		
		System.out.println(list);
		
		Collections.reverse(list);//反转
		
		System.out.println(list);
		
		Collections.shuffle(list);//洗牌,随机顺序
		
		System.out.println(list);
		
		List<Student> list2=Arrays.asList(new Student("a",16),new Student("c", 19),new Student("b", 15));//数组转list
		
		System.out.println(list2);
		
		Collections.sort(list2,new Comparator<Student>() {

			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				int t=o1.getAge()-o2.getAge();
				return t==0?o1.getName().compareTo(o2.getName()):t;
			}
			
		});
		System.out.println(list2);
	}
}

 

内容概要:本文档《Docker 新手入门指南》详细介绍Docker这一开源容器化平台,旨在帮助新手理解并掌握Docker的核心概念和基本操作。文中首先解释了Docker的概念及其相对于传统虚拟机的优势,如更快的启动速度、更低的资源占用和更好的隔离性。接着,文档提供了详细的安装步骤,包括不同操作系统下的安装方法以及针对国内用户的镜像加速配置。随后,文章深入讲解了镜像管理和容器操作的基础命令,如拉取镜像、运行容器等。进一步地,文档介绍了使用Dockerfile构建自定义镜像、实现数据持久化、进行端口映射以及利用Docker Compose管理多容器应用等高级技巧。最后,给出了一些学习建议和注意事项,鼓励读者动手实验并关注安全性。 适合人群:适合对容器技术感兴趣的初学者,尤其是有一定Linux基础或打算深入了解Docker的开发人员。 使用场景及目标:①帮助读者快速上手Docker,掌握从安装到实际操作的一系列技能;②通过实例演示,如构建Python Web服务、部署WordPress和搭建Jenkins环境,让读者能够将所学应用于实际项目中;③强调容器化的优势,如提高部署效率、解决环境差异问题。 阅读建议:建议读者跟随文档逐步操作,亲身体验每个步骤,同时参考官方文档和社区资源,不断实践以巩固所学知识。特别注意安全性和资源管理方面的提示,确保容器环境的安全稳定运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值