ArrayList和HashSet
生活随笔
收集整理的這篇文章主要介紹了
ArrayList和HashSet
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
眾所周知:List是接口,ArrayList實現(xiàn)了List接口。那HashSet又是什么玩意?
ArrayList實現(xiàn)了List接口,HashSet實現(xiàn)了Set接口,List和Set都是繼承Collection接口。
ArrayList底層是動態(tài)數(shù)組,HashSet底層是哈希表。
ArrayList存放的是對象的引用,HashSet存放之前檢索對象的HashCode,所以當存入對象時要重寫hashCode(),如果只是比較對象,只需要重寫equals()方法,
ArrayList是有序可重復,HashSet是無序不可重復。
對于 HashSet 而言,它是基于 HashMap 實現(xiàn)的,底層采用 HashMap 來保存元素
import java.util.ArrayList; import java.util.Collection; import java.util.HashSet;public class UseSetAppMain {public static void main(String[] args) {printCollection(addElementsToCollection(new HashSet()));/*輸出class java.util.HashSet中的元素,共5個總結
以上是生活随笔為你收集整理的ArrayList和HashSet的全部內容,希望文章能夠幫你解決所遇到的問題。