版权声明:此文章转载自51CTO
如需转载请联系听云College团队成员小尹 邮箱:yinhy#tingyun.com
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文件的reader reader = new FileReader(srcFile); // 写入tar文件的fileWriter writer = 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) { } } } } 1 <br>