前言
本期任务:毕向东老师Java视频教程学习笔记(共计25天)
原视频链接:黑马程序员_毕向东_Java基础视频教程day01:编写HelloWorld程序day02:操作符与条件选择语句day03:循环语句与函数day04:数组day07:继承、抽象类与接口day08:多态day09:异常处理day11:多线程day12:线程安全与同步机制day13:String类day14:集合(ArrayList,LinkedList,HashSet)day15:集合(TreeSet)和泛型)day16:集合(HashMap、TreeMap)day17:集合框架的工具类(Arrays、Collections)day18:IO流(字符流读写)day19:IO流(字节流、转换流读写)day20:IO流(File对象)
代码
import java
.util
.*
;
public class MapDemo {
public static void main(String
[] args
) {
Map
<String, String> map
= new HashMap<String, String>();
System
.out
.println("put: "+map
.put("01", "张三"));
System
.out
.println("put: "+map
.put("01", "张三1"));
map
.put("02", "李四");
map
.put("03", "王五");
System
.out
.println("remove: "+ map
.remove("01"));
System
.out
.println("get: "+ map
.get("02"));
System
.out
.println("get: "+ map
.get("05"));
System
.out
.println("ContainsKey: "+map
.containsKey("01"));
System
.out
.println("ContainsKey: "+map
.containsKey("05"));
Collection
<String> coll
= map
.values();
System
.out
.println(coll
);
System
.out
.println(map
);
}
}
import java
.util
.*
;
public class MapDemo2 {
public static void main(String
[] args
) {
Map
<String, String> map
= new HashMap<String, String>();
map
.put("张三", "01");
map
.put("张三", "02");
map
.put("李四", "01");
map
.put("王五", "03");
Set
<Map
.Entry
<String, String>> entrySet
= map
.entrySet();
Iterator
<Map
.Entry
<String, String>> it
= entrySet
.iterator();
while (it
.hasNext()) {
Map
.Entry
<String, String> me
= it
.next();
System
.out
.println("key: " + me
.getKey() + " value: " + me
.getValue());
}
}
}
import java
.util
.ArrayList
;
import java
.util
.HashMap
;
import java
.util
.Iterator
;
import java
.util
.List
;
class Student{
private String id
;
private String name
;
Student(String id
, String name
){
this.id
= id
;
this.name
= name
;
}
public String toString
(){
return id
+ "::"+name
;
}
}
public class MapDemo3 {
public static void main(String
[] args
) {
HashMap
<String
, List
<Student>> czbk
= new HashMap<String
, List
<Student>>();
List
<Student> yure
= new ArrayList<Student>();
List
<Student> jiuye
= new ArrayList<Student>();
czbk
.put("yure", yure
);
czbk
.put("jiuye", jiuye
);
yure
.add(new Student("01", "zhangsan"));
yure
.add(new Student("02", "lisi"));
jiuye
.add(new Student("01", "zhangsan"));
jiuye
.add(new Student("03", "wangwu"));
Iterator
<String> it
= czbk
.keySet().iterator();
while (it
.hasNext()){
String roomName
= it
.next();
List
<Student> room
= czbk
.get(roomName
);
System
.out
.println(roomName
);
getInfos(room
);
}
}
public static void getInfos(List
<Student> list
){
Iterator
<Student> it
= list
.iterator();
while (it
.hasNext()){
System
.out
.println(it
.next().toString());
}
}
}
import java
.util
.*
;
class Student1 implements Comparable<Student1> {
private int age
;
private String name
;
Student1(int age
, String name
) {
this.age
= age
;
this.name
= name
;
}
public String
getName() {
return name
;
}
public int getAge() {
return age
;
}
public String
toString() {
return name
+ "::" + age
;
}
public int compareTo(Student1 s
) {
int num
= ((Integer
) age
).compareTo((Integer
) s
.age
);
if (num
== 0) {
return name
.compareTo(s
.name
);
}
return num
;
}
public boolean equals(Object obj
) {
if (!(obj
instanceof Student1)) {
throw new RuntimeException("类型不匹配");
}
Student1 s
= (Student1
) obj
;
return s
.name
== name
&& s
.age
== age
;
}
public int hashCode() {
return name
.hashCode() + 47 * age
;
}
}
public class MapTest {
public static void main(String
[] args
) {
HashMap
<Student1, String> map
= new HashMap<Student1, String>();
map
.put(new Student1(10, "张三"), "北京");
map
.put(new Student1(11, "张三"), "上海");
map
.put(new Student1(10, "李四"), "福建");
map
.put(new Student1(12, "王五"), "成都");
Set
<Map
.Entry
<Student1, String>> entrySet
= map
.entrySet();
Iterator
<Map
.Entry
<Student1, String>> it
= entrySet
.iterator();
while (it
.hasNext()) {
Map
.Entry
<Student1, String> me
= it
.next();
System
.out
.println("key: " + me
.getKey().toString() + ", " + "value: " + me
.getValue());
}
}
}
import java
.util
.*
;
class Student2 implements Comparable<Student2> {
private int age
;
private String name
;
Student2(int age
, String name
) {
this.age
= age
;
this.name
= name
;
}
public String
getName() {
return name
;
}
public int getAge() {
return age
;
}
public String
toString() {
return name
+ "::" + age
;
}
public int compareTo(Student2 s
) {
int num
= ((Integer
) age
).compareTo((Integer
) s
.age
);
if (num
== 0) {
return name
.compareTo(s
.name
);
}
return num
;
}
public boolean equals(Object obj
) {
if (!(obj
instanceof Student2)) {
throw new RuntimeException("类型不匹配");
}
Student2 s
= (Student2
) obj
;
return s
.name
== name
&& s
.age
== age
;
}
public int hashCode() {
return name
.hashCode() + 47 * age
;
}
}
class StuNameComparable implements Comparator<Student2> {
public int compare(Student2 s1
, Student2 s2
) {
int num
= s1
.getName().compareTo(s2
.getName());
if (num
== 0) {
return ((Integer
) s1
.getAge()).compareTo((Integer
) s2
.getAge());
}
return num
;
}
}
public class MapTest2 {
public static void main(String
[] args
) {
TreeMap
<Student2, String> map
= new TreeMap<Student2, String>(new StuNameComparable());
map
.put(new Student2(10, "张三"), "北京");
map
.put(new Student2(11, "张三"), "上海");
map
.put(new Student2(10, "李四"), "福建");
map
.put(new Student2(12, "王五"), "成都");
Set
<Map
.Entry
<Student2, String>> entrySet
= map
.entrySet();
Iterator
<Map
.Entry
<Student2, String>> it
= entrySet
.iterator();
while (it
.hasNext()) {
Map
.Entry
<Student2, String> me
= it
.next();
System
.out
.println("key: " + me
.getKey().toString() + ", " + "value: " + me
.getValue());
}
}
}
import java
.util
.*
;
public class MapTest3 {
public static void main(String
[] args
) {
String str
= "sdfgzxcvasdfxcvdf";
System
.out
.println(charCount(str
));
}
public static String
charCount(String str
) {
char[] arr
= str
.toCharArray();
TreeMap
<Character, Integer> tm
= new TreeMap<Character, Integer>();
for (int x
= 0; x
< arr
.length
; x
++) {
if (!(arr
[x
] >= 'A' && arr
[x
] <= 'Z' || arr
[x
] >= 'a' && arr
[x
] <= 'z'))
continue;
if (tm
.get(arr
[x
]) != null
) {
tm
.put(arr
[x
], tm
.get(arr
[x
]) + 1);
} else {
tm
.put(arr
[x
], 1);
}
}
StringBuilder sb
= new StringBuilder();
Set
<Map
.Entry
<Character, Integer>> entrySet
= tm
.entrySet();
Iterator
<Map
.Entry
<Character, Integer>> it
= entrySet
.iterator();
while (it
.hasNext()){
Map
.Entry
<Character, Integer> me
= it
.next();
sb
.append(me
.getKey()+"("+me
.getValue()+")");
}
return sb
.toString();
}
}