结构体系:
Collection 父接口,List、Set子接口,不能被实例化(不能new一个对象)。
Arrylist、LinkedList、Vector、HashSet、SortdeSet、TreeSet可以实例化(可以new一个对象)。
Collection接口:
Collection基础使用:
package org.example;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;public class Main {public static void main(String[] args) {// 创建集合Collection collection=new ArrayList();// 添加集合元素collection.add("xianghong");collection.add("xianglan");collection.add("xiangdd");collection.add("xiangff");collection.add("xiangghdf");// 输出集合元素System.out.println(collection);// 输出集合元素个数System.out.println(collection.size());// 删除集合中的一个元素collection.remove("xianghong");}
}
输出结果:
使用增强for循环遍历元素:
// 使用增强for遍历元素for (Object a:collection) {System.out.println(a);}
结果如下:
使用迭代器来遍历集合:
// 使用迭代器来遍历集合//hasNext();有没有下一个元素。//next();获取下一个元素。//remove();删除当前元素。Iterator iterator= collection.iterator();while (iterator.hasNext()){System.out.println(iterator.next());iterator.remove();System.out.println(collection.size());}
结果如下:
List接口:
list集合是有角标的类似于数组。可以使用角标去运算。
package org.example;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class Demo3 {public static void main(String[] args) {// 创建List集合对象List list=new ArrayList();// 添加元素list.add("葡萄");list.add("水果");list.add("苹果");//输出元素个数System.out.println(list.size());//输出集合中的元素System.out.println(list.toString());//删除下标为0的元素list.remove(0);//打印集合中的元素System.out.println(list.toString());//用foreach循环遍历集合for (Object c:list) {System.out.println(c);}System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");//使用Iterator迭代器。//hasNext();集合中是否有下一个元素。//next();获取下一个元素。Iterator iterator=list.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}
}
输出结果:
获取元素位置:
//获取位置System.out.println(list.indexOf("水果"));
结果为 :
列表迭代器ListIterator:
//使用列表迭代器ListIterator。//hasNext();集合中是否有下一个元素。//next();获取下一个元素。//nextIndex();在调用next()方法后返回元素的下标。//从前往后输出集合中的元素。ListIterator listIterator= list.listIterator();while (listIterator.hasNext()){System.out.println(listIterator.nextIndex()+":"+listIterator.next());}System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");//hasPrevious();集合中是否有前一个元素。//previous();获取前一个元素,要和hasPrevious()搭配使用。//previousIndex();在调用previous()方法时返回当前元素的前一个元素的下标。//从后往前输出集合中的元素。while (listIterator.hasPrevious()){System.out.println(listIterator.previousIndex()+":"+listIterator.previous());}
结果如下:
Iterator和ListIterator区别:
Iterator只能单向遍历集合,即只能从头到尾依次遍历元素。可以用于遍历任何实现了Collection接口的集合,如Set、List等。
ListIterator可以双向遍历集合,既可以从头到尾遍历,也可以从尾到头遍历。这得益于它提供的previous()、hasPrevious()等方法。是Iterator的子接口,专门用于遍历List集合。它提供了更多针对List集合的特性,如添加、修改、获取元素索引等操作。
Vector集合类:
package org.example;import java.util.Enumeration;
import java.util.Vector;public class Demo4 {public static void main(String[] args) {//创建vectory集合Vector vector=new Vector();//插入数据vector.add("百度");vector.add("阿里");vector.add("小米");vector.add("字节跳动");System.out.println(vector.size());//使用枚举器遍历Enumeration elements = vector.elements();while (elements.hasMoreElements()){System.out.println(elements.nextElement());}//判断//contains();判断是否有该元素,返回true或false//isEmpty();检查字符串是否为空,返回true或falseSystem.out.println(vector.contains("小米"));System.out.println(vector.isEmpty());}
}
结果为:
ArrayList和LinkedList区别:
ArrayList是一个数组,必须开辟连续的空间,查询快,增删慢。
LinkedList是一个双向链表,查询慢,增删快。
泛型类的创建和使用:
创建:
package org.example;public class MGeneric <T>{ //T是类型占位符,表示一种引用类型,如果编写多个使用逗号隔开。//使用泛型T创建变量T t;//泛型作为方法的参数public void show(T t){System.out.println(t);}//泛型作为方法的返回值public T getT() {return t;}
}
使用:
package org.example;public class TGeneric {public static void main(String[] args) {//使用泛型创建对象//字符串型MGeneric<String> mGeneric=new MGeneric<String>();mGeneric.t="加油";mGeneric.show("请你努力,为了你自己");String t = mGeneric.getT();System.out.println(t);//整数型MGeneric<Integer> mGeneric1=new MGeneric<Integer>();mGeneric1.t=(1);mGeneric1.show(100000);Integer t1 = mGeneric1.getT();System.out.println(t1);}
}
运行结果:
泛型只能使用引用类型,不同泛型类型对象之间不能相互赋值。
泛型接口:
package org.example;public interface MyInterface<T> {T server(T t);
}
实现类:
package org.example;public class MyInterfaceImpl implements MyInterface<String>{public String server(String t) {System.out.println(t);return t;}
}
MyInterfaceImpl myInterface=new MyInterfaceImpl();myInterface.server("好好加油");
结果为:
泛型方法:
package org.example;public class MyGenericfangfa {public <T> T show(T t){System.out.println("这有啥用"+t);return t;}}
MyGenericfangfa myGenericfangfa=new MyGenericfangfa();myGenericfangfa.show("这没什么用");myGenericfangfa.show(777);myGenericfangfa.show(3.1415926535897);
结果为:
泛型的好处:
泛型集合允许你在声明集合时指定元素的类型。这意味着你可以在编译时捕获类型不匹配的错误,而不是在运行时。这减少了运行时错误,提高了代码的健壮性。
可以用于多种数据类型的代码。例如,一个 List<T>
可以存储任何类型的对象,而不需要为每种类型编写一个特定的集合类。这提高了代码的复用性和可维护性。
Set接口:
package org.example;import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;public class Demo5 {public static void main(String[] args) {//创建set集合Set<String> set=new HashSet<String>();//插入数据set.add("星期一");set.add("星期二");set.add("星期三");set.add("星期四");set.add("星期五");set.add("星期六");//删除数据set.remove("星期六");System.out.println(set.size());System.out.println(set);System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");//使用foreach循环遍历集合for (String a:set) {System.out.println(a);}System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");//使用迭代器遍历结合Iterator<String> in=set.iterator();while (in.hasNext()){System.out.println(in.next());}//判断System.out.println(set.contains("星期一"));System.out.println(set.isEmpty());}}
结果如下:
HashSet集合:
基于HashCode计算元素存放位置。
当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝后者存入。
//创建HashSet集合HashSet<String> hashSet=new HashSet<String>();
31*i=(i<<5)-i 移位运算效率更高。
TreeSet方法、Comparator接口:
package org.example;import java.util.Comparator;
import java.util.TreeSet;public class Demo6 {public static void main(String[] args) {//创建TreeSet对象,泛型参数为String,创建一个匿名内部类实现Comparator<String>接口TreeSet<String> treeSet=new TreeSet<String>(new Comparator<String>() {//compare方法默认是字典序排序,用户可自定义排序规则,以下是根据字符串长度比较的方法。public int compare(String o1, String o2) {//比较两个字符串的长度,返回比较字符串长度的差值int n1=o1.length()-o2.length();//n2为compare方法字典序排序int n2=o1.compareTo(o2);//若两字符串长度相等返回n2,不等返回n1.return n1==0?n2:n1;}});//添加数据treeSet.add("hahha");treeSet.add("zhenfulee");treeSet.add("ni");treeSet.add("dddd");treeSet.add("aaaaaaaaaaaaaaa");//输出集合内容System.out.println(treeSet.toString());}
}
结果如下: