大数据Java基础第十二天作业
生活随笔
收集整理的這篇文章主要介紹了
大数据Java基础第十二天作业
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一題:HashMap內部實現原理
HashMap存的是key?=>?value?對的集合,每一對就是一個entry(條目),key和value存的都是對象的引用。key不能存重復的值,key的集合是keySet()。value可以存重復的值,value的集合是values()。
HashMap底層用的是hash(散列)算法,使的在map中查詢值速度快效率高。
HashMap判斷對象是否相等,先判斷hashcode是否相等,再判斷equals值是否相等。對象相等代碼:(this.hashcode()?==?obj.hashcode()?&&?(this?==?obj?||?this.equals(obj)))第二題:HashSet和HashMap區別
HashSet:實現Collection接口,只存value的集合,用的hash算法存儲,value的值不能重復。
HashMap:實現Map接口,存的key=>value的集合,用的hash算法存儲,key的值不能重復,value值可以重復。第三題:
import?java.util.Map;
import?java.util.HashMap;
import?java.util.Map.Entry;public?class?Student?{public?static?void?main(String[]?args)?{Map<Integer,Map?<Integer,String>>?classes?=?new?HashMap<Integer,Map?<Integer,String>>();//往里添加數據Map<Integer,String>?student?=?null;int?num?=?1;for(int?i=1;i<=10;i++){student?=?new?HashMap<Integer,String>();classes.put(i,student);for(int?j=1;j<=50;j++){student.put(j,"Tom"?+?num);num?++;}}//使用Entryfor(Entry<Integer,Map<Integer,String>>?class_entry?:?classes.entrySet()){Map<Integer,String>?students?=?class_entry.getValue();for(Entry<Integer,String>?student_entry?:?students.entrySet()){System.out.println("班級:"?+?class_entry.getKey()?+?"班,學號:"?+?student_entry.getKey()?+?"號,姓名:"?+?student_entry.getValue());}}System.out.println("--------------------------------------------------------------");//使用keySetfor(Integer?class_num:classes.keySet()){Map<Integer,String>?students?=?classes.get(class_num);for(Integer?student_num:students.keySet()){System.out.println("班級:"?+?class_num?+?"班,學號:"?+?student_num?+?"號,姓名:"?+?students.get(student_num));}}}
}
第四題:
import?java.io.IOException;
import?java.io.FileReader;
import?java.io.FileWriter;class?IOWrite{public?static?void?main(String[]?args)?throws?IOException{int?i?=?0;FileReader?out?=?new?FileReader("d:/index1.txt");FileWriter?in?=?new?FileWriter("d:/index2.txt",true);char[]?buffer?=?new?char[1024];int?len?=?0;while((len?=?out.read(buffer))?!=?-1){in.write(buffer,0,len);}in.close();out.close();}
}
轉載于:https://blog.51cto.com/senlinmin/1782473
總結
以上是生活随笔為你收集整理的大数据Java基础第十二天作业的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cglib源码学习交流
- 下一篇: 调试4