解決Qt 5.12.5 執行報錯 qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed
集合
定義:物件的容器,實現了對物件的常用操作,類似陣列的功能。
和陣列的區別:
- 陣列的長度是固定的,集合長度不固定
- 陣列可以儲存基本型別和引用型別,集合只能儲存引用型別
位置:java.util.*
Collection體系集合
collection父介面使用
特點:代表一組任意型別的物件,無序,無下標,不能重複
public class Demo01 { public static void main(String[] args) { //建立集合 Collection collection = new ArrayList(); //新增元素 collection.add("紅豆"); collection.add("我願意"); collection.add("旋木"); collection.add("只愛陌生人"); collection.add("幽蘭操"); System.out.println("元素個數"+collection.size()); System.out.println(collection); //刪除元素 collection.remove("旋木"); System.out.println("元素個數"+collection.size()); System.out.println(collection); //遍歷元素 //增強for迴圈 System.out.println("==============使用增強for迴圈遍歷元素=============="); for (Object o : collection) { System.out.println(o); } //專門用來遍歷集合的一種方式 System.out.println("==============使用迭代器遍歷元素=============="); //hasNext()有無下個元素 next()獲取下個元素 remove()刪除當前元素 迭代過程中不能使用collection.remove進行刪除 Iterator iterator = collection.iterator(); while (iterator.hasNext()){ String string = (String) iterator.next(); System.out.println(string); iterator.remove(); } System.out.println("元素個數"+collection.size()); //判斷 System.out.println("==============判斷=============="); System.out.println(collection.contains("旋木")); } }
public class Couple { private int age; private String name; public Couple(int age, String name) { this.age = age; this.name = name; } 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; } @Override public String toString() { return "Couple ["+"name="+this.name+","+"age="+this.age+"]"; } }
public class Demo02 { public static void main(String[] args) { Collection collection = new ArrayList(); //新增資料 Couple c1 = new Couple(26,"AB"); Couple c2 = new Couple(27,"CD"); Couple c3 = new Couple(28,"EF"); collection.add(c1); collection.add(c2); collection.add(c3); System.out.println(collection.size()); System.out.println(collection.toString()); //刪除資料 //collection.remove(c1); //System.out.println(collection.size()); //collection.clear(); //System.out.println(collection.size()); //遍歷 for (Object o : collection) { System.out.println(o); } System.out.println("==============使用迭代器遍歷元素=============="); //迭代器 Iterator iterator = collection.iterator(); int i = 0; while (iterator.hasNext()){ System.out.println(i); Couple couple = (Couple) iterator.next(); System.out.println(couple.toString()); i++; } //判斷 Couple c4 = new Couple(28,"EF"); System.out.println(collection.contains(c4)); } }
List子介面
特點:有序,有下標,元素可重複
程式碼例項
public class Demo03 {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("Apple");
list.add("XiaoMi");
list.add("HuaWei");
System.out.println("元素個數"+list.size());
System.out.println(list.toString());
for (Object o : list) {
System.out.println(o);
}
for (int i = 0;i<list.size();i++){
System.out.println(list.get(i));
}
//listiterator可以向前也可以向後遍歷列表
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.nextIndex()+":"+listIterator.next());
}
System.out.println("=====逆序======");
while (listIterator.hasPrevious()){
System.out.println(listIterator.previousIndex()+":"+listIterator.previous());
}
//判斷
System.out.println(list.contains("HuaWei"));
System.out.println(list.isEmpty());
//獲取位置
System.out.println(list.indexOf("HuaWei"));
}
}
List實現類
ArrayList
特點:陣列結構實現,查詢快、增刪慢;JDK1.2之後出現的,執行效率快,執行緒不安全
預設容量大小:10 如果沒有向集合中新增元素,那麼其大小為0;新增任意一個元素後,容量變成10;超過10就會 擴容,每次擴容為前一次的1.5倍(newcapacity=oldcapacity右移一位+newcapacity)
elementData陣列用來存放元素
size實際元素個數
add()新增元素
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
程式碼例項
public class Couple {
private int age;
private String name;
public Couple(int age, String name) {
this.age = age;
this.name = name;
}
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;
}
@Override
public String toString() {
return "Couple ["+"name="+this.name+","+"age="+this.age+"]";
}
@Override
public boolean equals(Object obj) {
if (this==obj){
return true;
}
if (obj==null){
return false;
}
if (obj instanceof Couple){
Couple couple = (Couple)obj;
if (this.name.equals(couple.getName())&&this.age==couple.getAge()){
return true;
}
}
return false;
}
}
public class Demo05 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList<>();
Couple c1 = new Couple(16, "AB");
Couple c2 = new Couple(17, "CD");
Couple c3 = new Couple(18, "EF");
arrayList.add(c1);
arrayList.add(c2);
arrayList.add(c3);
System.out.println("the number og the segments is "+arrayList.size());
System.out.println(arrayList);
System.out.println(arrayList.toString());
//arrayList.remove(0);//刪除之後,後面的元素會補上來
//arrayList.remove(new Couple(16,"AB"));//方法重寫 從地址判斷 修改成為內容判斷
//System.out.println(arrayList.get(0));
//遍歷
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("================列表迭代器順序==================");
ListIterator listIterator = arrayList.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.next());
}
System.out.println("================列表迭代器逆序==================");
while (listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
//判斷
System.out.println(arrayList.contains(new Couple(16,"AB")));
}
}
Vector
特點:陣列結構實現,查詢快,增刪慢;JDK1.0出現,執行效率慢,執行緒安全
程式碼例項:
public class Demo06 {
public static void main(String[] args) {
//建立集合
Vector vector = new Vector();
vector.add("Apple");
vector.add("Banana");
vector.add("Orange");
vector.add("Strawberry");
System.out.println("元素個數="+vector.size());
//列舉器
Enumeration en = vector.elements();
while (en.hasMoreElements()){
System.out.println(en.nextElement());
}
}
}
LinkedList
特點:連結串列結構實現,增刪快,查詢慢;
程式碼例項
public class Demo07 {
//雙向連結串列
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
Couple c1 = new Couple(1, "A");
Couple c2 = new Couple(2, "B");
Couple c3 = new Couple(3, "C");
linkedList.add(c1);
linkedList.add(c2);
linkedList.add(c3);
System.out.println("元素個數"+linkedList.size());
System.out.println(linkedList);
for (Object o : linkedList) {
System.out.println(o);
}
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
ListIterator listIterator = linkedList.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.next());
}
while (listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
//獲取
System.out.println(linkedList.indexOf(c1));
}
}
泛型
Java泛型是JDK1.5中引入的一個新特性,其本質是引數化型別,把型別作為引數傳遞
常見形式有泛型類、泛型介面、泛型方法
語法:<T,...>T稱為型別佔位符,表示一種引用型別
好處:
- 提高程式碼的重用性
- 防止型別轉換異常,提高程式碼的安全性
泛型類
程式碼例項
//T是型別佔位符,表示一種引用型別,如果編寫多個,使用逗號隔開
public class MyGeneric <T>{
//使用泛型T
//建立變數,但是不能new一個變數,因為變數的型別不確定,不能例項化
T t;
//新增方法
public void show(T t){
System.out.println(t);
}
//使用泛型作為方法返回值
public T getT(){
return t;
}
}
泛型介面
程式碼例項
public interface MyInterface <T>{
String name = "Faye";
//不能使用泛型定義靜態常量
T server(T t);
}
public class MyInterfaceImpl implements MyInterface<String>{
@Override
public String server(String s) {
System.out.println(s);
return null;
}
}
泛型類實現泛型介面
⬇
public class MyInterfaceImpl2<T> implements MyInterface <T> {
@Override
public T server(T t) {
System.out.println(t);
return null;
}
}
泛型方法
public class MyGenericMethod {
//泛型方法
public void show(){
System.out.println("普通方法");
}
public <T> void show2(T t){
System.out.println(t+"泛型方法");
}
}
public class TestGeneric {
public static void main(String[] args) {
MyGeneric<String> myGeneric = new MyGeneric<>();
myGeneric.t = "hello";
myGeneric.show("hello bitches");
String str = myGeneric.getT();
System.out.println(str);
//泛型只能是引用型別,不同的泛型物件不能相互賦值
MyGeneric<Integer> myGeneric1 = new MyGeneric<>();
myGeneric1.t = 100;
myGeneric1.show(200);
MyInterfaceImpl myInterface = new MyInterfaceImpl();
myInterface.server("????");
MyInterfaceImpl2<String> myInterfaceImpl2 = new MyInterfaceImpl2<>();
myInterfaceImpl2.server("!!!!!!");
MyGenericMethod myGenericMethod = new MyGenericMethod();
myGenericMethod.show2("傳遞的型別不用確定,寫啥是啥");
myGenericMethod.show2(11927050);
myGenericMethod.show2(11927050.1314);
}
}
泛型集合
引數化型別、型別安全的集合,強制集合元素的型別必須一致。
特點:
- 編譯時即可檢查,而非執行時丟擲異常。
- 訪問時,不必型別轉換(拆箱)。
- 不同泛型之間引用不能相互賦值,泛型不存在多型
程式碼例項
public class Demo01 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("xxx");
arrayList.add("yyy");
for (String s : arrayList) {
System.out.println(s);
}
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Set集合
set子介面
特點:無序、無下標、元素不可重複
方法:全部繼承自collection中的方法
程式碼例項
public class Book implements Comparable<Book>{
private String name;
private int price;
public Book(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public int hashCode() {
//31是一個質數,減少雜湊衝突,避免出現hashCode的撞車
//31能夠提高執行效率:31*i = (i<<5)-i
int n1 = this.name.hashCode();
int n2 = this.price;
final int prime = 31;
int result = 1;
result = prime*result + price;
result = prime*result + ((name == null)? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this==obj){
return true;
}
if (this == null){
return false;
}
if (this instanceof Book){
Book book = (Book)obj;
if (this.name==book.getName()&&this.price==book.getPrice()){
return true;
}
}
return false;
}
@Override
//先比較姓名 再比較年齡
public int compareTo(Book o) {
int n1 = this.name.compareTo(o.getName());
int n2 = this.price-o.getPrice();
return n1 == 0?n2:n1;
}
}
public class Demo01 {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("《海邊的卡夫卡》");
set.add("《世界盡頭與冷酷仙境》");
set.add("《且聽風吟》");
set.add("《挪威的森林》");
set.add("《刺殺騎士團長》");
set.add("《刺殺騎士團長》");
System.out.println(set.size());
System.out.println(set);
set.remove("《且聽風吟》");
//遍歷
System.out.println("==================增強for迴圈==================");
for (String s : set) {
System.out.print(s+" ");
}
System.out.println("");
System.out.println("==================迭代器==================");
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println("");
System.out.println(set.contains("《且聽風吟》"));
}
}
Set實現類:
1. HashSet:
基於HashCode實現元素不重複;當存入元素的HashCode相同的時候,會呼叫equals進行確認,如果結果為true,則拒絕後者的進入
儲存結構為雜湊表(陣列+單向連結串列)
儲存過程:根據hashcode計算儲存為止,如果位置為空,則直接儲存,如果不為空,則執行第二步
再執行equals方法,若為true,則認為是重複的,否則形成連結串列
程式碼例項
public class Demo03 {
public static void main(String[] args) {
HashSet<Book> bookHashSet = new HashSet<>();
Book book1 = new Book("設計中的設計", 100);
Book book2 = new Book("巨人的隕落", 800);
Book book3 = new Book("我們這個世界的羊", 900);
Book book4 = new Book("我們這個世界的羊", 900);
bookHashSet.add(book1);
bookHashSet.add(book2);
bookHashSet.add(book3);
bookHashSet.add(book4);
bookHashSet.add(new Book("我們這個世界的羊", 900));
System.out.println(bookHashSet.size());
//bookHashSet.remove(new Book("我們這個世界的羊", 900));
//System.out.println(bookHashSet.size());
System.out.println(bookHashSet.contains(new Book("我們這個世界的羊", 900)));
}
}
2. TreeSet:
給予排列順序實現元素不重複
實現了SortedSet介面,對集合元素自動排序;元素物件的型別必須實現Comparable介面,指定排序規則;通過Comparable方法確定是否為重複元素
public class Demo04 {
public static void main(String[] args) {
TreeSet<Book> treeSet = new TreeSet<>();
Book book1 = new Book("設計中的設計", 100);
Book book2 = new Book("巨人的隕落", 800);
Book book3 = new Book("我們這個世界的羊", 900);
Book book4 = new Book("我們這個世界的羊", 900);
treeSet.add(book1);
treeSet.add(book2);
treeSet.add(book3);
treeSet.add(book4);
//元素必須實現comparable介面,compareTo()返回值為0的時候,認為是重複元素
System.out.println(treeSet.size());
}
}
//比較器實現定製比較
public class Demo05 {
public static void main(String[] args) {
//建立集合的時候就制定了比較規則,不用實現介面
TreeSet<Book> treeSet = new TreeSet<>(new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
int n1 = o1.getPrice()-o2.getPrice();
int n2 = o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
}
}
練習程式碼
//使用TreeSet集合來實現字串按照長度進行排序
//comparator介面
public class Demo06 {
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length()-o2.length();
int n2 = o1.compareTo(o2);
return n1==0?n2:n1;
}
});
treeSet.add("abcd");
treeSet.add("bcd");
treeSet.add("ebcd");
treeSet.add("abdd");
Iterator<String> iterator = treeSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Map集合
Map父介面
特點:儲存一對資料(Key-Value),無序無下標、鍵不允許重複,值可以重複
程式碼例項
public class Demo01 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("Cola","5");
map.put("Spine","3.5");
map.put("Soda","4.5");
map.put("Soda","7");
System.out.println(map.size());
System.out.println(map.toString());
//map.remove("Soda");
System.out.println(map.toString());
Set<String> keySet = map.keySet();
for (String s : keySet) {
System.out.println(s+"===="+map.get(s));
}
for (String s : map.keySet()) {
System.out.println(s+"===="+map.get(s));
}
System.out.println("使用entryset方法");
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+"==="+entry.getValue());
}
}
}
Map實現類
public class Wechat implements Comparable{
private String username;
private int userage;
public Wechat() {
}
public Wechat(String username, int userage) {
this.username = username;
this.userage = userage;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUserage() {
return userage;
}
public void setUserage(int userage) {
this.userage = userage;
}
@Override
public int hashCode() {
int prime = 31;
int result = 1;
result = prime*this.getUsername().hashCode();
result = prime*this.getUserage()+result;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj){
return true;
}
if (obj == null){
return false;
}
if (obj instanceof Wechat){
Wechat wechat = (Wechat)obj;
if (this.userage==wechat.getUserage()&&this.username==wechat.getUsername()){
return true;
}
}
return false;
}
@Override
public int compareTo(Object o) {
Wechat o1 = (Wechat)o;
int n1 = this.username.compareTo(o1.getUsername());
int n2 = this.userage-o1.getUserage();
return n1 == 0?n2:n1;
}
}
HashMap
JDK1.2版本 執行緒不安全,執行效率快
1.1 當hashmap剛剛建立的時候,table是null,為了既省空間,當新增第一個元素的時候,table的容量調整為16
1.2 當元素個數大於閾值(16*0.75=12)的時候,會進行擴容,擴容為原來的兩倍,目的是減少調整元素的個數
1.3 jdk1.8 當每個連結串列的長度大於8,並且元素個數大於等於64的時候,會調整為紅黑樹,目的是提高執行效率
1.4 jdk1.8 當連結串列長度小於6時,調整成連結串列
1.5 jdk1.8 連結串列尾插入
程式碼例項
public class Demo01 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("Cola","5");
map.put("Spine","3.5");
map.put("Soda","4.5");
map.put("Soda","7");
System.out.println(map.size());
System.out.println(map.toString());
//map.remove("Soda");
System.out.println(map.toString());
Set<String> keySet = map.keySet();
for (String s : keySet) {
System.out.println(s+"===="+map.get(s));
}
for (String s : map.keySet()) {
System.out.println(s+"===="+map.get(s));
}
System.out.println("使用entryset方法");
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+"==="+entry.getValue());
}
}
}
public class Demo02 {
public static void main(String[] args) {
HashMap<Wechat, String> hashMap = new HashMap<>();
//剛建立沒有新增元素的時候 table = null size = 0 目的是節省空間
Wechat wechat1 = new Wechat("A", 20);
Wechat wechat2 = new Wechat("B", 30);
Wechat wechat3 = new Wechat("C", 40);
Wechat wechat4 = new Wechat("D", 50);
hashMap.put(wechat1,"China");
hashMap.put(wechat2,"America");
hashMap.put(wechat3,"Netherlands");
hashMap.put(wechat4,"England");
hashMap.put(new Wechat("A",20),"China");
System.out.println(hashMap.size());
//entryset方法遍歷陣列
Set<Map.Entry<Wechat, String>> entrySet = hashMap.entrySet();
for (Map.Entry<Wechat, String> stringEntry : entrySet) {
System.out.println(stringEntry.toString());
}
//keySet方法遍歷陣列
Set<Wechat> keySet = hashMap.keySet();
for (Wechat wechat : hashMap.keySet()) {
System.out.println(wechat);
}
for (Wechat wechat : keySet) {
System.out.println(wechat);
}
}
}
HashTable
(目前用的比較少)JDK1.0版本 執行緒安全 執行效率慢 不允許null作為key或者value
Properties
(HashTable的子類)要求key和value都是String,通常用於配置檔案的讀取
TreeMap
實現了SortedMap介面,可以對key自動排序
程式碼例項
public class Demo03 {
public static void main(String[] args) {
TreeMap<Wechat, String> treeMap = new TreeMap<>();
Wechat wechat1 = new Wechat("A", 20);
Wechat wechat2 = new Wechat("B", 30);
Wechat wechat3 = new Wechat("C", 40);
Wechat wechat4 = new Wechat("D", 50);
Wechat wechat5 = new Wechat("D", 50);
treeMap.put(wechat1,"China");
treeMap.put(wechat2,"America");
treeMap.put(wechat3,"Netherlands");
treeMap.put(wechat4,"England");
treeMap.put(wechat5,"England");
System.out.println(treeMap.size());
System.out.println("=================keyset=================");
Set<Wechat> keySet = treeMap.keySet();
for (Wechat wechat : keySet) {
System.out.println(wechat+"==="+treeMap.get(wechat));
}
System.out.println("=================entryset=================");
Set<Map.Entry<Wechat, String>> entrySet = treeMap.entrySet();
for (Map.Entry<Wechat, String> stringEntry : entrySet) {
System.out.println(stringEntry);
}
}
}
collections工具類
程式碼例項
public class Demo04 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(20);
list.add(27);
list.add(19);
list.add(69);
list.add(2);
list.add(2);
list.add(4);
list.add(2);
//排序
System.out.println("排序前:"+list.toString());
Collections.sort(list);
System.out.println("排序後:"+list.toString());
//二分法查詢 注意二分法的規則以及可能出現的問題
int i1 = Collections.binarySearch(list,12);
int i2 = Collections.binarySearch(list,2);//注意二分查詢的規則 這裡容易出錯
System.out.println(i1);
System.out.println(i2);
//複製 但是複製的操作要求兩個集合的大小一致
List<Integer> list2 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
list2.add(0);
}
Collections.copy(list2,list);
System.out.println(list2);
//逆序
Collections.reverse(list);
System.out.println(list);
//shuffle打亂
Collections.shuffle(list);
System.out.println(list);
//補充:list轉成陣列
Integer[] array = list.toArray(new Integer[0]);
System.out.println(array.length);
//陣列轉成list
String[] names = {"Jessica","Linda","Cherry","Sabrina"};
List<String> list1 = Arrays.asList(names);
//受限集合 不能刪除或者新增元素
System.out.println(list1);
//如果使用基本型別的話,會使得整個集合中只有一個元素,這個元素就是原來的陣列,因此需要使用包裝類
Integer[] nums = {20,30,40,60,60};
int[] nums2 = {20,30,40,60,60};
List<Integer> list3 = Arrays.asList(nums);
List<int[]> list4 = Arrays.asList(nums2);
System.out.println(list3.size());
System.out.println(list4.size());
}
}