為什么要用國外服務(wù)器做網(wǎng)站搜索引擎優(yōu)化英文簡稱為
集合體系結(jié)構(gòu)
Collection 單列集合
包含List Set
List 包含ArrayList LinkedList
Set包含HashSet TreeSet
HashSet包含LinkedHashSet
List系列集合:添加的元素是有序的、可重復(fù)、有索引
Set系列集合:添加的元素是無序的、不重復(fù)、無索引
Collection是單列集合的祖宗接口,它的功能是全部單列集合都可以繼承使用的
public static void main(String[] args) {//Collection是一個接口,我們不能直接創(chuàng)建它的對象Collection<String> coll=new ArrayList<>();//1.添加元素/*細節(jié)1.如果王List里添加元素,方法總是返回true 因為List允許元素重復(fù)細節(jié)2.如果往Set李添加元素,不存在返回true,存在返回falseSet系列集合不允許重復(fù)* */coll.add("aaa");coll.add("bbb");coll.add("ccc");coll.add("ddd");System.out.println(coll);//2.清空 // coll.clear(); // System.out.println(coll);//3.刪除//因為Collection里面定義的方法是共性的方法,所以不能通過索引進行刪除,智能通過元素的對象進行刪除/*方法會有一個布爾類型的返回值,true刪除成功,false失敗* */coll.remove("aaa");System.out.println(coll);//4.判斷元素是否包含//底層是依賴equals方法進行判斷是否存在的//所以要判斷自定義對象是否存在時候,要重新equals方法System.out.println(coll.contains("bbb"));//5.判空boolean empty = coll.isEmpty();System.out.println(empty);//6.獲取集合的長度int size = coll.size();System.out.println(size);}
Collection的遍歷方式
迭代器遍歷
特點:迭代器不依賴索引
迭代器在Java中的類是Iterator,迭代器是集合專用的遍歷方式
Collection<Integer> collection= new ArrayList<>(); collection.add(1); collection.add(2); collection.add(3); Iterator<Integer> it=collection.iterator(); while (it.hasNext()){int i=it.next();System.out.print(i+" "); }
細節(jié)注意:
1.報錯NoSuchElementException
2.迭代器遍歷完畢,指針不會復(fù)位
3.循環(huán)中只能用一次next方法
4,迭代器遍歷時,不能用集合的方法進行添加或者刪除
增強for遍歷
增強for的底層就是迭代器,為了簡化迭代器的代碼書寫的
所有的單列集合和數(shù)組才能用增強for遍歷
格式:
for(元素的數(shù)據(jù)類型 變量名:數(shù)組或者集合){
}
for(String s : list){
}
public static void main(String[] args) {Collection<String> coll = new ArrayList<>();coll.add("zhangsan");coll.add("lisi");coll.add("wangwu");for (String s : coll) {System.out.println(s);} }
細節(jié):
修改增強for中的變量,不會改變集合中原來的數(shù)據(jù)
Lambda表達式遍歷
public static void main(String[] args) {Collection<String> coll = new ArrayList<>();coll.add("zhangsan");coll.add("lisi");coll.add("wangwu");/* coll.forEach(new Consumer<String>() {@Override//s 是以此表示集合的每一個數(shù)據(jù)public void accept(String s) {System.out.println(s);}});*/coll.forEach(s -> System.out.println(s));} }
List中常見的方法和遍歷方式
//List集合中的兩個刪除的方法 //1.直接刪除元素 //2.通過素銀進行刪除//1.創(chuàng)建集合并添加元素
//1.創(chuàng)建一個集合 List<String> list =new ArrayList<>();//2.添加元素 list.add("a"); list.add("b"); list.add("c");//void add(int index,E element);在指定索引位置添加指定的元素 //細節(jié): /* 原來索引上的元素會依次往后移 * */ list.add(1,"QQQ");//remove String remove = list.remove(0); System.out.println(remove);//set String aaa = list.set(0, "aaa"); System.out.println(aaa);//get String s = list.get(0); System.out.println(s);//3.打印集合 System.out.println(list);
List集合的五種遍歷方式
//創(chuàng)建集合List<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");/* //1.迭代器遍歷Iterator<String> iterator = list.iterator();while (iterator.hasNext()){String next = iterator.next();System.out.println(next);}*///2.增強for/* for (String str : list) {System.out.println(str);}*///3.Lambda表達式 /* list.forEach(s-> System.out.println(s));*///4.普通for/* for (int i = 0; i < list.size(); i++) {String s = list.get(i);System.out.println(s);}*///5.列表迭代器//獲取一個列表迭代器對象,同樣的里面的指針也是指向零索引ListIterator<String> it = list.listIterator();while (it.hasNext()){String next = it.next();if ("bbb".equals(next)){it.add("qqq");}System.out.println(next);}System.out.println(list);
遍歷方式比較
迭代器遍歷:在遍歷的過程中需要刪元素,請使用迭代器
列表迭代器:在遍歷的時候需要添加元素,請使用列表迭代器
增強for,Lambda:僅僅想遍歷
普通for:如果遍歷的時候想操作索引
Set系列集合
無序:存取順序不一致
不重復(fù):可以去除重復(fù)
無索引:沒有帶索引的方法,所以不能使用普通for遍歷,也不能通過索引獲取元素
Set集合的實現(xiàn)類
HashSet:無序,不重復(fù),無索引
LinkedHashSet:有序,不重復(fù),無索引
TreeSet:可排序,不重復(fù),無索引
//1.創(chuàng)建Set集合對象Set<String> s = new HashSet<>();//2.添加元素s.add("aaa");s.add("bbb");s.add("ccc");s.add("ddd"); // s.add("aaa");3.遍歷//迭代器遍歷Iterator<String> iterator = s.iterator();while (iterator.hasNext()) {String next = iterator.next();System.out.println(next);}//增強forfor (String s1 : s) {System.out.println(s1);}//Lambda表達式s.forEach(a -> System.out.println(a));
HshSet底層采取哈希表存儲數(shù)據(jù)
TreeSet集合默認的規(guī)則:對于字符、字符串類型,按照ASCII碼表中的順序進行排序
TreeSet的兩種排序方式
1.自然排序
類里面實現(xiàn)Comparable接口里面的方法
@Override public int compareTo(Student o) {//指定排序的規(guī)則//按照年齡的升序進行排序return this.getAge()-o.getAge();}
2.比較器排序
TreeSet<String> ts = new TreeSet<>((o1, o2) -> {int i = o1.length() - o2.length();i = i == 0 ? o1.compareTo(o2) : i;return i; });
TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {int i = o1.length() - o2.length();i = i == 0 ? o1.compareTo(o2) : i;return i;} });
使用場景
1.如果想要集合中的元素可重復(fù)
用ArrayList基于數(shù)組的
2.如果想要集合中的元素可重復(fù),而且當(dāng)前的增刪操作明顯多于查詢
用LinkedList,基于鏈表的
3.如果想對集合中的元素去重
用HashSet,基于哈希表的
4.如果想對集合中的元素去重,而且保重存取順序
用LinkedHashList,基于哈希表和雙鏈表,效率低于HashSet
5.如果想對集合中的元素進行排序
用TreeSet,基于紅黑樹,后續(xù)可以用List集合實現(xiàn)排序