Day17
一,迭代器
1,迭代器的原理实现
1.1,使用Iteractor遍历,及删除元素
import java.util.ArrayList;
public class Test01 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
for (String element : list) {
System.out.println(element);
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
public class Test02 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
Iterator<String> it = list.iterator();
while(it.hasNext()){
String element = it.next();
System.out.println(element);
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
public class Test03 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
Iterator<String> it = list.iterator();
while(it.hasNext()){
String element = it.next();
if(element.equals("椎名空")){
it.remove();
}
}
for (String element : list) {
System.out.println(element);
}
}
}
1.2,使用ListIteractor遍历元素
import java.util.ArrayList;
import java.util.ListIterator;
public class Test04 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
ListIterator<String> listIterator = list.listIterator();
while(listIterator.hasNext()){
String element = listIterator.next();
System.out.println(element);
}
}
}
1.3,使用ListIteractor指定下标开始遍历元素
import java.util.ArrayList;
import java.util.ListIterator;
public class Test05 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
ListIterator<String> listIterator = list.listIterator(2);
while(listIterator.hasNext()){
String element = listIterator.next();
System.out.println(element);
}
}
}
1.4,使用ListIteractor倒序遍历元素
import java.util.ArrayList;
import java.util.ListIterator;
public class Test06 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
ListIterator<String> listIterator = list.listIterator(list.size());
while(listIterator.hasPrevious()){
String element = listIterator.previous();
System.out.println(element);
}
}
}
1.5,使用ListIteractor遍历时删除元素
import java.util.ArrayList;
import java.util.ListIterator;
public class Test07 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
ListIterator<String> listIterator = list.listIterator();
while(listIterator.hasNext()){
String element = listIterator.next();
if(element.equals("椎名空")){
listIterator.remove();
}
}
for (String element : list) {
System.out.println(element);
}
}
}
1.6 使用ListIteractor遍历时修改元素
import java.util.ArrayList;
import java.util.ListIterator;
public class Test08 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
ListIterator<String> listIterator = list.listIterator();
while(listIterator.hasNext()){
String element = listIterator.next();
if(element.equals("椎名空")){
listIterator.set("周隽乐");
}
}
for (String element : list) {
System.out.println(element);
}
}
}
1.7, 使用ListIteractor遍历时添加元素
import java.util.ArrayList;
import java.util.ListIterator;
public class Test09 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
ListIterator<String> listIterator = list.listIterator();
while(listIterator.hasNext()){
String element = listIterator.next();
if(element.equals("椎名空")){
listIterator.add("周隽乐");
}
}
for (String element : list) {
System.out.println(element);
}
}
}
二,LinkedList集合类
1,LinkedList的使用
import java.util.LinkedList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Test01 {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
list.add("樱井步");
list.add("周隽乐");
list.add(5, "严荐翔");
LinkedList<String> newList1 = new LinkedList<>();
Collections.addAll(newList1, "aaa","bbb","ccc","ccc");
list.addAll(newList1);
LinkedList<String> newList2 = new LinkedList<>();
Collections.addAll(newList2, "xxx","yyy","zzz","zzz");
list.addAll(5,newList2);
list.set(2, "罗仁君");
String string = list.get(2);
System.out.println("获取指定下标上的元素:" + string);
int size = list.size();
System.out.println("获取元素个数:" + size);
System.out.println("判断集合中是否包含某个元素:" + list.contains("严荐翔"));
System.out.println("判断集合中是否包含某个子集合:" + list.containsAll(newList1));
System.out.println("获取元素第一次出现的下标:" + list.indexOf("ccc"));
System.out.println("获取元素最后一次出现的下标:" + list.lastIndexOf("ccc"));
System.out.println("判断集合是否没有元素:" + list.isEmpty());
list.remove(4);
list.remove("朝桐光");
list.removeAll(newList2);
List<String> subList = list.subList(2, 8);
Object[] objArr = subList.toArray();
System.out.println(Arrays.toString(objArr));
String[] strArr = new String[subList.size()];
subList.toArray(strArr);
System.out.println(Arrays.toString(strArr));
list.retainAll(newList1);
System.out.println("------------------");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("------------------");
for (String element : list) {
System.out.println(element);
}
System.out.println("------------------");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
String element = iterator.next();
System.out.println(element);
}
System.out.println("------------------");
ListIterator<String> listIterator = list.listIterator();
while(listIterator.hasNext()){
String element = listIterator.next();
System.out.println(element);
}
}
}
2,LinkedList独有的方法
import java.util.LinkedList;
public class Test02 {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("严荐翔1");
list.add("严荐翔2");
list.add("严荐翔3");
list.add("严荐翔4");
list.add("严荐翔5");
list.add("严荐翔6");
list.add("严荐翔7");
list.add("严荐翔1");
list.add("严荐翔2");
list.add("严荐翔3");
list.add("严荐翔4");
list.add("严荐翔5");
list.add("严荐翔6");
list.add("严荐翔7");
list.addFirst("aaa");
list.offerFirst("bbb");
list.push("ccc");
list.addLast("xxx");
list.offerLast("yyy");
list.offer("zzz");
System.out.println("获取第一个元素:" + list.element());
System.out.println("获取第一个元素:" + list.getFirst());
System.out.println("获取第一个元素:" + list.peek());
System.out.println("获取第一个元素:" + list.peekFirst());
System.out.println("获取第一个元素:" + list.pop());
System.out.println("获取最后一个元素:" + list.getLast());
System.out.println("获取最后一个元素:" + list.peekLast());
list.poll();
list.pollFirst();
list.removeFirst();
list.pollLast();
list.removeLast();
list.removeFirstOccurrence("严荐翔2");
list.removeLastOccurrence("严荐翔7");
for (String element : list) {
System.out.println(element);
}
}
}
3,LinkedList – 队列模式(先进先出)
import java.util.LinkedList;
public class Test03 {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("eee");
while(!list.isEmpty()){
String element = list.removeFirst();
System.out.println(element);
}
System.out.println("元素的个数:" + list.size());
}
}
4,LinkedList – 栈模式(先进后出)
import java.util.LinkedList;
public class Test04 {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("eee");
while(!list.isEmpty()){
String element = list.removeLast();
System.out.println(element);
}
System.out.println("元素的个数:" + list.size());
}
}
三,Vector集合类
1,Vector的使用
import java.util.Vector;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Test01 {
public static void main(String[] args) {
Vector<String> list = new Vector<>();
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
list.add("樱井步");
list.add("周隽乐");
list.add(5, "严荐翔");
Vector<String> newList1 = new Vector<>();
Collections.addAll(newList1, "aaa","bbb","ccc","ccc");
list.addAll(newList1);
Vector<String> newList2 = new Vector<>();
Collections.addAll(newList2, "xxx","yyy","zzz","zzz");
list.addAll(5,newList2);
list.set(2, "罗仁君");
String string = list.get(2);
System.out.println("获取指定下标上的元素:" + string);
int size = list.size();
System.out.println("获取元素个数:" + size);
System.out.println("判断集合中是否包含某个元素:" + list.contains("严荐翔"));
System.out.println("判断集合中是否包含某个子集合:" + list.containsAll(newList1));
System.out.println("获取元素第一次出现的下标:" + list.indexOf("ccc"));
System.out.println("获取元素最后一次出现的下标:" + list.lastIndexOf("ccc"));
System.out.println("判断集合是否没有元素:" + list.isEmpty());
list.remove(4);
list.remove("朝桐光");
list.removeAll(newList2);
List<String> subList = list.subList(2, 8);
Object[] objArr = subList.toArray();
System.out.println(Arrays.toString(objArr));
String[] strArr = new String[subList.size()];
subList.toArray(strArr);
System.out.println(Arrays.toString(strArr));
list.retainAll(newList1);
System.out.println("------------------");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("------------------");
for (String element : list) {
System.out.println(element);
}
System.out.println("------------------");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
String element = iterator.next();
System.out.println(element);
}
System.out.println("------------------");
ListIterator<String> listIterator = list.listIterator();
while(listIterator.hasNext()){
String element = listIterator.next();
System.out.println(element);
}
}
}
2,Vector的发展历史
import java.util.Enumeration;
import java.util.Vector;
public class Test02 {
public static void main(String[] args) {
Vector<String> v = new Vector<>();
v.addElement("aaa");
v.addElement("bbb");
v.addElement("ccc");
v.addElement("ddd");
v.addElement("eee");
v.addElement("fff");
v.removeElement("bbb");
v.removeElementAt(0);
Enumeration<String> elements = v.elements();
while(elements.hasMoreElements()){
String element = elements.nextElement();
System.out.println(element);
}
}
}
3,Stack
import java.util.Stack;
public class Test01 {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("aaa");
stack.push("bbb");
stack.push("ccc");
stack.push("ddd");
int search = stack.search("bbb");
System.out.println("查询出元素距离栈顶的位置:" + search);
String peek = stack.peek();
System.out.println("获取栈顶元素:" + peek);
while(!stack.empty()){
String pop = stack.pop();
System.out.println(pop);
}
}
}
四,HashSet集合类
1,HashSet的使用
import java.util.HashSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
public class Test01 {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("麻生希");
set.add("椎名空");
set.add("水菜丽");
set.add("朝桐光");
set.add("樱井步");
set.add("周隽乐");
HashSet<String> newSet1 = new HashSet<>();
Collections.addAll(newSet1, "aaa","bbb","ccc","ccc");
set.addAll(newSet1);
int size = set.size();
System.out.println("获取元素个数:" + size);
System.out.println("判断集合中是否包含某个元素:" + set.contains("严荐翔"));
System.out.println("判断集合中是否包含某个子集合:" + set.containsAll(newSet1));
System.out.println("判断集合是否没有元素:" + set.isEmpty());
set.remove("朝桐光");
set.removeAll(newSet1);
Object[] objArr = set.toArray();
System.out.println(Arrays.toString(objArr));
String[] strArr = new String[set.size()];
set.toArray(strArr);
System.out.println(Arrays.toString(strArr));
HashSet<String> newSet2 = new HashSet<>();
Collections.addAll(newSet2, "水菜丽","樱井步","aaa");
set.retainAll(newSet2);
System.out.println("------------------");
for (String element : set) {
System.out.println(element);
}
System.out.println("------------------");
Iterator<String> iterator = set.iterator();
while(iterator.hasNext()){
String element = iterator.next();
System.out.println(element);
}
}
}
2,HashSet的特点
import java.util.HashSet;
public class Test02 {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("aaa");
set.add("bbb");
set.add("ccc");
set.add("ddd");
set.add("ddd");
set.add("Aa");
set.add("BB");
for (String element : set) {
System.out.println(element);
}
}
}
五,LinkedHashSet集合类
1,LinkedHashSet的使用
import java.util.LinkedHashSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
public class Test01 {
public static void main(String[] args) {
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("麻生希");
set.add("椎名空");
set.add("水菜丽");
set.add("朝桐光");
set.add("樱井步");
set.add("周隽乐");
LinkedHashSet<String> newSet1 = new LinkedHashSet<>();
Collections.addAll(newSet1, "aaa","bbb","ccc","ccc");
set.addAll(newSet1);
int size = set.size();
System.out.println("获取元素个数:" + size);
System.out.println("判断集合中是否包含某个元素:" + set.contains("严荐翔"));
System.out.println("判断集合中是否包含某个子集合:" + set.containsAll(newSet1));
System.out.println("判断集合是否没有元素:" + set.isEmpty());
set.remove("朝桐光");
set.removeAll(newSet1);
Object[] objArr = set.toArray();
System.out.println(Arrays.toString(objArr));
String[] strArr = new String[set.size()];
set.toArray(strArr);
System.out.println(Arrays.toString(strArr));
LinkedHashSet<String> newSet2 = new LinkedHashSet<>();
Collections.addAll(newSet2, "水菜丽","樱井步","aaa");
set.retainAll(newSet2);
System.out.println("------------------");
for (String element : set) {
System.out.println(element);
}
System.out.println("------------------");
Iterator<String> iterator = set.iterator();
while(iterator.hasNext()){
String element = iterator.next();
System.out.println(element);
}
}
}
2,LinkedHashSet的特点
import java.util.LinkedHashSet;
public class Test02 {
public static void main(String[] args) {
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("aaa");
set.add("bbb");
set.add("ccc");
set.add("ddd");
set.add("ddd");
set.add("Aa");
set.add("BB");
for (String element : set) {
System.out.println(element);
}
}
}
六,TreeSet
1,TreeSet的使用
import java.util.TreeSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
public class Test01 {
public static void main(String[] args) {
TreeSet<String> set = new TreeSet<>();
set.add("麻生希");
set.add("椎名空");
set.add("水菜丽");
set.add("朝桐光");
set.add("樱井步");
set.add("周隽乐");
TreeSet<String> newSet1 = new TreeSet<>();
Collections.addAll(newSet1, "aaa","bbb","ccc","ccc");
set.addAll(newSet1);
int size = set.size();
System.out.println("获取元素个数:" + size);
System.out.println("判断集合中是否包含某个元素:" + set.contains("严荐翔"));
System.out.println("判断集合中是否包含某个子集合:" + set.containsAll(newSet1));
System.out.println("判断集合是否没有元素:" + set.isEmpty());
set.remove("朝桐光");
set.removeAll(newSet1);
Object[] objArr = set.toArray();
System.out.println(Arrays.toString(objArr));
String[] strArr = new String[set.size()];
set.toArray(strArr);
System.out.println(Arrays.toString(strArr));
TreeSet<String> newSet2 = new TreeSet<>();
Collections.addAll(newSet2, "水菜丽","樱井步","aaa");
set.retainAll(newSet2);
System.out.println("------------------");
for (String element : set) {
System.out.println(element);
}
System.out.println("------------------");
Iterator<String> iterator = set.iterator();
while(iterator.hasNext()){
String element = iterator.next();
System.out.println(element);
}
}
}
2,TreeSet的特点
import java.util.TreeSet;
public class Test02{
public static void main(String[] args) {
TreeSet<String> set1 = new TreeSet<>();
set1.add("c");
set1.add("d");
set1.add("a");
set1.add("b");
set1.add("b");
for (String element : set1) {
System.out.println(element);
}
TreeSet<Integer> set2 = new TreeSet<>();
set2.add(3);
set2.add(1);
set2.add(4);
set2.add(2);
set2.add(2);
for (Integer integer : set2) {
System.out.println(integer);
}
}
}
3,TreeSet存储自定义类型 – 内置比较器(Comparable)
public class Student implements Comparable<Student>{
private String name;
private char sex;
private int age;
private String classId;
private String id;
public Student() {
}
public Student(String classId, String id) {
this.classId = classId;
this.id = id;
}
public Student(String name, char sex, int age, String classId, String id) {
this.name = name;
this.sex = sex;
this.age = age;
this.classId = classId;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if(obj instanceof Student){
Student stu = (Student) obj;
if(this.classId.equals(stu.classId) && this.id.equals(stu.id)){
return true;
}
}
return false;
}
@Override
public String toString() {
return name + "\t" + sex + "\t" + age + "\t" + classId + "\t" + id;
}
@Override
public int compareTo(Student o) {
return this.age - o.age;
}
}
import java.util.TreeSet;
public class Test03 {
public static void main(String[] args) {
TreeSet<Student> set = new TreeSet<>();
set.add(new Student("麻生希", '女', 26, "2308", "001"));
set.add(new Student("椎名空", '女', 23, "2308", "002"));
set.add(new Student("水菜丽", '女', 29, "2308", "003"));
set.add(new Student("朝桐光", '女', 32, "2308", "004"));
set.add(new Student("北岛玲", '女', 42, "2308", "005"));
set.add(new Student("三上悠亚", '女', 31, "2308", "006"));
set.add(new Student("古川伊织", '女', 24, "2308", "007"));
set.add(new Student("濑亚美莉", '女', 21, "2308", "008"));
set.add(new Student("深田咏美", '女', 23, "2308", "009"));
set.add(new Student("北条麻衣", '女', 35, "2308", "010"));
set.add(new Student("水野朝阳", '女', 32, "2308", "011"));
set.add(new Student("徐灿", '男', 23, "2308", "012"));
set.add(new Student("波多野结衣", '女', 29, "2309", "001"));
set.add(new Student("小西满里惠", '女', 31, "2309", "002"));
set.add(new Student("铃原爱蜜莉", '女', 23, "2309", "003"));
set.add(new Student("桃谷绘里香", '女', 24, "2309", "004"));
set.add(new Student("京香Julia", '女', 32, "2309", "005"));
set.add(new Student("徐灿", '男', 25, "2309", "006"));
set.add(new Student("彭鹏", '男', 26, "2309", "007"));
set.add(new Student("周隽乐", '男', 27, "2309", "008"));
for (Student stu : set) {
System.out.println(stu);
}
}
}
4,TreeSet存储自定义类型 – 外置比较器(Comparator)
import java.util.Comparator;
import java.util.TreeSet;
public class Test04 {
public static void main(String[] args) {
TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.equals(o2)){
return 0;
}
int length1 = o1.getName().length();
int length2 = o2.getName().length();
if(length1 != length2){
return length1 - length2;
}
int age1 = o1.getAge();
int age2 = o2.getAge();
if(age1 != age2){
return age1 - age2;
}
return 1;
}
});
set.add(new Student("麻生希", '女', 26, "2308", "001"));
set.add(new Student("椎名空", '女', 23, "2308", "002"));
set.add(new Student("水菜丽", '女', 29, "2308", "003"));
set.add(new Student("朝桐光", '女', 32, "2308", "004"));
set.add(new Student("北岛玲", '女', 42, "2308", "005"));
set.add(new Student("三上悠亚", '女', 31, "2308", "006"));
set.add(new Student("古川伊织", '女', 24, "2308", "007"));
set.add(new Student("濑亚美莉", '女', 21, "2308", "008"));
set.add(new Student("深田咏美", '女', 23, "2308", "009"));
set.add(new Student("北条麻衣", '女', 35, "2308", "010"));
set.add(new Student("水野朝阳", '女', 32, "2308", "011"));
set.add(new Student("徐灿", '男', 23, "2308", "012"));
set.add(new Student("波多野结衣", '女', 29, "2309", "001"));
set.add(new Student("小西满里惠", '女', 31, "2309", "002"));
set.add(new Student("铃原爱蜜莉", '女', 23, "2309", "003"));
set.add(new Student("桃谷绘里香", '女', 24, "2309", "004"));
set.add(new Student("京香Julia", '女', 32, "2309", "005"));
set.add(new Student("徐灿", '男', 25, "2309", "006"));
set.add(new Student("彭鹏", '男', 26, "2309", "007"));
set.add(new Student("周隽乐", '男', 27, "2309", "008"));
for (Student stu : set) {
System.out.println(stu);
}
}
}