Collection
它是集合框架中的根接口,它提供了对集合的基本操作
常用子接口:list 和set
list常用实现类ArrayList LinkedList
Set常用实现类 HashSet TreeSet LinkedHashSet
List、set和map的相关细节
list和set的区别
ArrayList和linkedList的区别
ArrayList底层数据结构是数组的一种list集合
常用方法:
1、add(e):在集合的末尾添加元素,并返回true
2、add(index,e):在指定位置上添加元素
3、remove(index):删除指定位置上的元素,返回被删除的元素
4、remove(object):删除第一次出现的元素对象,如果该对象不存在则删除失败返回false
5、set(index,e):将新值e替换指定位置上的原始数据,并返回被替换的原始值
6、get(index):通过下标获取指定元素
7、clear():清空集合
8、contains(object):判断集合中是否包含指定元素
9、isEmpty():判断集合内容是否为空
10、indexOf(object):根据指定元素返回第一次出现的下标,元素不存在返回-1
11、lastIndexOf(object):根据指定元素返回最后一次出现的下标,元素不存在返回-1
12、size():返回集合的元素个数
13、addAll(collection e):将e集合添加到集合的末尾
14、addAll(index,collection e):将e集合添加到集合的指定位置
15、containsAll(collection e):判断是否包含指定集合中的元素
16、removeAll(collection e):从原集合中删除两集合的交集
17、retainAll(collection e):将两集合的交集存到原集合中
18、equals(collection e):判断两集合的元素是否完全相同(顺序)
遍历:
for(int i = 0;i < list.size();i++){ // list.get(i); }
LinkedList
底层的数据结构是链表
适用场景:
LinkedList一般用于增删频繁的业务逻辑中,常会使用在池相关的技术中
ArrayList大多使用在查询频繁的业务中,一般“好友列表”,“商品列表”,“订单列表”都会使用ArrayList
//集合中嵌套数组
List list = new ArrayList();
String [] arr1= {"1","2","3"};
String [] arr2= {"a","d","e"};
String [] arr3= {"b","c","f"};
list.add(arr1);
list.add(arr2);
list.add(arr3);
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.get(i).length; j++) {
System.out.print(list.get(i)[j]);
}
System.out.println();
}
#Set
特点:无序(存和取的顺序不一致),不可重复
##迭代器
迭代的步骤:
注:
在迭代的过程中不能对集合进行增删改,会由于不确定性发生ConcurrentModificationException。
解决办法: 使用ListIterator
步骤:
1、通过list的litIterator()获取使用ListIterator对象
2、使用ListIterator的方法对集合进行改动
ListIterator的方法:
1、add(e)
2、remove()
3、set(e)
4、nextIndex():获取下一个可迭代元素的下标
5、previousIndex():获取上一个元素的下标
6、previous():获取上一个元素
List中迭代
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println(list);
// 通过集合的iterator()方法获取迭代器对象,并将迭代器与集合绑定
// 表示这个迭代器迭代的是这个集合
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
List list2 = new ArrayList<>();
list2.add("java");
list2.add("python");
list2.add("H5");
list2.add("c");
System.out.println(list2);
ListIterator it = list2.listIterator();
while (it.hasNext()) {
String n = it.next();
if (n.equals("python")) {
//set等同于下面2行代码
//it.remove();
//it.add("c++");
it.set("c++");
}
}
System.out.println(list2);
输出结果为
[java, python, H5, c]
[java, c++, H5, c]
Map接口中数据的存储方式与其他集合不同,它的数据是以键值对的形式进行存储的。
需要在创建Map集合时,明确Map中键(key)和值(value)的数据类型。
表示只有通过此键值对产生映射关系,才能确定一个确切的数据。
注:
1、键必须唯一
2、值可以重复
3、Map是无序的
实现类:
HashMap
TreeMap
LinkedHashMap
常用方法:
1、put(k,v):将k-v对存储到map中,并返回被覆盖的v
2、remove(k):通过k删除整个k-v对,并返回被删除的v
3、remove(k,v):只有键值对匹配时,才能删除,返回是否删除成功
4、replace(k,v):将v替换指定k上的旧值,并返回被替换的值
5、replace(k,oldValue,newValue):只有键和旧值匹配时,采用新值替换旧值,返回是否替换成功
6、get(k):通过k返回对应的v
7、isEmpty():判断map是否为空
8、clear():清空map集合
9、size():获取map集合长度
10、containsKey(k):判断是否包含指定的键
11、containsValue(v):判断是否包含指定的值
Map的迭代
Map的迭代:
Map不能使用普通循环和增强for循环
原因是:map不是数组,也不是iterable的实例,map不同于list,set,list、set都继承了collection,而collection继承了Iterable,所以list和set都是iterable的实例,不仅可以获取iterator对象,还能使用增强for循环。
两种方法:
KeySet():返回这个map集合中的键的set的集合
entrySet():
Map中迭代:
Map map = new HashMap();
map.put("A", 1);
map.put("C", 2);
map.put("F", 3);
map.put("D", 4);
map.put("B", 4);
// 通过keySet方法获取map集合中所有键组成的集合
Set set = map.keySet();
// 通过set的iterator方法获取set集合的迭代器对象
Iterator it = set.iterator();
// 循环判断并获取
while(it.hasNext()){
String key = it.next();
System.out.println(key +" = "+ map.get(key));
}
Map map = new HashMap();
map.put("王宝强", "马蓉");
map.put("李晨", "范冰冰");
map.put("文章", "马伊琍");
map.put("刘恺威", "杨幂");
// 通过map的entrySet方法获取map中所有键值对的映射关系所组成的set集合
Set> set = map.entrySet();
// 通过set的iterator方法获取迭代器对象
Iterator> it = set.iterator();
// 循环判断并获取
while(it.hasNext()){
Map.Entry en = it.next();
String key = en.getKey();
String value = en.getValue();
System.out.println(key +" - "+ value);
}
1、打乱 shuffle()
2、交换 swap()
3、倒置 reverse()
4、填充 fill()
5、拷贝 copy()
6、排序 sort()
集合对象增删改查
package 集合对象增删改查8_4;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Demo {
public static Scanner input = new Scanner(System.in);
public static List students = new ArrayList();
public static void main(String[] args) {
while (true) {
System.out.println("请按照规则输入操作,1.增 2.删 3.改 4.查");
int num = input.nextInt();
switch (num) {
case 1:
addStudent();
break;
case 2:
delect();
break;
case 3:
updateStudent();
break;
case 4:
showStudent();
break;
case 5:
System.out.println("Bye!");
return;
}
}
}
public static void addStudent() {
System.out.println("输入学生的ID");
String id = input.next();
System.out.println("输入学生的姓名");
String name = input.next();
System.out.println("输入学生的年龄");
int age = input.nextInt();
Student student = new Student(id, name, age);
students.add(student);
}
public static void delect() {
System.out.println("请根据学号进行删除信息");
System.out.println("请输入你要删除的信息");
String idDelete = input.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(idDelete)) {
students.remove(i);
i--;
System.out.println("删除成功");
break;
}else {
System.out.println("输入信息有误,请三思");
break;
}
}
}
public static void updateStudent() {
System.out.println("根据学号,修改信息()");
System.out.println("请输入学号:");
String idUpdate = input.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(idUpdate)) {
System.out.println("请输入修改信息序号,1.姓名 2.年龄");
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("输入新姓名:");
String newName= input.next();
students.get(i).setName(newName);
break;
case 2:
System.out.println("输入新年龄:");
int newAge= input.nextInt();
students.get(i).setAge(newAge);
break;
}
}
}
}
public static void showStudent() {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
System.out.println("学号" + student.getId() + "姓名:" + student.getName() + "年龄:" + student.getAge());
}
}
}
class Student {
private String id;
private String name;
private int age;
public Student() {
}
public Student(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}