当前位置:网站首页>集合框架知识
集合框架知识
2022-08-03 05:09:00 【*super】
一、集合
1.集合与数组的相同点和不同点
数组的长度是固定的,集合的长度可以变化
集合中存储的元素必须是引用类型数据
2.Collection
List有序的、可以重复的(ArrayList+LinkedList)
ArrayList:底层是数组--查询快,增删慢。
package demoji.one;
import java.util.ArrayList;
import java.util.List;
/**
* @Author:张金贺
* @Date:2022/7/5 20:49
* @Version 1.0
*/
public class demo01 {
public static void main(String[] args) {
List list =new ArrayList();
list.add("张三");
list.add("李四");//添加
list.add(0,"王五");
list.remove("张三");//删除
list.remove(1);//下标删除
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));//获取索引为i的人
}
System.out.println(list);
}
}
LinkedList:底层是双向链表--查询慢,增删快。
package demoji.one;
import java.util.LinkedList;
/**
* @Author:张金贺
* @Date:2022/7/5 20:58
* @Version 1.0
*/
public class demo02 {
public static void main(String[] args) {
LinkedList l =new LinkedList();
l.add("张三");
l.add(0,"李四");
l.addFirst("王五");//添加到最前面
l.addLast("马六");//添加到最后面
l.remove("王五");
System.out.println(l);
l.get(2);//获取元素
l.getFirst();
l.getLast();
}
}
Set无序的、不可重复的(HashSet+TreeSet)
HashSet接口
package demoji.one;
import java.util.HashSet;
/**
* @Author:张金贺
* @Date:2022/7/5 21:07
* @Version 1.0
*/
public class three {
public static void main(String[] args) {
HashSet hashSet=new HashSet();
hashSet.add("张三");
hashSet.add("李四");
hashSet.add("李四");//不会输出
hashSet.remove("张三");
System.out.println(hashSet);//乱序输出
System.out.println(hashSet.size());//获取长度
}
}
3.迭代器遍历集合Iterable
package demoji.one;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @Author:张金贺
* @Date:2022/7/5 21:10
* @Version 1.0
*/
public class four {
// 迭代器遍历集合
public static void main(String[] args) {
List lst =new ArrayList();
lst.add("张三");
lst.add("李四");
lst.add("王五");
Iterator iterator = lst.iterator();//获取迭代器
// iterator.hasNext();//判断下一个元素是否存在
while (iterator.hasNext()){
String str= (String) iterator.next();//获取下一个元素
System.out.println(str);
}
}
}
4.增强for循环
package demoji.one;
import java.util.HashSet;
/**
* @Author:张金贺
* @Date:2022/7/5 21:16
* @Version 1.0
*/
public class demo05 {
public static void main(String[] args) {
HashSet h =new HashSet();
h.add("张三");
h.add("李四");
h.add("王五");
for(Object o :h){
String str= (String) o;
System.out.println(str);
}
}
}
5.HashMap集合
public static void main(String[] args) { Map map = new HashMap(); map.put("杨过", "小龙女");//添加键值对 map. put("郭靖","黄蓉"); map.remove("郭靖"); }
通过键的方式来遍历map:
public static void main(String[] args) { Map map = new HashMap(); map.put("杨过", "小龙女");//添加键值对 map. put("郭靖","黄蓉"); Set keys = map.keySet();//获取集合中所有的键 Iterator it =keys. iterator();//获取到迭代器 while(it.hasNext()){ String key =(String)it.next();//获取到键 String value = (String)map. get(key);//根据键得到值 System.out.println("key :"+key+"value:"+value); } }
通过键值对的方式遍历map:
public static void main(String[] args) { Map map = new HashMap(); map.put("杨过", "小龙女");//添加键值对 map. put("郭靖","黄蓉"); Set set= map.entrySet();//获取map集合的键值对集合 Iterator it =set. iterator();//获取到迭代器 while(it.hasNext()){ Map.Entry entry =(Map.Entry)it.next();//获取到每一个键值对的对象 System.out.println(entry.getKey());//获取到健 System.out.println(entry.getValue());//获取到值 } }
HashMap存储自定义类型键值:
package demoji;
/**
* @Author:张金贺
* @Date:2022/7/5 21:36
* @Version 1.0
*/
public class Student {
private String name;
private Integer age;
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override//如果键一样,则视为同一个对象
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return name.equals(student.name) && age.equals(student.age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package demoji;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @Author:张金贺
* @Date:2022/7/5 21:37
* @Version 1.0
*/
public class demo1 {
public static void main(String[] args) {
Map map = new HashMap();
map.put(new Student("张三",23),"北京");
map.put(new Student("李四",25),"上海");
//获取键的集合
Set set1 = map.keySet();
for (Object o : set1) {
Student stu =(Student) o;
String value =(String) map.get(stu);
System.out.println(stu);
System.out.println(value);
}
//获取键值对的集合遍历
Set set2 = map. entrySet() ;
for (Object o : set2){
Map.Entry entry = (Map.Entry) o;
System.out.println ((Student)entry.getKey());
System.out.println(entry.getValue());
}
}
}
6.可变参数 (※)
7.Collections集合工具类
public static void main(String[] args) { List lst =new ArrayList(); lst.add("a"); lst.add("b"); lst.add("c"); Collections.sort(lst);//排序会按升序排列 Collections.shuffle(lst);//打乱顺序 System.out.println(lst); }
二、泛型
泛型集合
package demoji.fan;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @Author:张金贺
* @Date:2022/7/5 22:08
* @Version 1.0
*/
public class demo1 {
public static void main(String[] args) {
List<String> lst = new ArrayList<String>();
lst.add("abc");
// lst.add(123)报错
HashMap<String, String> map = new HashMap<String,String>();
map.put("张三","北京");
}
}
泛型类
package demoji;
/**
* @Author:张金贺
* @Date:2022/7/5 22:28
* @Version 1.0
*/
public class demo<T>{
public void show(T t){
System.out.println(t);
}
public T getResult(T t){
return t;
}
}
测试
package demoji;
/**
* @Author:张金贺
* @Date:2022/7/5 22:30
* @Version 1.0
*/
public class test {
public static void main(String[] args) {
demo<String> ge=new demo<String>();
ge.show("abc");
// ge.show(123);报错
System.out.println(ge.getResult("xyz"));
}
}
泛型方法
public class demo03 { public <T> void show(T t){ System.out.println(t); } public <S> void print(S s){ System.out.println(s); } }
泛型接口
public interface three<T> { public void show(T t); }
1.public class threeImpl implements three<String>{ @Override public void show(String s) { System.out.println(s); } }
2.public class threeImpl<T> implements three<T>{ @Override public void show(T t) { System.out.println(t); } }
泛型限定
所谓泛型限定,是指一个方法在接收所传参数引用数据类型时做的限定!
package demoji;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @Author:张金贺
* @Date:2022/7/5 22:43
* @Version 1.0
*/
public class test10 {
public static void main(String[] args) {
ArrayList<String> lst = new ArrayList<String>();
lst.add("abc");
lst.add("xyz");
ArrayList<Integer> lst1 = new ArrayList<Integer>();
lst1.add(1);
lst1.add(2);
show(lst);
// show(lst1);报错
}
// public static void show(ArrayList<?> arr){ 不报错
public static void show(ArrayList<String> arr){
Iterator it = arr.iterator ();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
泛型上限和下限()
<? super Student> <? extends Person>
三、案例
模拟斗地主洗牌发牌
按照斗地主的规则,完成洗牌发牌的动作。
具体规则:
1.组装54 张扑克牌
2.将54张牌顺序打乱
3.三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
4.查看三人各自手中的牌(按照牌的大小排序)、底牌
手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
package doudizhu;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
/**
* @Author:张金贺
* @Date:2022/7/5 23:05
* @Version 1.0
*/
public class lianxi {
public static void main(String[] args) {
ArrayList<String> color =new ArrayList<String>();
color.add("");
color.add("");
color.add("");
color.add("");
ArrayList<String> number=new ArrayList<String>();
Collections.addAll(number,"3","4","5","6","7","8","9","10","J","Q","K","A","2");
HashMap<Integer,String> map =new HashMap<Integer,String>();
int index=0;//编号
for (String s : color) {
for (String s1 : number) {
map.put(index,s+s1);//将牌放入map集合
index++;
}
}
map.put (index++,"小");
map.put (index++,"大");
Set set= map.keySet();
// 测试生成的扑克牌
// for (Object o : set) {
// System.out.println(map.get(o));
// }
ArrayList<Integer> cards =new ArrayList<Integer>();//存牌的编号
for (int i = 0; i <= 53; i++) {
cards.add(i);
}
Collections.shuffle(cards);
ArrayList<Integer> player1 =new ArrayList<Integer>();
ArrayList<Integer> player2 =new ArrayList<Integer>();
ArrayList<Integer> player3 =new ArrayList<Integer>();
ArrayList<Integer> dipai =new ArrayList<Integer>();
for (int i = 0; i <= 53; i++) {
if (i>=51){
dipai.add(cards.get(i));
}else if(i%3==1){
player1.add(cards.get(i));
}else if(i%3==2){
player2.add(cards.get(i));
}else {
player3.add(cards.get(i));
}
}
//排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(dipai);
ArrayList<String> p1 =new ArrayList<String>();
ArrayList<String> p2 =new ArrayList<String>();
ArrayList<String> p3 =new ArrayList<String>();
ArrayList<String> di =new ArrayList<String>();
for (Integer i : player1) {
p1.add(map.get(i));
}
for (Integer i : player2) {
p2.add(map.get(i));
}
for (Integer i : player3) {
p3.add(map.get(i));
}
for (Integer i : dipai) {
di.add(map.get(i));
}
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
System.out.println(di);
}
}
边栏推荐
- Kotlin-Flow common encapsulation class: the use of StateFlow
- 接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
- The problem that the rosbag tool plotjuggler cannot open rosbag
- JS bottom handwriting
- Build your own web page on raspberry pie (1)
- 【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
- Installation of Apache DolphinScheduler version 2.0.5 distributed cluster
- Shell条件语句判断
- 【Harmony OS】【ARK UI】ETS 上下文基本操作
- Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
猜你喜欢
VR全景展打造专属元宇宙观展空间
接口测试如何准备测试数据
接口测试实战| GET/POST 请求区别详解
接口和协议
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
Kotlin-Flow common encapsulation class: the use of StateFlow
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
Shell之条件语句
Alienware上线首个数字时装AR试穿体验
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
随机推荐
Interface testing framework of actual combat (2) | interface request assertion
Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
2022/08/02 学习笔记 (day22) 多线程
用户密码加密工具
Build your own web page on raspberry pie (1)
接口测试框架实战(二)| 接口请求断言
FileZilla 搭建ftp服务器
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
tag单调栈-单调栈预备知识-lt.739. 每日温度
rosbag工具plotjuggler无法打开rosbag的问题
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
Bubble sort in c language structure
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
【Harmony OS】【ARK UI】轻量级数据存储
【生物素叠氮化物|cas:908007-17-0】价格_厂家
3. 无重复字符的最长子串
CobalStrike(CS)基础超级详细版
Tag stack - stack monotonically preparatory knowledge - lt. 739. The daily temperature
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
Two ways to simulate multi-user login in Jmeter