文章目录
list集合
list常用方法
import java.util.ArrayList;
import java.util.List;
/*
java.lang.List接口 extends Collection接口
List接口的特点:
1. 有序的集合:存储元素和取出元素的顺序是一致的
2. 有索引,包含了一些带索引的方法
3. 允许存储重复的元素
注意:
操作索引时一定要防止索引越界异常
*/
public class Demo01List {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("a");
System.out.println(list);
//public void add(int index,E element):将指定的元素,添加到该集合中的指定位置上
list.add(3,"新加的元素");
System.out.println(list);//[a, b, c, 新加的元素, a]
//public E remove(int index):移除列表中指定位置的元素,返回的是被移除的元素
String removeE = list.remove(2);
System.out.println(removeE);//c
System.out.println(list);//[a, b, 新加的元素, a]
//public E set(int index,E element):用指定元素替换集合中指定位置的元素,返回的是更新前的元素
String serE = list.set(3,"A");
System.out.println(serE);//a
System.out.println(list);//[a, b, 新加的元素, A]
//public E get(int index):返回集合中指定位置的元素
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 或者使用迭代器或增强for
}
}
ArrayList集合
List 接口的大小可变数组的实现。
注意,此实现不是同步的。(多线程,快!)
ArrayList增删慢,查询快
LinkedList集合
List 接口的链接列表实现。
LinkedList查询慢,增删快
里面包含了大量操作首尾元素的方法
注意:使用LinkedList集合特有的方法,不能使用多态
方法
public class Demo02LinkedList {
public static void main(String[] args) {
LinkedList<String> linked = new LinkedList<>();
linked.add("a");
linked.add("b");
linked.add("c");
// linked.addFirst("www");
// System.out.println(linked);//[www, a, b, c]
linked.push("www");
System.out.println(linked);//[www, a, b, c]
linked.addLast("com");
System.out.println(linked);//[www, a, b, c, com] 与add效果一样
System.out.println(linked.removeFirst());//www
System.out.println(linked);//[a, b, c, com]
System.out.println(linked.removeLast());//com
System.out.println(linked);//[a, b, c]
linked.pop();//相当于linked.removeFirst()
linked.clear();
if(!linked.isEmpty()){
System.out.println(linked.getFirst());
System.out.println(linked.getLast());
}
}
}
Vector集合
Vector 类可以实现可增长的对象数组。
与新 collection 实现不同,Vector 是同步的。 (被ArrayList取代)
Set接口
一个不包含重复元素的 collection。
没有索引,没有带索引的方法,也不能使用普通的for循环遍历
HashSet
HashSet常用方法
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/*
java.util.Set接口 extends Collection接口
Set接口的特点:
1. 不允许存储重复元素
2. 没有索引,也不能使用普通的for循环遍历
java.util.HashSet集合 implements Set接口
HashSet特点:
1. 不允许存储重复的元素
2. 没有索引,也不能使用普通的for循环遍历
3. 是一个无序的集合,存储元素和取出元素的顺序可能不一样
4. 底层是一个哈希表结构(查询的速度非常的快)
*/
public class Demo01Set {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(3);
set.add(2);
set.add(1);
//遍历,使用迭代器
Iterator<Integer> it = set.iterator();
while (it.hasNext()){
Integer n = it.next();
System.out.println(n);//1,2,3 不允许存储重复元素
}
// 使用增强for遍历
for(Integer n : set){
System.out.println(n);
}
}
}
哈希值
/*
哈希值:是一个十进制的整数,由系统随机给出(就是对象的地址值,是一个逻辑地址,是模拟出来得到的地址,不是数据实际存储的地址)
在Object类有一个方法,可以获取对象的哈希值
int hashCode() 返回该对象的哈希码值。
hashCode方法的源码
public native int hashCode(); native:代表该方法调用的是本地操作系统的方法
*/
public class Demo02HashCode {
public static void main(String[] args) {
// Person类继承了Object类,所以可以使用Object类的hashCode方法
Person p1 = new Person();
int h1 = p1.hashCode();
System.out.println(h1);//284720968
Person p2 = new Person();
int h2 = p2.hashCode();
System.out.println(h2);//189568618
/*
toString方法的源码
return getClass().getName() + "@" + Integer.toHexString(hashCode());
*/
System.out.println(p1);//demo02.Person@10f87f48
System.out.println(p2);//demo02.Person@b4c966a
/*
String类的哈希值
String类重写了Object类的hashcode方法
*/
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1.hashCode());//96354
System.out.println(s2.hashCode());//96354
System.out.println("重地".hashCode());//1179395
System.out.println("通话".hashCode());//1179395 巧合
}
}
HashSet集合存储数据的结构
HashSet集合存储元素不重复的原理
HashSet存储自定义类型元素
给HashSet中存放自定义类型元素时,需要重写对象中的hashCode和equals方法,建立自己的比较方式,才能保证HashSet集合中的对象唯一。
Person类
public class Person extends Object {
private String name;
private int age;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
public Person() {
}
public Person(String name, int age) {
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;
}
}
main方法
import java.util.HashSet;
/*
set集合保证元素唯一
存储的元素(String,Integer,...,Student,...),必须重写 hashcode方法和equals方法
要求:
年龄和姓名一样视为同一个人
*/
public class Demo03HashSetSavePerson {
public static void main(String[] args) {
HashSet<Person> set = new HashSet<>();
Person p1 = new Person("易烊千玺",19);
Person p2 = new Person("易烊千玺",19);
Person p3 = new Person("王俊凯",20);
System.out.println(p1==p2);//false
System.out.println(p1.equals(p2));//true
set.add(p1);
set.add(p2);
set.add(p3);
System.out.println(set);//[demo02.Person@d2f51d66, demo02.Person@35bb51a5]
}
}
LinkedHashSet
具有可预知迭代顺序的 Set 接口的哈希表和链接列表实现。
/*
java.util.LinkedHashSet集合 extends HashSet集合
LinkedHashSet集合特点:
底层是一个哈希表(数组+链表/红黑树)+链表,多了一条链表(记录元素的存储顺序),保证元素有序
*/
public class Demo04LinkedHashSet {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("www");
set.add("abc");
set.add("abc");
set.add("com");
System.out.println(set);//[com, abc, www] 无序,不允许重复
LinkedHashSet<String> linked = new LinkedHashSet<>();
linked.add("www");
linked.add("abc");
linked.add("abc");
linked.add("com");
System.out.println(linked);//[www, abc, com] 有序,不允许重复
}
}
可变参数
/*
可变参数:时JDK1.5之后出现的新特性
使用前提:
当方法的参数列表数据类型已经确定,但参数的个数不确定,就可以使用可变参数
使用格式:定义方法时使用
修饰符 返回值类型 方法名(数据类型...变量名){}
可变参数的原理:
可变参数底层就是一个数组,根据传递参数不同,会创建不同长度的数组,来存储这些参数
传递的参数个数,可以是0个(不传递)、1、2、...多个
可变参数的注意事项:
1. 一个方法的参数列表,只能有一个可变参数
2. 如果方法的参数有多个,那么可变参数必须写在参数列表的末尾
*/
public class VarArgs {
public static void main(String[] args) {
int i = add("abc",1,10,12);
System.out.println("和为" + i);//22
}
/*
可变参数的终极写法
public static void method(Object...obj){}
*/
// 计算多个整数的和i
// add();就会创建一个长度为0的数组,new int[0]
public static int add(String b,int c,int... arr) {
System.out.println(arr);//[I@723279cf底层是一个数组
System.out.println(arr.length);//2
int sum = 0;
for (Integer i : arr) {
sum += i;
}
return sum;
}
}
Collections集合工具类
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/*
java.util.Collections是集合工具类,用来对集合进行操作,部分方法如下:
public static <T> boolean addAll(Collection<T> c,T... elements):往集合中添加一些元素
public static void shuffle(list<?> list) 打乱顺序
public static <T> void sort(list<T> list) 将集合中的元素按照默认顺序排序
注意:
sort(list<T> list)使用前提:
被排序的集合里面存储的元素,必须实现Comparable,重写接口中的方法CompareTo定义排序规则
Comparable接口的排序规则:
自己(this)-参数:升序
参数-自己(this):降序
public static <T> void sort(list<T> list,Comparator<? super T>) 将集合中的元素按照指定顺序排序
Comparator和Comparable的区别
Comparable:自己(this)和别人(参数)比较,自己需要实现Comparable接口,重写比较的规则conpareTo方法
Comparator:相当于找一个第三方的裁判,来比较两个
Comparable排序规则:
o1-o2:升序
o2-o1:降序
*/
public class Demo01Collections {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"a","b","c","d");
System.out.println(list);//[a, b, c, d]
Collections.shuffle(list);
System.out.println(list);//顺序变了,每次都不一样
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(1);
list2.add(3);
list2.add(2);
Collections.sort(list2);//默认升序
System.out.println(list2);//[1, 2, 3]
ArrayList<Person> list3 = new ArrayList<>();
list3.add(new Person("张三",18));
list3.add(new Person("李四",20));
list3.add(new Person("王五",15));
list3.add(new Person("王五",18));
System.out.println(list3);//[Person{name='张三', age=18}, Person{name='李四', age=20}, Person{name='王五', age=15}]
Collections.sort(list3);
System.out.println(list3);//[Person{name='王五', age=15}, Person{name='张三', age=18}, Person{name='李四', age=20}]
Collections.sort(list2, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;//降序
}
});
System.out.println(list2);//[3, 2, 1]
Collections.sort(list3, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
int result = o1.getAge()-o2.getAge();// 按照年龄升序排序
// 如果两人年龄相同,再用姓名首字母比较
if(result == 0){
result = o1.getName().charAt(0)-o2.getName().charAt(0);
}
return result;
}
});
System.out.println(list3);//[Person{name='王五', age=15}, Person{name='张三', age=18}, Person{name='王五', age=18}, Person{name='李四', age=20}]
}
}
Map集合
双列集合
Collection 与 Map 的对比
Map常用子类
/*
java.util.Map<K,V>集合
Map集合的特点:
1. Map集合是一个双列集合,一个元素包含两个值(一个key,一个value)
2. Map集合中的元素,key和value的数据类型可以相同,也可以不同
3. Map集合中的元素,key是不允许重复的,value是可以重复的
4. Map集合中的元素,key和value是一一对应
java.util.HashMap<K,V>集合 implements Map<K,V>接口
HashMap集合的特点:
1.底层是哈希表,查询速度特别的快
2.是一个无需的集合,存储元素和取出元素的顺序可能不一致
java.util.LinkedHashMap<K,V>集合 extends HashMap<K,V>集合
LinkedHashMap的特点
1. LinkedHashMap底层是哈希表+链表
2. LinkedHashMap集合是一个有序的集合,存储元素和取出元素的顺序是一致的
常用方法:
public V put(K key, V value) 将指定的值与此映射中的指定键关联(可选操作)。
public V get(Object key) 返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。
*/
public class Demo01Map {
public static void main(String[] args) {
show01();
show02();
}
/*
public V remove(Object key) 如果存在一个键的映射关系,则将其从此映射中移除(可选操作)。
返回值:V
key存在,v返回被删除的值
key不存在,v返回null
public V get(Object key) 返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。
public boolean containsKey(Object key) 如果此映射包含指定键的映射关系,则返回 true。
*/
private static void show02() {
Map<String,Integer> map = new HashMap<>();
map.put("赵丽颖",168);
map.put("杨颖",165);
map.put("林志玲",178);
System.out.println(map);//{林志玲=178, 赵丽颖=168, 杨颖=165}
System.out.println(map.remove("林志玲"));//178
System.out.println(map.remove("林志玲"));//null
System.out.println(map.get("杨颖"));//165
System.out.println(map.get("林志玲"));//null
System.out.println(map.containsKey("杨颖"));//true
System.out.println(map.containsKey("林志玲"));//false
}
/*
public V put(K key, V value) 将指定的值与此映射中的指定键关联(可选操作)。
返回值V:
存储键值对的时候,key不重复,返回值V是null
存储键值对的时候,key重复,会使用新的value替换map中重复的value,返回被替换的value值
*/
private static void show01() {
Map<String,String> map = new HashMap<>();
String v1 = map.put("鹿晗","关晓彤1");
System.out.println("v1:"+v1);//v1:null
String v2 = map.put("鹿晗","关晓彤2");//新值替换旧值
System.out.println("v2:"+v2);//v2:关晓彤1
System.out.println(map);//{鹿晗=关晓彤2}
map.put("小强","小红");
map.put("小刘","晓燕");
map.put("小强","小丽");
System.out.println(map);//{小强=小丽, 小刘=晓燕, 鹿晗=关晓彤2}
}
}
Map集合的遍历
两种方法
/*
Map集合的第一种遍历方式:通过键找值的方式
Map集合种的方法:
Set<K> keySet() 返回此映射中包含的键的 Set 视图。
实现步骤:
1. 使用Map集合种的方法keySet(),把Map集合所有的key取出来,存储到一个Set集合中
2. 遍历Set集合,获取Map集合中的每一个key
3. 通过Map集合中的方法get(key),通过key找到value
Map集合的第二种遍历方式:使用Entry进行遍历
*/
public class Demo02KeySet {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("赵丽颖",168);
map.put("杨颖",165);
map.put("林志玲",178);
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()){
String key = it.next();
Integer value = map.get(key);
System.out.println(key+"="+value);
}
Set<Map.Entry<String,Integer>> set2 = map.entrySet();
Iterator<Map.Entry<String,Integer>> it2 = set2.iterator();
while(it2.hasNext()){
Map.Entry<String,Integer> entry = it2.next();
String key2 = entry.getKey();
Integer value2 = map.get(key2);
System.out.println(key2+"="+value2);
}
}
}
例题:计算一个字符串中每一个字符出现的次数
import java.util.HashMap;
import java.util.Scanner;
public class Demo01MapTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
HashMap<Character,Integer> map = new HashMap<>();
for(char c:str.toCharArray()){
if(map.containsKey(c)){
Integer value = map.get(c);
value++;
map.put(c,value);
}else{
map.put(c,1);
}
}
for(Character key:map.keySet()){
Integer value = map.get(key);
System.out.println(key+"="+value);
}
}
}
Debug
斗地主案例(有序版本,双列)
案例介绍
需求分析
这里面map顺序写错了
实现
public class DouDiZhu {
public static void main(String[] args) {
// 准备一个map集合,存储牌的索引和组装好的牌
HashMap<Integer, String> poker = new HashMap<Integer, String>();
// 创建一个index集合,存储牌的索引
ArrayList<Integer> index = new ArrayList<>();
// 定义两个集合,存储花色和牌的序号
List<String> colors = List.of("♠", "♥", "♣", "♦");
List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
// 将大王小王存入牌的集合中
poker.put(0, "大王");
poker.put(1, "小王");
index.add(0);
index.add(1);
int n = 2;
for (int i = 0; i < numbers.size(); i++) {
for (int i1 = 0; i1 < colors.size(); i1++) {
poker.put(n, colors.get(i1) + numbers.get(i));
index.add(n);
n++;
}
}
// 洗牌
Collections.shuffle(index);
// 定义四个集合,存储玩家牌的索引和底牌的索引
ArrayList<Integer> player01 = new ArrayList<>();
ArrayList<Integer> player02 = new ArrayList<>();
ArrayList<Integer> player03 = new ArrayList<>();
ArrayList<Integer> dipai = new ArrayList<>();
// 发牌
for (int i = 0; i < index.size(); i++) {
Integer in = index.get(i);
if (i >= 51) {
dipai.add(in);
} else if (i % 3 == 0) {
player01.add(in);
} else if (i % 3 == 1) {
player02.add(in);
} else if (i % 3 == 2) {
player03.add(in);
}
}
// 排序,sort默认升序排序
Collections.sort(player01);
Collections.sort(player02);
Collections.sort(player03);
Collections.sort(dipai);
// 看牌
lookPoker("刘德华",poker,player01);
lookPoker("周润发",poker,player02);
lookPoker("周星驰",poker,player03);
lookPoker("底牌",poker,dipai);
}
/*
定义一个看牌方法,提高代码的复用性
参数:
String name :玩家名称
HashMap<Integer, String> poker
ArrayList<Integer> list:存储玩家和底牌的list集合
查表法:
遍历玩家或底牌集合,获取牌的索引,使用牌的索引,去Map集合中,找到对应的牌
*/
public static void lookPoker(String name,HashMap<Integer, String> poker,ArrayList<Integer> list){
// 输出玩家名称,不换行
System.out.print(name+":");
// 遍历玩家或底牌集合,获取牌的索引
for (Integer key : list) {
String value = poker.get(key);
System.out.print(value+" ");
}
System.out.println();//打印完每个玩家的牌,换行
}
}