日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java面试知识点:集合、Set、泛型、HashSet、HashMap

發布時間:2024/7/5 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java面试知识点:集合、Set、泛型、HashSet、HashMap 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java面試知識點:集合、Set、泛型、HashSet、HashMap

答案:

1.集合


集合和數組的對比小結
? ? ?1,數組的長度是不可變的,集合的長度是可變的。
? ? ?2 ,數組可以存基本數據蟀和引用數據類型。
集合只能存引用數據類型,如野存基本數據類型,需要存對應的包裝類。


迭代器小結
? lterator<E> iterator()?:創建迭代器對象,默認指向當前集合的0索引。
? boolean hasNext()?:判斷當前位置是否有元素可以被取出
? E next()?:獲取當前位置的元素,將迭代器對象移向下一個索引位置
增強for循環
?增強for:簡化數組和Collection集合的遍歷
?? 它是JDK5之后出現的,其內部原理是一個Iterator迭代器
??實現Iterable接口的類才可以使用迭代器和增強for

package com.xy;import java.util.ArrayList; import java.util.Collection;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test01* @Author: 楊路恒* @Description:* @Date: 2021/8/18 0018 10:18* @Version: 1.0*/ public class test01 {public static void main(String[] args) {Collection<String> collection=new ArrayList<>();collection.add("楊大大");System.out.println(collection);collection.add("恒大大");collection.remove("恒大大");System.out.println(collection);collection.removeIf((String s)->{return s.length()<=3;});System.out.println(collection);} }package com.xy;import java.util.ArrayList; import java.util.Collection; import java.util.Iterator;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test02* @Author: 楊路恒* @Description:* @Date: 2021/8/18 0018 15:41* @Version: 1.0*/ public class test02 {public static void main(String[] args) {Collection<String> collection=new ArrayList<>();collection.add("楊大大");collection.add("恒大大");Iterator<String> iterator=collection.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}for (String s:collection) {System.out.println(s);}} }package com.xy;import java.util.LinkedList;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test03* @Author: 楊路恒* @Description:* @Date: 2021/8/18 0018 19:18* @Version: 1.0*/ public class test03 {public static void main(String[] args) {LinkedList<String> linkedList=new LinkedList<>();linkedList.add("楊大大");linkedList.add("恒大大");for (int i = 0; i < linkedList.size(); i++) {System.out.println(linkedList.get(i));}linkedList.addFirst("楊");linkedList.addLast("恒");for (String s : linkedList) {System.out.println(s);}System.out.println(linkedList.getFirst());System.out.println(linkedList.getLast());linkedList.remove(0);linkedList.removeFirst();linkedList.removeLast();for (int i = 0; i <linkedList.size() ; i++) {System.out.println(linkedList.get(i));}} }

2.Set集合


Set集合概述和特點
Set集合特點
? ?可以去除重復
? ?存取順序不一致
? ?沒有帶索引的方法,所以不能使用普通for循環遍歷,也不能通過獲取索引來獲取,刪除Set集合里面的元素


Set集合練習
? ?存儲字符串并遍歷

public class test04Set集合{public static void main(String[] args) {Set<String> strings=new TreeSet<>();strings.add("楊大大");strings.add("恒大大");Iterator<String> iterator = strings.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}for (String string : strings) {System.out.println(string);}TreeSet<Integer> treeSet=new TreeSet<>();treeSet.add(1);treeSet.add(2);treeSet.add(3);System.out.println(treeSet);} }


?

代碼如下:

public class Student implements Comparable<Student>{private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic int compareTo(Student o) {int i = this.age - o.age;i=i==0?this.name.compareTo(o.name):i;return i;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;if (age != student.age) return false;return name != null ? name.equals(student.name) : student.name == null;}@Overridepublic int hashCode() {int result = name != null ? name.hashCode() : 0;result = 31 * result + age;return result;} }public class test05Set集合 {public static void main(String[] args) {/*TreeSet<Student> treeSet=new TreeSet<>(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {//o1表示現在要存入的那個元素//o2表示已經存入到集合中的元素int i = o1.getAge() - o2.getAge(); //主要條件i=i==0?o1.getName().compareTo(o2.getName()):i; //次要條件return i;}});*/TreeSet<Student> treeSet=new TreeSet<>((Student o1, Student o2)->{int i = o1.getAge() - o2.getAge(); //主要條件i=i==0?o1.getName().compareTo(o2.getName()):i; //次要條件return i;});treeSet.add(new Student("楊大大",22));treeSet.add(new Student("恒大大",23));treeSet.add(new Student("路大大",23));System.out.println(treeSet);//不實現implements Comparable<Student>會報Exception in// thread "main" java.lang.ClassCastException:// com.xy.Student cannot be cast to java.lang.Comparable} }

3.泛型
(1)泛型概述
泛型:是JDK5中引入的特性,它提供了編譯時類型安全檢測機制,該機制允許在編譯時檢測到非法的類型 它的本質是參數化類型,也就是說所操作的數據類型被指定為一個參數
一提到參數,最熟悉的就是定義方法時有形參,然后調用此方法時傳遞實參。那么參數化類型怎么理解呢?
顧名思義,就是將類型由原來的具體的類型參數化,然后在使用/調用時傳入具體的類型
這種參數類型可以用在類、方法和接口中,分別被稱為泛型類、泛型方法、泛型接口

?代碼如下:

(1)泛型類:

package com.xy;public class Generic<T>{private T t;public T getT() {return t;}public void setT(T t) {this.t = t;} }public class test06泛型 {public static void main(String[] args) {TreeSet treeSet=new TreeSet();treeSet.add("楊大大"); // treeSet.add(6);Generic<String> generic=new Generic<>();generic.setT("楊大大");Generic<Integer> generic1=new Generic<>();generic1.setT(6);System.out.println(generic.getT());System.out.println(generic1.getT());Generic1<Integer> generic11=new Generic1Impl();generic11.show(6);} }

?

(2)泛型接口

public interface Generic1<T> {void show(T t); }public class Generic1Impl implements Generic1 {@Overridepublic void show(Object o) {} }public class test06泛型 {public static void main(String[] args) {TreeSet treeSet=new TreeSet();treeSet.add("楊大大"); // treeSet.add(6);Generic<String> generic=new Generic<>();generic.setT("楊大大");Generic<Integer> generic1=new Generic<>();generic1.setT(6);System.out.println(generic.getT());System.out.println(generic1.getT());Generic1<Integer> generic11=new Generic1Impl();generic11.show(6);} }

(3)

public class test07泛型 {public static void main(String[] args) {List<?> list=new ArrayList<Object>();List<?> list1=new ArrayList<Number>();List<?> list2=new ArrayList<Integer>();List<? extends Number> list3=new ArrayList<Number>();List<? extends Integer> list4=new ArrayList<Integer>();List<? super Number> list5=new ArrayList<Number>();List<? super Integer> list6=new ArrayList<Integer>();} }

?4.HashSet
哈希值
哈希值(哈希碼值):是JDK根據對象的地址或者屬性值,算出來的int類型的整數
Object類中有一個方法可以獲取對象的哈希值
? public int hashCode():根據對象的地址值計算出來的哈希值
對象的哈希值特點
? 如果沒有重寫hashCode方法,那么是根據對象的地址值計算出的哈希值。同一個對象多次調用hashCode()方法返回的哈希值是相同的不同對象的哈希值是不一樣的。
? 如果重寫了hashCode方法,一般都是通過對象的屬性值計算出哈希值。
? ? ? ? 如果不同的對象屬性值是一樣的,那么計算出來的哈希值也是一樣的。


HashSet1.7版本原理總結
? ? ?? 底層結構:哈希表。(數組+鏈表)
? ? ?? 數組的長度默認為16,加載因子為0.75
? ? ?? 首先會先獲取元素的哈希值,計算出在數組中應存入的索引
? ? ? 判斷該索引處是否為null
? ? ? 如果是null ,直接添加
? ? ? 如果不是null ,則與鏈表中所有的元素,通過equals方法比較屬性值 只要有一個相同,就不存,如果都不一樣,才會存入集合。
HashSet1.8版本原理解析
? ? 底層結構:哈希表。(數組、鏈表、紅黑樹的結合體)。
? ? 當掛在下面的元素過多,那么不利于添加,也不利于查詢,所以在JDK8以后, 當鏈表長度超過8的時候,自動轉換為紅黑樹。

代碼如下:

public class test09HashSet {public static void main(String[] args) {HashSet<String> hashSet=new HashSet<>();hashSet.add("楊大大");hashSet.add("恒大大");Iterator<String> iterator = hashSet.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}for (String s : hashSet) {System.out.println(s);}} }public class test10HashSet {public static void main(String[] args) {HashSet<Student> hashSet=new HashSet<>();hashSet.add(new Student("楊大大",22));hashSet.add(new Student("恒大大",23));hashSet.add(new Student("楊大大",22));/*** 結論:* 如果HashSet集合要存儲自定義對象,那么必須重寫hashCode和equals方法。*/for (Student student : hashSet) {System.out.println(student.hashCode());}} }

5.HashMap

代碼如下:

public class test11HashMap {public static void main(String[] args) {Map<String,String> map=new HashMap<>();map.put("1","楊大大");map.put("2","恒大大");System.out.println(map);System.out.println(map.get("1"));System.out.println(map.size());Set<String> strings = map.keySet();System.out.println(strings);Collection<String> values = map.values();System.out.println(values);for (String s : strings) {System.out.println(map.get(s));}} }public class test12HashMap {public static void main(String[] args) {Map<String,Student> map=new HashMap<>();map.put("1",new Student("楊大大",22));map.put("2",new Student("恒大大",22));Set<String> keySet = map.keySet();for (String s : keySet) {System.out.println(map.get(s));}Set<Map.Entry<String, Student>> entries = map.entrySet();for (Map.Entry<String, Student> entry : entries) {System.out.println(entry);}} }public class test13HashMap {public static void main(String[] args) {Map<Student,String> map=new HashMap<>();map.put(new Student("楊大大",22),"西安");map.put(new Student("恒大大",22),"上海");Set<Student> keySet = map.keySet();for (Student s : keySet) {System.out.println(map.get(s));}Set<Map.Entry<Student, String>> entries = map.entrySet();for (Map.Entry<Student, String> entry : entries) {System.out.println(entry);}map.put(new Student("恒大大",22),"上海");} }public class test14HashMap {public static void main(String[] args) {ArrayList<HashMap<String,String>> array=new ArrayList<>();HashMap<String,String> map=new HashMap<>();map.put("楊大大","22");map.put("恒大大","22");HashMap<String,String> map1=new HashMap<>();map1.put("楊大大","22");map1.put("恒大大","22");array.add(map);array.add(map1);for (HashMap<String, String> hashMap : array) {System.out.println(hashMap);}} }

總結

以上是生活随笔為你收集整理的Java面试知识点:集合、Set、泛型、HashSet、HashMap的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。