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
2.Set集合
Set集合概述和特點
Set集合特點
? ?可以去除重復
? ?存取順序不一致
? ?沒有帶索引的方法,所以不能使用普通for循環遍歷,也不能通過獲取索引來獲取,刪除Set集合里面的元素
Set集合練習
? ?存儲字符串并遍歷
?
代碼如下:
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 专家:人工智能开始对现实世界产生重大影响
- 下一篇: Java面试知识点:多态、内部类