20240325_CollectionPractise
参考来源:黑马程序员
Collection类
package com.Basic.Collection;
import java.util.*;
import java.util.function.Consumer;
/*
Collection分为两种:List和Set
List:添加的元素是有序的、可重复的、有索引。ArrayList、LinkedList
Set:元素不重复、无索引。
HashSet:无序,不重复,无索引
LinkedHashSet:有序,不重复,无索引
TreeSet:按照大小默认升序排序,不重复,无索引
*/
public class CollectionPractise {
public static void main(String[] args) {
Collection<String> list = new ArrayList<>();
//增加元素
list.add("1");
list.add("1");
list.add("2");
list.add("3");
System.out.println(list);
Collection<String> c = new HashSet<>();
c.add("1");
c.add("1");
c.add("2");
c.add("3");
System.out.println(c);//[1, 2, 3]
System.out.println("==============");
Collection<String> c1 = new ArrayList<>();
//添加
c1.add("1");
c1.add("1");
c1.add("2");
c1.add("3");
System.out.println(c1);
System.out.println("==============");
//清空
// c1.clear();
// System.out.println(c1);
//isEmpty
System.out.println(c1.isEmpty());
System.out.println("==============");
//获取大小
System.out.println(c1.size());
System.out.println("==============");
//是否包含
System.out.println(c1.contains("1"));
System.out.println("==============");
//把集合转换为数组
System.out.println(Arrays.toString(c1.toArray()));
System.out.println("==============");
//将一个集合的数据倒入另一个集合
ArrayList<String> c2 = new ArrayList<>();
c2.add("666");
c2.add("999");
c1.addAll(c2);
System.out.println(c1);//[1, 1, 2, 3, 666, 999]
System.out.println(c2);//[666, 999]
System.out.println("==============");
//迭代器Iterator:
Iterator<String> it = c1.iterator();
System.out.println(it.next());//有几个取几次,取完一个指向下一个
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
System.out.println("==============");
//使用循环加迭代器:
Iterator<String> it2 = c1.iterator();
while(it2.hasNext()){//hasNext:判断当前位置有无数据
String ele = it2.next();
System.out.println(ele);
}
System.out.println("==============");
//增强for循环:
for (String s : c1) {
System.out.println(s);
}
System.out.println("==============");
//用lambda表达式遍历
c1.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
//简化
c1.forEach((String s) -> {
System.out.println(s);
});
//简化
c1.forEach(System.out::println);
}
}
List类
package com.Basic.Collection;
import java.util.ArrayList;
import java.util.List;
public class ListPractise {
public static void main(String[] args) {
//创建一个List对象:
List<String> list = new ArrayList<>();
//add
list.add("1");
list.add("2");
list.add("2");
list.add("3");
System.out.println(list);
System.out.println("==============");
//往某一位置添加数据
list.add(1,"hui");
System.out.println(list);
System.out.println("===========");
//删除索引处数据.并返回被删除数据
System.out.println(list.remove(4));
System.out.println(list);
System.out.println("===========");
//获取某一位置数据
System.out.println(list.get(0));
System.out.println("===========");
//修改索引处数据
list.set(0,"6");
System.out.println(list);
}
}
Set类
package com.Basic.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
public class SetPractise {
public static void main(String[] args) {
// Set<Integer> set = new HashSet<>();//[5, 6, 7, 8]无序,不重复,无索引
// Set<Integer> set = new LinkedHashSet<>();//[6, 5, 8, 7]有序,不重复,无索引
Set<Integer> set = new TreeSet<>();//[5, 6, 7, 8]按大小排序,不重复,无索引
set.add(6);
set.add(5);
set.add(5);
set.add(8);
set.add(8);
set.add(7);
System.out.println(set);
//Set的方法几乎就是Collection的方法
}
}
Linkedlist类
package com.Basic.Collection;
import java.util.LinkedList;
public class LinkedListPractise {
public static void main(String[] args) {
LinkedList<String> queue = new LinkedList<>();//创建一个队列,先入先出
//从后加,入队
queue.addLast("no1");
queue.addLast("no2");
queue.addLast("no3");
queue.addLast("no4");
System.out.println(queue);
System.out.println("===========");
//从前删,出队
System.out.println(queue.removeFirst());
System.out.println(queue.removeFirst());
System.out.println(queue.removeFirst());
System.out.println(queue);
System.out.println("===========");
LinkedList<String> stack = new LinkedList<>();//创建一个栈,先入后出
// stack.addFirst("no1");
// stack.addFirst("no2");
// stack.addFirst("no3");
// stack.addFirst("no4");
// System.out.println(stack);
// System.out.println("===========");
stack.push("no1");//.push = .addFirst
stack.push("no2");
stack.push("no3");
stack.push("no4");
System.out.println(stack);
System.out.println("===========");
// stack.removeFirst();
// stack.removeFirst();
// stack.removeFirst();
// System.out.println(stack);
// System.out.println("===========");
stack.pop();
stack.pop();
stack.pop();//.pop = .removeFirst
System.out.println(stack);
System.out.println("===========");
}
}
TreeSet类
package com.Basic.Collection;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetPractise {//可排序,无重复,无索引,底层基于红黑树实现
public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();//[5, 6, 7, 8]按大小排序,不重复,无索引
set.add(6);
set.add(5);
set.add(5);
set.add(8);
set.add(8);
set.add(7);
System.out.println(set);
System.out.println("================");
Set<Student> stu = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//需求:身高
return Double.compare(o1.getHeight(), o2.getHeight());
}
});
stu.add(new Student(3, "K", 180));
stu.add(new Student(4, "H", 180.1));
stu.add(new Student(3, "h", 179));
System.out.println(stu);//报错,因为无法排序,当两种排序规则存在时就近原则
}
}
测试TreeSet类的Student类
package com.Basic.Collection;
public class Student implements Comparable<Student>{
private int age;
private String name;
private double height;
public Student(){
}
public Student(int age, String name, double height) {
this.age = age;
this.name = name;
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", height=" + height +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public int compareTo(Student o) {//重写Comparable接口的方法
//按年龄
return this.age - o.age;
}
}