一、Map集合
(一)Map集合第一种遍历
1、获取
Map
集合中所有的键,存储在一个
Set
集合中,之后遍历
Set
集合,获取每一个键,根据键获取对应的值
2、获取
Map
集合中所有的键:
keySet
()
3、遍历
Set
集合
(1)迭代器遍历
(2)增强
for
循环遍历
4、获取每一个键之后,根据键获取值的方法:
get
(Object key)
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Demo01_PrintMapOne {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(234, "qwe");
map.put(345, "asd");
map.put(745, "zxc");
map.put(247, "sdf");
map.put(542, "cvb");
map.put(845, "rty");
//增强for循环简写
for (Integer key : map.keySet()) {
String value = map.get(key);
System.out.println(key + "..." + value);
}
//获取键集
Set<Integer> set = map.keySet();
//增强for循环
for (Integer key : set) {
String value = map.get(key);
System.out.println(key + "..." + value);
}
}
public static void test(Map<Integer, String> map) {
//获取键集
Set<Integer> set = map.keySet();
//迭代器遍历
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
//Set中的元素就是Map中的每一个键
Integer key = it.next();
//根据键获取对应的值
String value = map.get(key);
System.out.println(key + "..." + value);
}
}
}
(二)Map集合第二种遍历
1、获取
Map
集合中所有的键值对对象(
Entry
),存储在一个
Set
集合中,遍历
Set
集合,获取每一个键值对对象,从键值对对象中,获取键和值
2、根据
Map
集合中提供的获取键值对对象的方法
:
entrySet
()
Set<Map.Entry<Key, Value>> set = map.entrySet();
3、遍历
Set
集合,获取每一个键值对对象:迭代器遍历、增强
for
循环遍历
4、得到
Entry
对象之后,从对象中获取
Key
和
Value
(1)获取
Key
:
getKey
()
(2)获取
Value
:
getValue
()
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Demo02_PrintMapTwo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(234, "qwe");
map.put(567, "yyy");
map.put(345, "asd");
map.put(745, "zxc");
map.put(247, "sdf");
map.put(542, "cvb");
map.put(845, "rty");
//增强for循环简写
for(Map.Entry<Integer, String> entry : map.entrySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "..." + value);
}
//获取键值对对象
Set<Map.Entry<Integer, String>> set = map.entrySet();
//增强for循环遍历
for(Map.Entry<Integer, String> entry : set) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "..." + value);
}
}
//迭代器遍历
public static void test(Map<Integer, String> map) {
//获取键值对对象
Set<Map.Entry<Integer, String>> set = map.entrySet();
//获取迭代器对象 泛型类型和Set集合保持一致
Iterator<Map.Entry<Integer, String>> it = set.iterator();
while (it.hasNext()) {
//获取每一个键值对对象
Map.Entry<Integer, String> entry = it.next();
//根据键值对中的方法获取对应的键和值
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "..." + value);
}
}
}
(三)练习
键盘录入一个字符串,统计每一个字符出现的次数,使用Map
集合
打印格式为:a
有
1
个,
b
有
3
个
...
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Demo03_Exercise {
/**
* 键盘录入一个字符串,统计每一个字符出现的次数,使用Map集合
* 打印格式为:a有1个,b有3个...
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请录入一个字符串:");
String str = sc.nextLine();
//使用Map集合进行存储
Map<Character, Integer> map = new HashMap<>();
//为了统计字符个数,先将字符串转为字符数组
char[] arr = str.toCharArray();
//遍历数组获取每一个字符
for (char key : arr) {
map.put(key, map.containsKey(key) ? map.get(key) + 1 : 1);
}
System.out.println(getResult(map));
/*遍历Map集合
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
Character key = entry.getKey();
Integer value = entry.getValue();
System.out.print(key + "有" + value + "个,");
}*/
}
public static <K, V> String getResult(Map<K, V> map) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<K, V> entry : map.entrySet()) {
K key = entry.getKey();
V value = entry.getValue();
sb.append(key).append("有").append(value).append("个,");
}
//将StringBuilder对象转为字符串,进行裁剪
String str = sb.toString();
//进行字符串裁剪,删除最后被一个逗号
return str.substring(0, str.length() - 1);
}
}
(四)HashMap
1、是
Map
集合的子实现类
2、
HashMap
存储
JDK
提供类型的元素都可以去重
3、
HashMap
存储自定义类型元素作为键,无法保证键的唯一性:原因:和
HashSet
的去重原理一样,想要去重,在自定义类型所在类中重写hashCode
和
equals
方法
4、
HashMap
和
HashSet
:
HashSet
就是
HashMap
键的那一列,
HashSet
是由
HashMap
实现出来的
import java.util.*;
public class Demo04_HashMap {
public static void main(String[] args) {
HashMap<Person, Integer> map = new HashMap<>();
new HashSet<>();
map.put(new Person("迪丽热巴", "新疆"), 23);
map.put(new Person("迪丽热巴", "新疆"), 23);
map.put(new Person("古力娜扎", "新疆"), 23);
map.put(new Person("迪丽热巴", "新疆"), 30);
Set<Map.Entry<Person, Integer>> set = map.entrySet();
Iterator<Map.Entry<Person, Integer>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<Person, Integer> entry = it.next();
Person key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key.hashCode() + "..." + value);
}
}
}
(五)HashMap和Hashtable区别
1、相同点:
HashMap和
Hashtable
都是
map
接口的实现类,都是双列集合,底层都是哈希表
2、不同点:
(1)
HashMap
:不安全,
Hashtable
:安全
(2)
HashMap
:效率高,
Hashtable
:效率低
(3)
HashMap
:允许存储
null
值
null
键,
Hashtable
:不允许
(4)
HashMap
:初始容量为
16
,
Hashtable
:初始容量为
11
(5)
HashMap
:扩容为原来的两倍,
Hashtable
:扩容为原来的两倍
+1
(6)
HashMap
:会重新计算哈希码值,
Hashtable
:会直接使用元素的
hashCode
(7)
HashMap
:出现在
JDK1.2
,
Hashtable
:出现在
JDK1.0
二、File类型
(一)概述
1、
File
类型:用于表示一个文件或者文件夹的路径
2、路径:用于描述文件或者文件夹存储位置的字符串
(1)绝对路径:从根目录开始的路径【可以理解为本地路径
】
(2)相对路径:从项目路径开始的【
src\com\hqyj\demos\Person.java
】
(二)构造方法
1、
File
(String pathname)
:将一个字符串表示的路径封装为
File
对象
2、
File
(String parent, String child)
:将父级路径和子级路径封装为一个
File
对象,对象中的父级路径是字符串
3、
File
(File parent, String child)
:将父级路径和子级路径封装为一个
File
对象,对象中的父级路径是File对象
import java.io.File;
public class Demo06_File {
public static void main(String[] args) {
File file = new File("src\\com\\hqyj\\demos");
File file1 = new File(file, "Person.java");
System.out.println(file1);
File file2 = new File("src\\com\\hqyj\\demos", "Person.java");
System.out.println(file2);
}
}
(三)创建文件的方法
1、
createNewFile
()
:当且仅当不存在具有此抽象路径名指定名称的文件时,创建一个新的文件
2、
mkdir
()
:只能创建单个文件夹,当且仅当
File
对象后表示的抽象文件夹路径不存在的时候,
3、
mkdirs
()
:可以创建嵌套文件夹,如果父级路径不存在,直接创建
(四)练习
在D
盘下的
a/b/c/d
目录下创建一个文件,名称叫做
Helloworld.txt
import java.io.File;
import java.io.IOException;
public class Demo08_Exercise {
public static void main(String[] args) throws IOException {
/**
* 在D盘下的a/b/c/d目录下创建一个文件,名称叫做HelloWorld.txt
* D:a/b/c/d
* D:a\\b\\c\\d
*/
//创建路径
File file = new File("D:a/b/c/d");
//创建嵌套文件夹
file.mkdirs();
//新的路径是父级路径+文件名
File newFile = new File(file, "HelloWorld.txt");
//创建文件
newFile.createNewFile();
}
}
(五)删除的方法
1、
delete
()
:删除指定路径的文件或者文件夹
2、注意
(1)使用该方法删除的文件夹只能是空文件夹
(2)使用该方法删除不走回收站
import java.io.File;
public class Demo09_Delete {
public static void main(String[] args) {
File file = new File("D:\\a\\b\\c\\d\\HelloWorld.txt");
System.out.println(file.delete());
}
}
(六)重命名方法
1、
renameTo
(File dest)
:将调用者对象表示的文件使用参数对象表示的新名称重新命名
2、注意:
(1)在同路径下,该方法是重命名
(2)在不同路径下,该方法是剪切并且重命名
import java.io.File;
public class Demo10_ReName {
public static void main(String[] args) {
File file = new File("a.txt");
File newFile1 = new File("aaa.txt");
File newFile2 = new File("D:\\a\\b\\c\\d\\a.txt");
System.out.println(newFile1.renameTo(newFile2));
}
}
(七)判断的方法
1、
exists
()
:判断调用者表示的抽象路径是否存在
2、
isFile
()
:判断调用者表示的抽象路径是一个文件
3、
isDirectory
()
:判断调用者表示的抽象路径是一个文件夹
import java.io.File;
public class Demo11_IsFile {
public static void main(String[] args) {
File file = new File("b.txt");
System.out.println(file.exists());
System.out.println(file.isFile());
System.out.println(file.isDirectory());
}
}
(八)获取功能
1、
getAbsoluteFile
()
:获取当前
File
表示的内容的绝对路径
2、
getName
()
:获取文件或者文件夹的名称
3、
list
()
:获取
File
对象指代的文件夹下的所有文件和文件夹存储在一个
String
数组中
4、
length
()
:获取文件的长度,长度指的是字节数
5、
listFiles
()
:将调用者对象表示的文件夹下,每一个文件或者文件夹都封装为一个
File
对象存储在一个File数组中
6、
getPath
()
:将
File
对象表示的路径转为字符串
(九)练习
键盘录入一个字符串,该字符串表示一个文件夹路径,如果不是文件夹则重新录入
打印当前文件夹下,所有大于
5M
的后缀名为
.mp4
格式的文件的绝对路径
import java.io.File;
import java.util.Scanner;
public class Demo13_Exercise {
/**
* 键盘录入一个字符串,该字符串表示一个文件夹路径,如果不是文件夹则重新录入
* 打印当前文件夹下,所有大于5M的后缀名为.mp4格式的文件的绝对路径
*/
public static void main(String[] args) {
File file = getDir();
//遍历文件夹中的内容
File[] arr = file.listFiles();
//遍历数组获取每一个文件或者文件夹
for (File path : arr) {
//判断是不是文件
if (path.isFile()) {
//获取文件的名字
String name = path.getName();
//获取文件名最后一个点出现的索引
int index = name.lastIndexOf(".");
//截取点直到结尾就是后缀名
String suffixName = name.substring(index);
//判断文件的后缀是不是.mp4格式的
if (suffixName.equals(".mp4")) {
//判断文件的长度是否大于5M 8bit KB MB GB TB
if (path.length() / 1024 > 5) {
System.out.println(path.getAbsoluteFile());
}
}
}
}
}
//判断当前录入的是不是一个文件夹路径
public static File getDir() {
Scanner sc = new Scanner(System.in);
System.out.println("请录入一个文件夹路径:");
while (true) {
String path = sc.nextLine();
//将String转为File
File file = new File(path);
//判断当前字符串是不是合法的文件夹路径
if (file.isDirectory()) {
return file;
} else {
System.out.println("请录入合法的文件夹路径:");
}
}
}
}
(十)File和递归
键盘录入一个文件夹路径,删除该文件夹,该文件夹是非空文件夹
import java.io.File;
public class Demo14_Exercise {
/**
* 键盘录入一个文件夹路径,删除该文件夹,该文件夹是非空文件夹
*/
public static void main(String[] args) {
File file = Demo13_Exercise.getDir();
deleteFile(file);
}
public static void deleteFile(File file) {
//开发文件夹
File[] arr = file.listFiles();
//获取文件夹中的每一个内容
for (File path : arr) {
//判断是不是文件
if (path.isFile()) {
path.delete();
} else {
System.out.println(path);
deleteFile(path);
}
}
file.delete();
}
}