D18
2016-03-31 13:57
190 查看
1、获取功能
2、 1:Hashtable和HashMap的区别?
Hashtable:线程安全,效率低。不允许null键和null值
HashMap:线程不安全,效率高。允许null键和null值
3、 List,Set,Map等接口是否都继承子Map接口?
List,Set不是继承自Map接口,它们继承自Collection接口
Map接口本身就是一个顶层接口
4、Collection和Collections的区别?
Collection:是单列集合的顶层接口,有子接口List和Set。
Collections:是针对集合操作的工具类,有对集合进行排序和二分查找的方法
键找值 public static void main(String[] args) { // 创建集合对象 Map<String, String> map = new HashMap<String, String>(); // 创建元素并添加元素 map.put("邓超", "孙俪"); map.put("黄晓明", "杨颖"); map.put("周杰伦", "蔡依林"); map.put("刘恺威", "杨幂"); // V get(Object key):根据键获取值 System.out.println("get:" + map.get("周杰伦")); System.out.println("get:" + map.get("周杰")); // 返回null System.out.println("----------------------"); // Set<K> keySet():获取集合中所有键的集合 Set<String> set = map.keySet(); for (String key : set) { System.out.println(key); } System.out.println("----------------------"); // Collection<V> values():获取集合中所有值的集合 Collection<String> con = map.values(); for (String value : con) { System.out.println(value); } }
根据键值对对象获取键和值 public static void main(String[] args) { // 创建集合对象 Map<String, String> map = new HashMap<String, String>(); // 创建元素并添加到集合 map.put("杨过", "小龙女"); map.put("郭靖", "黄蓉"); map.put("杨康", "穆念慈"); map.put("陈玄风", "梅超风"); // 获取所有键值对对象的集合 Set<Map.Entry<String, String>> set = map.entrySet(); // 遍历键值对对象的集合,得到每一个键值对对象 for (Map.Entry<String, String> me : set) { // 根据键值对对象获取键和值 String key = me.getKey(); String value = me.getValue(); System.out.println(key + "---" + value); } }
<pre name="code" class="java"><pre name="code" class="java">根据键值对对象获取键和值---------------------- public static void main(String[] args) { // 创建集合对象 HashMap<String, Student> hm = new HashMap<String, Student>(); // 创建学生对象 Student s1 = new Student("周星驰", 58); Student s2 = new Student("刘德华", 55); Student s3 = new Student("梁朝伟", 54); Student s4 = new Student("刘嘉玲", 50); // 添加元素 hm.put("9527", s1); hm.put("9522", s2); hm.put("9524", s3); hm.put("9529", s4); // 遍历 Set<String> set = hm.keySet(); for (String key : set) { // 注意了:这次值不是字符串了 // String value = hm.get(key); Student value = hm.get(key); System.out.println(key + "---" + value.getName() + "---" + value.getAge()); } }
/* * HashMap:是基于哈希表的Map接口实现。 * 哈希表的作用是用来保证键的唯一性的。 * * HashMap<String,String> * 键:String * 值:String */ public static void main(String[] args) { // 创建集合对象 HashMap<String, String> hm = new HashMap<String, String>(); // 创建元素并添加元素 // String key1 = "it001"; // String value1 = "马云"; // hm.put(key1, value1); hm.put("it001", "马云"); hm.put("it003", "马化腾"); hm.put("it004", "乔布斯"); hm.put("it005", "张朝阳"); hm.put("it002", "裘伯君"); // wps hm.put("it001", "比尔盖茨"); // 遍历 Set<String> set = hm.keySet(); for (String key : set) { String value = hm.get(key); System.out.println(key + "---" + value); } }
* * LinkedHashMap:是Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。 * 由哈希表保证键的唯一性 * 由链表保证键盘的有序(存储和取出的顺序一致) */ public static void main(String[] args) { // 创建集合对象 LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>(); // 创建并添加元素 hm.put("2345", "hello"); hm.put("1234", "world"); hm.put("3456", "java"); hm.put("1234", "javaee"); hm.put("3456", "android"); // 遍历 Set<String> set = hm.keySet(); for (String key : set) { String value = hm.get(key); System.out.println(key + "---" + value); } }
/* * TreeMap:是基于红黑树的Map接口的实现。 * * HashMap<String,String> * 键:String * 值:String */ public static void main(String[] args) { // 创建集合对象 TreeMap<String, String> tm = new TreeMap<String, String>(); // 创建元素并添加元素 tm.put("hello", "你好"); tm.put("world", "世界"); tm.put("java", "爪哇"); tm.put("world", "世界2"); tm.put("javaee", "爪哇EE"); // 遍历集合 Set<String> set = tm.keySet(); for (String key : set) { String value = tm.get(key); System.out.println(key + "---" + value); } }
<pre name="code" class="java">public static void main(String[] args) { // 创建集合对象 TreeMap<Student, String> tm = new TreeMap<Student, String>( new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { // 主要条件 int num = s1.getAge() - s2.getAge(); // 次要条件 int num2 = num == 0 ? s1.getName().compareTo( s2.getName()) : num; return num2; } }); // 创建学生对象 Student s1 = new Student("潘安", 30); Student s2 = new Student("柳下惠", 35); Student s3 = new Student("唐伯虎", 33); Student s4 = new Student("燕青", 32); Student s5 = new Student("唐伯虎", 33); // 存储元素 tm.put(s1, "宋朝"); tm.put(s2, "元朝"); tm.put(s3, "明朝"); tm.put(s4, "清朝"); tm.put(s5, "汉朝"); // 遍历 Set<Student> set = tm.keySet(); for (Student key : set) { String value = tm.get(key); System.out.println(key.getName() + "---" + key.getAge() + "---" + value); }}
嵌套集合 public static void main(String[] args) { // 创建集合对象 HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>(); // 创建元素集合1 ArrayList<String> array1 = new ArrayList<String>(); array1.add("吕布"); array1.add("周瑜"); hm.put("三国演义", array1); // 创建元素集合2 ArrayList<String> array2 = new ArrayList<String>(); array2.add("令狐冲"); array2.add("林平之"); hm.put("笑傲江湖", array2); // 创建元素集合3 ArrayList<String> array3 = new ArrayList<String>(); array3.add("郭靖"); array3.add("杨过"); hm.put("神雕侠侣", array3); //遍历集合 Set<String> set = hm.keySet(); for(String key : set){ System.out.println(key); ArrayList<String> value = hm.get(key); for(String s : value){ System.out.println("\t"+s); } } }
需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1) public static void main(String[] args) { // 定义一个字符串(可以改进为键盘录入) Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串:"); String line = sc.nextLine(); // 定义一个TreeMap集合 TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>(); //把字符串转换为字符数组 char[] chs = line.toCharArray(); //遍历字符数组,得到每一个字符 for(char ch : chs){ //拿刚才得到的字符作为键到集合中去找值,看返回值 Integer i = tm.get(ch); //是null:说明该键不存在,就把该字符作为键,1作为值存储 if(i == null){ tm.put(ch, 1); }else { //不是null:说明该键存在,就把值加1,然后重写存储该键和值 i++; tm.put(ch,i); } } //定义字符串缓冲区变量 StringBuilder sb= new StringBuilder(); //遍历集合,得到键和值,进行按照要求拼接 Set<Character> set = tm.keySet(); for(Character key : set){ Integer value = tm.get(key); sb.append(key).append("(").append(value).append(")"); } //把字符串缓冲区转换为字符串输出 String result = sb.toString(); System.out.println("result:"+result); }
2、 1:Hashtable和HashMap的区别?
Hashtable:线程安全,效率低。不允许null键和null值
HashMap:线程不安全,效率高。允许null键和null值
3、 List,Set,Map等接口是否都继承子Map接口?
List,Set不是继承自Map接口,它们继承自Collection接口
Map接口本身就是一个顶层接口
4、Collection和Collections的区别?
Collection:是单列集合的顶层接口,有子接口List和Set。
Collections:是针对集合操作的工具类,有对集合进行排序和二分查找的方法
5、Collections可以针对ArrayList存储基本包装类的元素排序,存储自定义对象可不可以排序呢 public static void main(String[] args) { // 创建集合对象 List<Student> list = new ArrayList<Student>(); // 创建学生对象 Student s1 = new Student("林青霞", 27); Student s2 = new Student("风清扬", 30); Student s3 = new Student("刘晓曲", 28); Student s4 = new Student("范冰冰", 29); Student s5 = new Student("林青霞", 27); // 添加元素对象 list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); // 排序 // 自然排序 // Collections.sort(list); // 比较器排序 // 如果同时有自然排序和比较器排序,以比较器排序为主 Collections.sort(list, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int num = s2.getAge() - s1.getAge(); int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num; return num2; } }); // 遍历集合 for (Student s : list) { System.out.println(s.getName() + "---" + s.getAge()); } }
/* * 模拟斗地主洗牌和发牌 * * 分析: * A:创建一个牌盒 * B:装牌 * C:洗牌 * D:发牌 * E:看牌 * 思路: * A:创建一个HashMap集合 * B:创建一个ArrayList集合 * C:创建花色数组和点数数组 * D:从0开始往HashMap里面存储编号,并存储对应的牌 * 同时往ArrayList里面存储编号即可。 * E:洗牌(洗的是编号) * F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收) * G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌) */ <pre name="code" class="java">public class DouDiZhu { public static void main(String[] args) { // 装牌集合 HashMap<Integer, String> mp = new HashMap<Integer, String>(); // 装序号 ArrayList<Integer> list = new ArrayList<Integer>(); // 装花色 String[] colors = { "♦", "♣", "♥", "♠" }; // 装点数 String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", }; // 发牌集合 TreeSet<Integer> al1 = new TreeSet<Integer>(); TreeSet<Integer> al2 = new TreeSet<Integer>(); TreeSet<Integer> al3 = new TreeSet<Integer>(); TreeSet<Integer> al4 = new TreeSet<Integer>(); // 装牌 int index = 0; for (String num : numbers) { for (String col : colors) { mp.put(index, col.concat(num)); list.add(index); index++; } } mp.put(index, "小王"); list.add(index); index++; mp.put(index, "大王"); list.add(index); // 洗牌 Collections.shuffle(list); // 发牌 for (int i = 0; i < list.size(); i++) { if (i < list.size() - 3) { if (i % 3 == 0) { al1.add(list.get(i)); } else if (i % 3 == 1) { al2.add(list.get(i)); } else if (i % 3 == 2) { al3.add(list.get(i)); } } else { al4.add(list.get(i)); } } //看牌 System.out.println(looks("刘备",al1,mp)); System.out.println(looks("关羽",al2,mp)); System.out.println(looks("张飞",al3,mp)); System.out.println(looks("底牌",al4,mp)); } //看牌方法 public static String looks(String str,TreeSet<Integer> set,HashMap<Integer,String> hm){ StringBuilder sb=new StringBuilder(str+":{"); for(Integer st:set){ sb.append(hm.get(st)); sb.append(" "); } sb.append("}"); return sb.toString(); } }
相关文章推荐
- centos 7.0下关闭&启动防火墙
- 学习编程小技巧
- 两分钟彻底让你明白Android Activity生命周期(图文)!
- 原已经安装好的nginx,现在需要添加一个未被编译安装的模块:
- Oracle相关账户几个语句
- android 图片轮播控件 Android-ConvenientBanner简单使用
- caffe cudnn出现错误: declaration is incompatible with "const char *cudnnGetErrorString(cudnnStatus_t)"
- 职场35岁现象:各位小伙伴接近了吗?!
- PID连续控制算法的表达式以及C语言实现
- Android自定义类似ProgressDialog效果的Dialog
- iOS conformsToProtocol
- memcached搭建缓存系统
- Java 多线程 synchronized关键字详解
- java操作cookie
- iOS基本UI控件总结
- iOS 去掉tableViewCell 点击效果 三个简单方法
- C#中偏移量和长度超出数组的界限的问题
- IT十八掌作业_java基础第23天_数据库连接池\反射
- NodeJs操作mysql数据库(coffee版)
- 【C++】《C++标准程序库》小结第十三章-stream(1)