目录
一、本周学习内容:
二、学习笔记:
(1)Map集合
1.Map集合的初步认识:
2.Map集合的特点和常用方法
3.Map集合的遍历
4.Map集合的底层原理
(2)Collections工具类
1.Collections的常用方法
三、编程练习
(1)数组练习
1.矩阵顺时针打印
2.矩阵查找某个值(快捷法)
(2)StringJoiner练习
1.练习
(3)集合统一练习
1.扑克牌的洗牌、发牌(无排序)
2.统计80个人中去abcd四个景点的人数(用Map)
四、学习感悟:
五、下周学习计划:
1.Map双列集合中HashMap、LinkedHashMap、TreeMap
2.简单认识了Java网络编程
3.复习了前面学的Collection集合,学习了Collections工具类
Map集合:每个元素必须存放两个数据,每次存放数据要存两个,占一个元素
格式:{key1=value1,key2=value2,......},一次存放两个数据
Map集合的每个元素“key=value”称为一个键值对/键值对对象,Map集合也叫做键值对集合
key叫做键,value叫做值,键不能重复,值可以重复,键和值是一一对应的
应用场景:描述购物车,夫妻关系等一一对应的数据|----HashMap
实现类----LinkedHashMap 实现类
|
Map接口---|
|
|----TreeMap实现类 (注:Map集合的特点都是由******键****决定的,值只是一个附属品,不做要求)
Map集合特点:
HashMap:无序、不重复、无索引
LinkedHashMap:有序、不重复、无索引
TreeMap:按大小升序排序、不重复、无索引Map普用方法:1. put( , ); //往集合里添加元素
2. size(); //获取Map集合元素的个数
3. m.clear(); //清空集合
4.m.isEmpty(); //判断集合是否为空
5.m.get(key); //根据输入的键,输出对应的“值”。若键不存在,则返回null
6.m.remove(key); //根据键删除整个元素,并且返回键对应的值
7.m.containsKey(key); //判断m集合里面有木有包含某个键
8.m.containsValue(key); //判断m集合里面有木有包含某个值
9.Setkeys=m.keySet(); //将m集合里面的所有键转成一个Set集合
10.Collection values = m.values; //将m集合里面的全部值转成一个Collection集合
11.m1.putAll(m2); //将m2集合里的全部元素倒入集合m1里
Map集合的遍历:一个一个的访问Map集合里面的键值对元素
1.键找值:先获取Map集合全部的键,再通过遍历键来找值
先用keySet方法获取全部的键,再用键遍历获取值例:Map
maps=new HashMap<>(); //创建Map集合
maps.put("张三",18); //添加元素
maps.put("李四",20);
maps. put("王五",17);
Setkeys = maps.keySet(); //将全部键转变成一个Set集合存住
for(String key:keys){ //遍历键集合
Int value=maps.get(key);
System.out.prinnln(key===value); //根据键获取值
}
2.键值对:把键值对看作一个整体来进行遍历(主要考虑的是数据类型)
Mapmaps=new HashMap<>(); //创建Map集合
maps.put("张三",18); //添加元素
maps.put("李四",20);
maps. put("王五",17); Map.Entry为一个类
Set> entrys=maps.entrySet(); //通过entrySet方法将每个键值对封装成一个一个的对象,并且交给Set集合存住,Set集合的数据类型就为Map.Entry
for(Map.Entryentry : entrys){ //遍历
String key = entry.getkey(); //Map.Entry提供了getkey和getValue方法来获取当前Map对象里面的键和值
int value = entry.getValue();
System.out.prinnln(key===value);
}3.Lambda表达式:调用forEach()方法
Map
maps=new HashMap<>(); //创建Map集合
maps.put("张三",18); //添加元素
maps.put("李四",20);
maps. put("王五",17);
maps.forEach((k,v)->System.out.println("键:"+k+" 值:"+v));
HashMap:底层原理:也是基于哈希表实现的
实际上:Set集合的底层原理就是基于Map实现的,只是只去键不取值而已若键的数据为一个对象,其不能直接去重复,需要重写hashCode和equals方法
LinkedHashMap:底层原理依然是基于哈希表来实现的,多了一个双链表来记录数据
TreeMap: 底层原理也是基于红黑树实现的
1.对于Integer、Double来说是根据数值本身大小排序的
2.对于String来说是默认按照首字符的编号排序
Collections工具类:是一个用来操作集合的工具类
其提供静态方法用来操作集合
方法:1.addAll(Collection集合,数据); //往Collection集合里面批量添加数据
比add()方法简便:
Listnames = new ArrayList<>();
names.add("张三");
names.add("李四");
names.add("王五");
等价于:Collection.addAll(names,"张三","李四","王五");
2.shuffle(List集合) //打乱List集合里面的元素(Set本身就是乱的)
Collections.shuffle(names);
3.sort(List集合) //对List集合进行升序排序(Set集合有TreeSet可以进行排序)
Collections.sort(names);
//对一个矩阵进行顺时针打印
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr = {
{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9,10},
{11,12,13,14,15},
{16,17,18,19,20}
};
int h1=0;
int h2=arr.length-1;
int l1=0;
int l2=arr[0].length-1;
while(h1<=h2&&h1<=h2) {
//上
for(int i=l1;i<=l2;i++) {
System.out.println(arr[h1][i]);
}
//右
for(int i=h1+1;i<=h2;i++) {
System.out.println(arr[i][l2]);
}
//下
if(h1!=h2)
for(int i=l2-1;i>=l1;i--) {
System.out.println(arr[h2][i]);
}
//左
if(l1!=l2)
for(int i=h2-1;i>=h1+1;i--) {
System.out.println(arr[i][l1]);
}
h1++;h2--;l1++;l2--;
}
}
}
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr= {
{1, 4, 7, 11,15},
{2, 5, 8, 12,19},
{3, 6, 9, 16,22},
{10,13,14,17,24},
{18,21,23,26,30}
};
compare a=new compare();
boolean b = a.find(55, arr);
System.out.println(b);
}
}
public class compare {
public boolean find(int target,int[][] arr) {
int H=0;
int L=arr[0].length-1;
while(H<=arr.length-1&&L>=0) {
if(target==arr[H][L]) {return true;}
if(target>arr[H][L]) {
H++;
}
else if(target= 0) {
if (target == matrix[r][c])
return true;
else if (target > matrix[r][c])
r++;
else
c--;
}
return false;*/
}
import java.util.StringJoiner;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringJoiner sj = new StringJoiner(",","[","]");
sj.add("我的世界");
sj.add("The forest");
sj.add("泰拉瑞亚");
System.out.println(sj);
int size=sj.length();
System.out.println(size);
}
}
import java.util.ArrayList;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
room play = new room();
}
}
public class cards {
private String num;
private String color;
private int size;
@Override
public String toString() {
return num + "" + color;
}
public cards() {
}
public cards(String num, String color, int size) {
super();
this.num = num;
this.color = color;
this.size = size;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
public class room {
public room() {
//创建54张牌的对象
String[] num = {"3","4","5","6","7","8","9","10","A","2","j","Q","k","",""};
String[] colors = {"♠","♣","♥","♦"};
List cardarr= new ArrayList<>();
for(int i=0;i<=12;i++) {
for(int j=0;j<=3;j++) {
cards card = new cards(num[i],colors[j],i);
cardarr.add(card);
}
}
cardarr.add(new cards("","",13));
cardarr.add(new cards("","",13));
System.out.println("牌为:"+cardarr);
Collections.shuffle(cardarr);
System.out.println("开始洗牌~~");
System.out.println("打乱后的牌为:"+cardarr);
System.out.println("开始发牌~~");
List Zhangsan = new ArrayList<>();
List Lisi = new ArrayList<>();
List Wangwu= new ArrayList<>();
for(int i=0;i<=50;i++) {
if(i%3==0) {
Zhangsan.add(cardarr.get(i));
}else if(i%3==1) {
Lisi.add(cardarr.get(i));
}else if(i%3==2) {
Wangwu.add(cardarr.get(i));
}
}
System.out.println("张三的牌为:"+Zhangsan);
System.out.println("李四的牌为:"+Lisi);
System.out.println("王五的牌为:"+Wangwu);
List bottomcards = new ArrayList<>();
Collections.addAll(bottomcards,cardarr.get(51),cardarr.get(52),cardarr.get(53));
System.out.println("底牌为:"+bottomcards);
Random r1 = new Random();
int rd=r1.nextInt(2);
if(rd%3==0) {
Zhangsan.addAll(bottomcards);
System.out.println("张三获得了底牌~");
}else if(rd%3==1) {
Lisi.addAll(bottomcards);
System.out.println("李四获得了底牌~");
}else if(rd%3==2) {
Wangwu.addAll(bottomcards);
System.out.println("王五获得了底牌~");
}
System.out.println("张三的牌为:"+Zhangsan);
System.out.println("李四的牌为:"+Lisi);
System.out.println("王五的牌为:"+Wangwu);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class travel {
public static void main(String[] args) {
// TODO Auto-generated method stub
//一个班80人,要去旅游,景点有ABCD四个,得到景点人数数据
List date = new ArrayList<>();
String[] destination= {"A","B","C","D"};
Random r=new Random();
for(int i=1;i<=80;i++) {
int num = r.nextInt(4);
date.add(destination[num]);
}
Map resault = new HashMap();
for(String d:date) {
if(resault.containsKey(d)) {
resault.put(d, resault.get(d)+1);
}else {
resault.put(d, 1);
}
}
System.out.println(resault);
/**Set keys = resault.keySet(); //遍历方法1
int max=0;
String tour = null;
for(String key:keys) {
if(resault.get(key)>max) {
max=resault.get(key);
tour=key;
}
}*/ //遍历方法二
Set> entries=resault.entrySet();
int max = 0;
String tour = null;
for(Map.Entry entry:entries) {
String key=entry.getKey();
int value=entry.getValue();
if(value>max) {
max=value;
tour=key;
}
}
System.out.println("因为"+tour+"投票人数为"+max+"票最多,故去"+tour+"旅游");
}
}
学计算机编程真的要多敲敲代码,光记真不行。这周的学习总的来说还行,给练习留了挺长时间。
按照学习计划进行,同时复习这之前学习的内容