hashCode就是对象的散列码,是根据对象的某些信息推导出的一个整数值,默认情况下表示是对象的存储地址。通过散列码,可以提高检索的效率,主要用于在散列存储结构中快速确定对象的存储地址,如Hashtable、hashMap中。
import java.util.ArrayList;
import java.util.List;
/**
*
* @title: 获取hashCode
* @author: wll
* @since: 2021-11-25 13:56:23
*/
public class TestHashCode {
public static void main(String[] args) {
List<String> hashList = new ArrayList<String>();
for(int i=0;i<10;i++){hashList.add(i+" 3");}for(int i=0;i<hashList.size();i++){System.out.print(hashList.get(i).hashCode()+" ");}hashList.clear();System.out.println();
for(int i=0;i<10;i++){hashList.add(i+" 4");}for(int i=0;i<hashList.size();i++){System.out.print(hashList.get(i).hashCode()+" ");}hashList.clear();System.out.println();
for(int i=0;i<10;i++){hashList.add(i+" 5");}for(int i=0;i<hashList.size();i++){System.out.print(hashList.get(i).hashCode()+" ");}hashList.clear();System.out.println();
for(int i=0;i<10;i++){hashList.add(i+" 6");}for(int i=0;i<hashList.size();i++){System.out.print(hashList.get(i).hashCode()+" ");}hashList.clear();System.out.println();
for(int i=0;i<10;i++){hashList.add(i+" 7");}for(int i=0;i<hashList.size();i++){System.out.print(hashList.get(i).hashCode()+" ");}hashList.clear();System.out.println();
for(int i=0;i<10;i++){hashList.add(i+" 8");}for(int i=0;i<hashList.size();i++){System.out.print(hashList.get(i).hashCode()+" ");}hashList.clear();System.out.println();
for(int i=0;i<10;i++){hashList.add(i+" 9");}for(int i=0;i<hashList.size();i++){System.out.print(hashList.get(i).hashCode()+" ");}hashList.clear();System.out.println();
}
}
数据结果:
47171 48132 49093 50054 51015 51976 52937 53898 54859 55820
47172 48133 49094 50055 51016 51977 52938 53899 54860 55821
47173 48134 49095 50056 51017 51978 52939 53900 54861 55822
47174 48135 49096 50057 51018 51979 52940 53901 54862 55823
47175 48136 49097 50058 51019 51980 52941 53902 54863 55824
47176 48137 49098 50059 51020 51981 52942 53903 54864 55825
47177 48138 49099 50060 51021 51982 52943 53904 54865 55826