您的位置:首页 > 编程语言 > Java开发

java基础第十二天_集合

2016-06-12 20:23 447 查看
1.描述HashMap内部实现原理。HashMap存的是K-V对,K是唯一的不重复的。在存储方式上hashmap底层实现了一个散列算法,散列是一种基于关键词的搜索算法,提升了hashmap的查找速度。hashmap的查找机制是先用对象的hashcode得出一个地址用equals比较地址中的链表的各个元素,如果形同,取出对应的value值。2.描述Hashset和HashMap的区别。HashSet:HashSet集合内部是通过HashMap进行实现的,使用的是hashMap中key部分。实现set接口,set继承collection接口。HashMap:实现Map接口,Map接口与collection接口是同级的。它们都具有不重复的特点,采用hash机制进行存储。3.年级的集合使用Map的嵌套实现。 10班,每个班50人。import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;/*** 年级的集合使用Map的嵌套实现。 10班,每个班50人。** @author admin**/public class Map_嵌套循环作业1 {public static void main(String[] args) {// 创建班级号Map<Integer, Map<String, String>> classes = new HashMap<Integer, Map<String, String>>();// 创建班级的名单集合Map<String, String> names = null;int no = 1;for (int i = 1; i <= 10; i++) {names = new HashMap<String, String>();classes.put(i, names);// 将班级名单放入班级号集合中for (int j = 1; j <= 50; j++) {names.put("学号" + j, "Marry" + no);no++;}}// 读取map嵌套循环for (Entry<Integer, Map<String, String>> entry : classes.entrySet()) {Integer key = entry.getKey();Map<String, String> values = entry.getValue();for (Entry<String, String> entry0 : values.entrySet()) {// 取学号String classNum = entry0.getKey();// 取名字String nameSet = entry0.getValue();System.out.println(key + "." + classNum + "," + nameSet);}}}}4.编程实现文本文件的复制。合理设计程序,得到缓冲区的大小的高效区间。 提示缓冲区设置1k开始,不超过10M。/*** 4.编程实现文本文件的复制。合理设计程序,得到缓冲区的大小的高效区间。 提示缓冲区设置1k开始,不超过10M。** @author admin**/public class copy_作业 {public static void main(String[] args) {// 获取系统属性String str = System.getProperty("line.separator");System.out.println(str);String srcFile = "d:/aa.txt";String tarFile = "d:/bb.txt";FileReader reader = null;FileWriter writer = null;try {// 读取src文件的readerreader = new FileReader(srcFile);// 写入tar文件的fileWriterwriter = new FileWriter(tarFile, false);// 定义字符缓冲区char[] buf = new char[1024];int len = 0;while ((len = reader.read(buf)) != -1) {writer.write(buf, 0, len);}System.out.println("over");} catch (Exception e) {e.printStackTrace();} finally {try {if (reader != null) {reader.close();}if (writer != null) {writer.close();}} catch (Exception e2) {}}}}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件 读入 写出