集合(Collection)
1.List集合子类的特点
List集合常用子类:ArrayList,LinkedList;
(1)ArrayList:底层数据结构是数组,查询快,增删慢;
(2)LinkedList:底层 数据结构是链表,查询慢,增删快;
2.哈希值
(1)哈希值:是JDK根据对象的地址或者字符串或者数字算出来的int类型的数值;
(2)Objetc类中有一个方法可以获取对象的哈希值:public int hashCode():返回对象的哈希码值;
(3)对象的哈希值特点:
●同一个对象多次调用hashCode0方法返回的哈希值是相同的;
●默认情况下, 不同对象的哈希值是不同的。而重写hashCode0方法,可以实现让不同对象的哈希值相同;
3.HashSet集合概述和特点
(1)HashSet集合特点
(2)底层数据结构是哈希表
(3)对集合的迭代顺序不作任何保证,也就是说不保证存储和取出的元素顺序一致
(4)没有带索引的方法,所以不能使用普通for循环遍历
由于是Set集合, 所以是不包含重复元素的集合
4.遍历的三种方式
package itheima_02;
import java. util. ArrayList;
import java. util. Iterator;
import java. util. List;
public class ListDemo {
public static void main ( String[ ] args) {
List< Student> list = new ArrayList < > ( ) ;
Student s1 = new Student ( "小江" , 20 ) ;
Student s2 = new Student ( "小琴" , 22 ) ;
Student s3 = new Student ( "小艳" , 24 ) ;
list. add ( s1) ;
list. add ( s2) ;
list. add ( s3) ;
Iterator< Student> it = list. iterator ( ) ;
while ( it. hasNext ( ) ) {
Student s = it. next ( ) ;
System. out. println ( s. getName ( ) + "," + s. getAge ( ) ) ;
}
System. out. println ( "------" ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
Student s = list. get ( i) ;
System. out. println ( s. getName ( ) + "," + s. getAge ( ) ) ;
}
System. out. println ( "------" ) ;
for ( Student s : list) {
System. out. println ( s. getName ( ) + "," + s. getAge ( ) ) ;
}
}
}