当前位置:网站首页>集合框架知识
集合框架知识
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);
}
}
边栏推荐
- 社交电商如何做粉丝运营?云平台怎么选择商业模式?
- Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
- C# async and multithreading
- Interface test framework combat (1) | Requests and interface request construction
- 接口测试框架实战(四)| 搞定 Schema 断言
- User password encryption tool
- MCM箱模型建模方法及大气O3来源解析
- 1079 延迟的回文数 (20 分)
- Two ways to simulate multi-user login in Jmeter
- Exception (abnormal) and Error (error) difference analysis
猜你喜欢

接口测试框架实战(四)| 搞定 Schema 断言

Bubble sort in c language structure

【Harmony OS】【FAQ】Hongmeng Questions Collection 1

力扣561. 数组拆分

Concepts and Methods of Exploratory Testing

设计模式——组合模式、享元模式(Integer缓存)(结构型模式)

高可用 两地三中心

FileZilla 搭建ftp服务器

How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use

数字化时代,企业如何建立自身的云平台与商业模式的选择?
随机推荐
DFS's complement to pruning
Jmeter 模拟多用户登录的两种方法
技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
PotPlayer实现上班摸鱼电视自由
数字孪生园区场景中的坐标知识
力扣561. 数组拆分
IO进程线程->线程->day5
shell script loop statement
2022暑假牛客多校联赛第一场
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
Presto installation and deployment tutorial
在树莓派上搭建属于自己的网页(2)
Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)
The problem that the rosbag tool plotjuggler cannot open rosbag
Power button 561. An array of split
User password encryption tool
建立树形结构
js的垃圾回收机制
接口测试框架实战(一) | Requests 与接口请求构造
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8