集合Collection接口下Set接口List接口

时间:2020-12-03 17:57:53

集合 : 装载数据的容器
Collection 是一个接口,我常用的集合类实现了这个接口
任何需要装载数据的类实现这个接口即可添加和删除数据
interface 也可以使用extends继承,需要实现全部的方法
collection 接口有两个子接口,分别是List和Set
List接口有两个常用的类 ,ArrayList和LinkedList

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
// ArrayList LinkedList
public class CollectionTest {

public static void main(String[] args) {

// 集合 : 装载数据的容器

// Collection 是一个接口,我常用的集合类实现了这个接口
// 任何需要装载数据的类实现这个接口即可添加和删除数据

// interface 也可以使用extends继承,需要实现全部的方法

// collection 接口有两个子接口,分别是List和Set

// List接口有两个常用的类 ,ArrayList和LinkedList
// 类型不限 长度不限制
ArrayList arrayList = new ArrayList();

arrayList.add("Abc");
arrayList.add(123);

// 使用索引获取元素
Object o1 = arrayList.get(0);
Object o2 = arrayList.get(1);

String text1 = (String)o1;
System.out.println("内容是:"+ o1 + o2);

// 使用泛型,解决取数据后的强制转换
// 限定ArrayList中能够存储的数据类型
// ArrayList<类型>
ArrayList<String> arrayList2 = new ArrayList<String>();

arrayList2.add("abc");
// arrayList2.add(123); 非string 类型的数据就存不进去

String text2 = arrayList2.get(0);

// 实际工作中都是使用泛型来限定集合的存储数据类型

// collection 接口中的方法

// 是否为空 里边能否存储数据
boolean flag = arrayList2.isEmpty();

System.out.println("是否为空:" + flag);

//增加
arrayList2.add("text");

// 是否包含某个元素
boolean isContain = arrayList2.contains("Text");
System.out.println("是否包含text:" + isContain);

// 删除指定索引的元素
arrayList2.remove(0);

// 删除指定的元素
arrayList2.remove("abc");

// 已经存储的元素长度的个数
int size = arrayList2.size();
System.out.println("长度为:" + size);

// 根据索引获取元素,get不是collection接口中定义的方法
// 索引不要越界
arrayList2.get(0);

// 获取指定元素的索引 如果不存在返回-1
int index = arrayList.indexOf("abc");

// 遍历和普通数组一样,两种方式
for (int i = 0; i < arrayList2.size(); i++) {

String text = arrayList2.get(i);
System.out.println(text);
}

for (String string : arrayList2) {
System.out.println(string);
}


// LinkedList使用和ArrayList完全一模一样
LinkedList<String> linkedList = new LinkedList<String>();

linkedList.add("string");
String text3 = linkedList.get(0);

// 基本用法都相同

// 区别:
// 1. ArrayList 获取数据的时候比较快
// 2. LinkedList 增加删除数据的时候会比较快
// ArrayList 使用数组实现的 所以获取数据比较方便
// 增加删除比较慢 增加删除需要重新创建一个数组再复制一遍
// LinkedList 使用链表实现的,所以增加和删除比较快
// 查找元素相对较慢,每次都要从头开始搜索

// 数据量较小时,两者差距不大,通常都是使用ArrayList
}
}

List 和 Set 都可以存储多个元素
1.List中的元素可以重复,Set中的元素不可以重复
2.List有顺序(索引) Set 无顺序(索引)
Set中常用的是HashSet
经常使用Set获取多个数组中不重复的内容

import java.util.ArrayList;
import java.util.HashSet;

public class SetTest {

public static void main(String[] args) {

HashSet<String> hs1 = new HashSet<String>();
hs1.add("张三");
hs1.add("李四");
hs1.add("王五");
hs1.add("赵六");

// 先添加的不一定先输出,没有索引
// HashSet没有添加顺序,里边的内容按照hashCode进行排序
// hashCode是对象的一个唯一编号,根据规则自动生成
// 相同的元素只会存一份
for (String string : hs1) {

System.out.println(string);
}
System.out.println("-----------");
// 没有get方法,不能根据位置获取对象
ArrayList<String> al1 = new ArrayList<String>();
al1.add("张三");
al1.add("李四");
al1.add("张三");
al1.add("张三");
al1.add("王五");
al1.add("赵六");

for (String string : al1) {
System.out.println(string);
}

}

}

hashSet和ArrayList的遍历使用

import java.util.ArrayList;
import java.util.HashSet;

public class SetTest2 {

public static void main(String[] args) {

// 兴趣爱好
ArrayList<String> al1 = new ArrayList<String>();

al1.add("打篮球");
al1.add("踢足球");
al1.add("打游戏");

ArrayList<String> al2 = new ArrayList<String>();

al2.add("看电影");
al2.add("打游戏");

// 统计所有的兴趣爱好有哪些,全部合并在一起,不保留重复的内容
HashSet<String> hs = new HashSet<>();

// 遍历数组,把每个元素都加入到HashSet中
// 遍历hs,输出里边的每一个元素

// 遍历数组,获取数组中的每一个元素,然后做操作,非常常用
for (String string : al1) {

hs.add(string); // 将数组中的每一个元素加入到HashSet中
}

// 一次性加入整个数组
hs.addAll(al1);
hs.addAll(al2);
System.out.println(hs); // HashSet自动排除所有重复的部分

// 获得共有的兴趣爱好,两个数组的交集
// 遍历al1
for (String s1 : al1) {
// s1同学1的每一个兴趣爱好
// 2.s1要同学2的每一个兴趣爱好进行比较
for (String s2 : al2) {
if (s1.equals(s2)) {
// 相等表示俩人有共同的兴趣爱好,记录在al3中
System.out.println("共有的兴趣爱好是:"+s1);
}
}
// 集合是否包含某个元素
// 同学2的兴趣爱好是否包含t1
if (al2.contains(s1)) {
System.out.println(s1);
}
}

}
}