zoukankan      html  css  js  c++  java
  • Java 集合-set集合,Map接口

      一、set集合

      1、特点:怎么存不一定怎么取。没有下标。不可重复。

        2、set集合的add()会默认调用改对象类型的equals方法和hashCode方法。进行判断,如果哈希码不同则存入,如果相同调用equals判断值是否相同,如果相同终止,不同则存储。

          注意:自定义存储类型的类必须重写 equals方法和hashCode方法。

       3、HashSet 集合 是有 数组和链表 数据结构 组合成的 (哈希表)

         4、HashSet  中的方法:add():追加指定元素

                  clear():移除集合中所有元素

                  contains(Object o) :查找指定元素  返回 Boolean

                  isEmpty() :判断是不是空集合,则返回 Boolean。

                  remove(Object o) : 移除指定元素,返回Boolean。

                  iterator() :返回对此 set 中元素进行迭代的迭代器。返回 iterator 对象

       5、LinkedHashSet 是由  链表 和 哈希表 组合而成 (双重链表)特点:有顺序,怎么存怎么取

       二、Map接口

      1、键值对集合

        2、该类是接口不能实例化用其实现类 HashMap 和  LinkedHashMap 类

        3、定义:Map<K, V> map = new HashMap<K,V>();

        4、常用方法:get(Object key):根据key 获取 value ;

              put(Object key,Object value):添加key 和 value

              remove(Object key):通过key 删除value 

       5、遍历 Map 集合 的四种方式

        

     1 public static void main(String[] args) {
     2         Map<String, Integer> map = new HashMap<String, Integer>();
     3         map.put("a", 1);
     4         map.put("d", 2);
     5         map.put("b", 2);
     6         map.put("a", 1);
     7         // 获取元素 根据key获取value
     8         System.out.println(map.get("a"));
     9         System.out.println(map.get("b"));
    10         System.out.println(map.get("d"));
    11         map.remove("a");
    12         System.out.println(map);
    13         System.out.println();
    14         // 遍历
    15 
    16         // 方法一 增强for
    17         // 1、首先获取key所在的Set集合
    18         // Set<String> set = map.keySet();
    19         // // 2、遍历所有key所在的set集合取到每一个key
    20         // for (String str : set) {
    21         // System.out.println(str + "..." + map.get(str));
    22         // }
    23 
    24         // 方法二 迭代器
    25         // 1、首先获取key所在的Set集合
    26         // Set<String> set = map.keySet();
    27         // Iterator<String> it = set.iterator();
    28         // while (it.hasNext()) {
    29         // String key = it.next();
    30         // System.out.println(key + "..." + map.get(key));
    31         // }
    32 
    33         // 方法三 Map.Entry 加增强 for
    34         // Set<Map.Entry<String, Integer>> set = map.entrySet();
    35         // for (Map.Entry<String, Integer> str : set) {
    36         // System.out.println(str.getKey() + "..." + str.getValue());
    37         // }
    38 
    39         // 方法四   Map.Entry 加 迭代器
    40         // Set<Map.Entry<String, Integer>> set = map.entrySet();
    41         // Iterator<Map.Entry<String, Integer>> it = set.iterator();
    42         // while (it.hasNext()) {
    43         // Entry<String, Integer> str = it.next();
    44         // System.out.println(str.getKey() + "..." + str.getValue());
    45         // }
    46     }

         

  • 相关阅读:
    Codeforces Round #325D (Div. 2) (DP)
    Codeforces Round #382 (Div. 2) (模拟|数学)
    HDU5950-Recursive sequence(矩阵快速幂)
    9. javacript高级程序设计-客户端检测
    8. javacript高级程序设计-BOM
    7. javacript高级程序设计- 函数表达式
    6. javacript高级程序设计-面向对象设计
    Js注释
    5. javacript高级程序设计-引用类型
    4. javacript高级程序设计-变量、作用域和内存问题
  • 原文地址:https://www.cnblogs.com/yanghaoyu0624/p/11604970.html
Copyright © 2011-2022 走看看