那些网站可以做反链,金乡县网站建设,wordpress修改后台路径,网站打开加速集合
1 . Collection接口
Collection接口是Java单列集合的根接口#xff0c;它定义了各种具体单列集合的共性 #xff0c; 其它大多数单列集合直接或间接继承该接口
定义 :
public interface CollectionE extends IterableE{// Operations
}
一般用得少…集合
1 . Collection接口
Collection接口是Java单列集合的根接口它定义了各种具体单列集合的共性 其它大多数单列集合直接或间接继承该接口
定义 :
public interface CollectionE extends IterableE{// Operations
}
一般用得少基本上都是直接使用其子接口,如 : List , Set , Queue , SortedSet ;
其中 List : 有序 可重复 有索引 Set : 无序 不重复 无索引
该接口的常用方法 : boolean add(Object o) : 向当前集合中添加一个元素 boolean addAll(Collection c) : 将c中所有元素添加到当前集合 void clear() : 删除当前集合得全部元素 ; boolean remove(Object o) : 删除指定元素 ; boolean removeAll(Collection c) : 删除全部元素 ; bool isEmpty() : 判断当前集合是否为空 boolean contains(Object o) : 判断是否包含元素o; boolean containsAll(Collection c) : 判断是否包含集合c中全部元素 Iterator itreator() : 返回当前集合得迭代器 , 用于遍历集合中的元素 int size() : 获取集合大小;
示例 :
package com.itheima.d1_collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
/**目标明确Collection集合体系的特点*/
public class collectionDemo1 {public static void main(String[] args) {//有序 可重复 有索引Collection list new ArrayList();list.add(java);list.add(java);list.add(Mybatis);list.add(23);list.add(23);list.add(false);list.add(false);System.out.println(list);System.out.println(-------------------);
//无序 不重复 无索引Collection list1 new HashSet();list1.add(java);list1.add(java);list1.add(Mybatis);list1.add(23);list1.add(23);list1.add(false);list1.add(false);
System.out.println(list1);System.out.println(-----------------------------------);CollectionString list2new ArrayListString();list2.add(java);// list2.add(23);(报错)list2.add(黑马);
//集合和泛型只支持引用数据类型不支持基本类型CollectionInteger list3 new ArrayListInteger();list3.add(23);}
}
常用API :
package d2_collection_api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
public class CollectionDemo2 {public static void main(String[] args) {
//Hashset:添加的元素是无序的不重复无索引CollectionString listnew ArrayList();//1.添加元素添加成功返回truelist.add(java);list.add(html);list.add(java);list.add(Java);list.add(mysql);System.out.println(list.add(html));System.out.println(list);
//2.清空集合中的元素// list.clear();
//3.判断集合是否为空为空返回trueSystem.out.println(list.isEmpty());
//4.获取集合的大小System.out.println(list.size());
//5.判断集合中是否包含这个元素System.out.println(list.contains(java));
//6.删除某个元素如果有多个重复元素那就删除前一个System.out.println(list.remove(java));System.out.println(list);
//7.吧集合转换成数组[java,独孤求败html,Mybatis]Object[] arrs list.toArray();System.out.println(Arrays.toString(arrs));
System.out.println(---------------拓展--------------);
CollectionString c1new ArrayListString();c1.add(java1);c1.add(java2);CollectionString c2 new ArrayListString();c2.add(java3);c2.add(java4);//addAll把c2集合中的元素全部倒入到c1里面去c1.addAll(c2);System.out.println(c1);System.out.println(c2);
}
}
2 . List接口
2 . 1 List接口简介 List继承自Collection接口 特点 : 有序(存入顺序与取出顺序相同) 可重复 线性 , 有索引 ;
特有API (Collection的全有) : void add(int idx , Object o) : 将元素o插入到idx处 ; addAll(int idx , Collection c) : 将集合c的所有元素插入到List集合的idx处 ; get(idx) : 获取下标为idx的元素 ; remove(idx) : 删除并返回idx处的元素 set(idx,o) : 设置idx处元素为o; int index(o) : 返回o第一次出现的下标 int lastIndexOf( o ) : 返回o最后一次出现的下标 List subList(int l , int r) : 截取[l,r)并返回
示例 :
package d5_collection_list;
import java.util.ArrayList;
import java.util.List;
public class ListDemo01 {public static void main(String[] args) {//1.创建一个ArrayList集合对象//2.List:有序可重复有索引的ListString list new ArrayList();list.add(java);list.add(java);
list.add(MySQL);list.add(MySQl);
//2.在某个索引位置插入元素list.add(0,HTML);
System.out.println(list);
//3.根据索引删除元素,返回被删除元素System.out.println(list.remove(2));System.out.println(list);
//4.根据索引获取元素public E get(int index):返回集合中指定位置的元素System.out.println(list.get(2));
//5.修改索引位置的元素public E set(int index,E element)//返回修改前的数据System.out.println(list.set(1, cpp));System.out.println(list);//[HTML, cpp, MySQL, MySQl]int idx list.indexOf(cpp) ;// 返回第一次出现的索引System.out.println(idx);int idx1 list.lastIndexOf(MySQl) ;System.out.println(idx1);
// 6 . 截取ListString lt list.subList(1,4) ; // 左闭右开System.out.println(lt) ;}
}
2 . 2 ArrayList接口
特点 : 长度可变 继承自List , 也有索引 查询元素很快增删效率较低 ;
ArrarList集合底层原理 1 . ArrayList底层是基于数组实现的根据索引定位元素快增删要做元素的移位操作 2 . 第一次创建集合并添加第一个元素的时候在底层创建一个默认长度为10的数组 示例 :
package d5_collection_list;
import java.util.ArrayList;
public class ArrayList1 {public static void main(String[] args) {ArrayList list new ArrayList() ;// 添加元素list.add(zs) ;list.add(ls) ;list.add(ww) ;list.add(zl) ;// 打印集合System.out.println(list);// 获取集合中元素的个数int sz list.size() ;System.out.println(sz) ;// 获取并打印指定元素的2位置System.out.println(list.get(2));//删除索引为3的元素list.remove(3);System.out.println(list);// 元素替换list.set(1 , yss) ;System.out.println(list);}
}
2 . 3 List集合的遍历方法
4种方法 : for循环 迭代器 foreach 不能修改集合中的值 lambda表达式
代码示例 :
package d5_collection_list;
/**List集合的遍历方法*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListDemo03 {public static void main(String[] args) {ListString lists new ArrayList();lists.add(java1);lists.add(java2);lists.add(java3);System.out.println(-----------------------);
/** 1.for循环 */for(int i0;ilists.size();i){String elelists.get(i);System.out.println(ele);}/** (2).迭代器 */System.out.println(-------------------------);IteratorString it lists.iterator();while(it.hasNext()){String eleit.next();System.out.println(ele);}
/** (* 3).foreach */System.out.println(--------------------);for(String ele:lists){System.out.println(ele);}/** (4)JDK 1.8开始之后的lambda表达式 */System.out.println(-----------------------------);lists.forEach(s-{System.out.println(s);});}
}
2 . 4 LinkedList 底层维护了一个双向链表 链表中的每一个元素都用引用的方式记录它的前一个元素和后一个元素
LinkedList特有的方法 : void add(int index , E element) : 在idx处插入元素element void addFirst( o ) void addLast(( o ) Object getFirst() Object getLast() removeFirst() : 删除第一个并返回 removeLast() : 删除最后一个并返回 boolean offer( o ) : 将o添加到集合末尾 boolean offerFirst( o ) : 添加到开头 boolean offerLast( o ) : 添加到末尾 Object peekFirst() : 获取第一个元素 Object peekLast() : 获取最后一个元素 Object pollFirst() : 移除并返回第一个 Object pollLast() : 移除并返回最后一个 void push( o ) : 将o添加到开头 Object pop() : 删除第一个并返回
示例(用LinkedList实现栈和队列) :
package d5_collection_list;
import java.util.LinkedList;
/**ArrarList集合底层原理1.ArrayList底层是基于数组实现的根据索引定位元素快增删要做元素的移位操作2.第一次创建集合并添加第一个元素的时候在底层创建一个默认长度为10的数组LinkedList的底层是用双链表来实现的*/
public class ListDemo02 {public static void main(String[] args) {//1.LinkedList可以完成队列结构和栈结构双链表//栈LinkedListString stack new LinkedList();//压栈入栈stack.push(java1);stack.push(java2);stack.addFirst(java3);stack.addFirst(java4);System.out.println(stack);//出栈弹栈// System.out.println(stack.getFirst());System.out.println(stack.pop());System.out.println(stack.removeFirst());System.out.println(stack.removeFirst());System.out.println(stack);
System.out.println();
//队列LinkedListString queue new LinkedList();//1.入队queue.offerLast(c1);queue.addLast(c2);queue.addLast(c3);queue.addLast(c4);System.out.println(queue);//2.出队System.out.println(queue.removeFirst());System.out.println(queue.removeFirst());System.out.println(queue.removeFirst());System.out.println(queue);}
}
3 . Set接口
3 . 1 set接口
特点 : 无序且不重复 ;
Set常见的三个实现类 : HashSet : 根据hash值来确定元素的存储位置,具有良好的存储和查找性能 LinkedHashSet : 链表哈希表 TreeSet : 二叉树 , 可以对元素进行排序
示例 :
package com.itheima.d1_set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
public class SetDemo1 {public static void main(String[] args) {// 看看set系列集合的特点HashSet,LinkedHashSet,TreeSet//无序不重复无索引SetString sets new HashSet(); //一行经典代码,多态sets.add(Mysql);sets.add(Mysql);sets.add(java);sets.add(java);sets.add(html);sets.add(html);sets.add(springboot);sets.add(springboot);System.out.println(sets);
//LinkedHashSet 有序 不重复无索引SetString sets1 new LinkedHashSet(); //一行经典代码,多态sets1.add(Mysql);sets1.add(Mysql);sets1.add(java);sets1.add(java);sets1.add(html);sets1.add(html);sets1.add(springboot);sets1.add(springboot);System.out.println(sets1);}
}
3 . 2 HashSet
HashSet存储的元素是不可重复的当向HashSet中添加一个元素时 首先会调用hashCode()方法确定元素的存储位置 然后再调用equals()方法确保该位置没有重复元素 Set接口和List接口存取元素的方式是一样的 吗但是Set集合中的元素是无序的 ;
调用hashCode()方法示例 :
public class SetDemo2 {public static void main(String[] args) {//目标学会获取对象的哈希值并确认一下String name itheima;System.out.println(name.hashCode());System.out.println(name.hashCode());
String name1 itheima1;System.out.println(name1.hashCode());System.out.println(name1.hashCode());
}
}
hashSet使用示例 :
public class hashSet {public static void main(String[] args) {HashSet ht new HashSet() ;ht.add(张三) ;ht.add(李四) ;ht.add(王五) ;ht.add(赵六) ;Iterator it ht.iterator();while(it.hasNext()){System.out.println(it.next());}}
}
3 . 3 LinkedHashSet
底层采用双向链表来维护内部元素之间的关系
LinkedHashSet set new LinkedHashSet() ; 3 . 4 TreeSet
内部采用二叉树存储元素 保证没有重复元素并且能够对元素进行排序 (二叉排序树);
TreeSet特有的方法 : Object first() : 返回集合的第一个元素 Object last() : 返回集合的最后一个元素 Object lower( o ) : 返回集合中小于给定元素的最大元素没有则返回null ; Object floor( o ) : 返回o的最大元素 没有返回null ; Object higher( o ) : o 的最小元素,.... Object ceiling( o ) : o 的最小元素 Object pollFirst() : 移除并返回集合的第一个元素 Object pollLast() : 移除并返回最后一个元素。
示例 :
Apple类 :
package com.itheima.d1_set;
public class Apple implements ComparableApple{private String name;private String color;private double price;private int weight;
public Apple() {}
public Apple(String name, String color, double price, int weight) {this.name name;this.color color;this.price price;this.weight weight;}
public String getName() {return name;}
public void setName(String name) {this.name name;}
public String getColor() {return color;}
public void setColor(String color) {this.color color;}
public double getPrice() {return price;}
public void setPrice(double price) {this.price price;}
public int getWeight() {return weight;}
public void setWeight(int weight) {this.weight weight;}
Overridepublic String toString() {return Apple{ name name \ , color color \ , price price , weight weight };}
/**方式一类自定义比较规则o1.compareTo(o2)* param o the object to be compared.* return*/Overridepublic int compareTo(Apple o) {//按照重量比较// return this.weight-o.weight 0 ?1 : -1; //保留return this.weight-o.weight; //去掉重量重复元素}
}
test :
package com.itheima.d1_set;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
/**目标 观察TreeSet对于有值特性的数据如何排序学会对自定义类型对象进行指定规则排序*/
public class SetDemo5 {public static void main(String[] args) {SetInteger sets new TreeSetInteger();//不重复 无索引 可排序sets.add(23);sets.add(24);sets.add(12);sets.add(8);System.out.println(sets);
SetString sets1 new TreeSet();//不重复 无索引 可排序sets1.add(d);sets1.add(c);sets1.add(b);sets1.add(a);System.out.println(sets1);
System.out.println(------------------------------------);//方式二:集合自带比较器对象进行规则制定
SetApple apples new TreeSet(new ComparatorApple() {Overridepublic int compare(Apple o1, Apple o2) {return o1.getWeight()- o2.getWeight(); //升序// return Double.compare(o2.getPrice()-o1.getPrice());}});apples.add(new Apple(hongfushi,red,9.9,500));apples.add(new Apple(lufushi,lu,15.9,300));apples.add(new Apple(yellowfushi,yellow,29.9,400));apples.add(new Apple(blackfushi,black,9.8,500));System.out.println(apples);
}
}
TreeSet对于自定义类型可以采用的两种排序规则 :
1.自然排序 :
类必须实现Comparable接口 并重写CompareTo()方法 /**方式一类自定义比较规则o1.compareTo(o2)* param o the object to be compared.* return*/
Override
public int compareTo(Apple o) {//按照重量比较// return this.weight-o.weight 0 ?1 : -1; //保留return this.weight-o.weight; //去掉重量重复元素
}
2.自定义排序:
SetApple apples new TreeSet(new ComparatorApple() {Overridepublic int compare(Apple o1, Apple o2) {return o1.getWeight()- o2.getWeight(); //升序// return Double.compare(o2.getPrice()-o1.getPrice());}
});
4 . Map接口
4 . 1 Map
Map接口是一个双列集合 , 每个元素包含[key,val] ; 键值对 , 称为映射 Map中的键不允许重复
常用方法 : void put(Object key , Object val) : 存入集合 Object get(Object key) : 获取key对应的值,不存在则返回null ; void clear() : 清空Map 移除所有的键值对集合 V renove(Object key) : 根据key删除对应的值返回被删除的值 ; int size() : 返回Map大小 boolean containsKey(Object Key) : 查看是否包含key的映射关系 boolean containsValue(Object value) : 查看是否包含key的映射关系 Set keySet() : 返回此映射中包含键的Set集合 CollectionV values() : 返回此映射中包含的值的Collection集合 SetMap,EntryK,V entrySet() : 返回此映射中包含的映射关系的集合 ;
示例 :
package com.itheima.d6_map_api;
import java.util.*;
import java.util.Map.Entry;
public class MapDemo {public static void main(String[] args) {MapString, Integer maps new HashMap();// 1.添加maps.put(张飞,18888);maps.put(小乔,2888) ;
//2.清空 maps.clear();
// maps.isEmpty();
System.out.println(maps.get(张飞));
boolean tag maps.containsKey(张飞);System.out.println(tag);maps.remove(张飞);boolean tag2 maps.containsKey(张飞);System.out.println(tag2);
maps.put(张飞,18888);boolean tag3 maps.containsValue(18888);System.out.println(tag3);
// 获取全部键的集合 public SetK keySet()SetString keys maps.keySet();System.out.println(keys);
//获取全部值的集合CollectionInteger values maps.values();System.out.println(values);
//集合的大小System.out.println(maps.size());
SetMap.EntryString,Integer st maps.entrySet();System.out.println(st);
//合并其他map集合// map1.putAll(map2);
}
}
4 . 2 HashMap 键不重复且元素无序 使用put遇到相同的key,那么value实现覆盖 ;
遍历方式 : foreach : maps.forEach((k,v)-{System.out.println(kv);}); 先转set : SetMap.EntryString, Integer entries maps.entrySet();for(Map.EntryString, Integer entry : entries){String key entry.getKey();int value entry.getValue();System.out.println(key value);} 先用Set存下所有的key,再遍历set,得到key,再得到val : //1.键找值SetString keys maps.keySet();//2. 第二步遍历每个键根据键找值for(String key:keys){int value maps.get(key);System.out.println(key value);} 4.3 LinkedHashMap
集合中的遍历顺序和存入顺序一致;
使用双向链表维护内部元素之间的关系 : 示例 :
public class linkedHashmap {public static void main(String[] args) {Map map new LinkedHashMap() ;map.put(3,李四) ;map.put(2,张三) ;Set keyset map.keySet() ;Iterator it keyset.iterator() ;while(it.hasNext()){Object key it.next() ;Object val map.get(key) ;System.out.println(key -- val);}}
}