zoukankan      html  css  js  c++  java
  • 集合框架

    集合框架:

                            

    1.总结起来:List、Set、Map

                                           

              另外:

                                             

    2. 集合的操作的工具类:Arrays、Collections。

                   

                               

    3.常用集合类:HashSet(LinkedHashSet子类)   ArrayList (LinkedList)  HashMap(Hashtable、LinkedHashMap子类)

    (1)ArrayList:  List  list   =   new  ArrayList();

    (2)HashMap使用:put进去时,如果存在key,则替换value,如果不存在Key,则新建一个k-v,而且HashMap可以接受为null的键值(key)和值(value)

                 我们能否让HashMap同步?HashMap可以通过下面的语句进行同步:

                     Map m = Collections.synchronizeMap(hashMap);

        另外关于   代替hashtable 的 ConcurrentHashMap   参考: http://www.importnew.com/21388.html

      (

    总结:HashMap的实现原理:

    1. 利用key的hashCode重新hash计算出当前对象的元素在数组中的下标
    2. 存储时,如果出现hash值相同的key,此时有两种情况。(1)如果key相同,则覆盖原始值;(2)如果key不同(出现冲突),则将当前的key-value放入链表中
    3. 获取时,直接找到hash值对应的下标,在进一步判断key是否相同,从而找到对应值。
    4. 理解了以上过程就不难明白HashMap是如何解决hash冲突的问题,核心就是使用了数组的存储方式,然后将冲突的key的对象放入链表中,一旦发现冲突就在链表中做进一步的对比。

     1 package com.wang.map;
     2 
     3 import java.util.HashMap;
     4 import java.util.Iterator;
     5 import java.util.Map.Entry;
     6 import java.util.Set;
     7 
     8 public class TestHashMap {
     9 
    10     /**
    11      * @param args
    12      */
    13     public static void main(String[] args) {
    14         //先将字符串放到一个char数组中去
    15         String str = "ababcd";
    16         char[] ch = str.toCharArray();
    17         HashMap<Character, Integer> map = new HashMap<>();
    18 
    19         int count = 0;
    20         //遍历数组,判断char数组中的元素重复情况
    21         for (int i = 0; i < ch.length; i++) {
    22             Integer value = map.get(ch[i]);
    23             // 判断map中是否存在某值
    24             if (value != null) {
    25                 // 存在的话,返回value,将value+1后赋值给该key的值
    26                 count = value;
    27             }
    28             count++;
    29             // 不存在的话,将该值put进map,并且赋值1,
    30             map.put(ch[i], count);
    31             count = 0;
    32         }
    33         //System.out.println(map);
    34          
    35         //hashMap的entrySet()方法,将map中的数据存在set集合中,从而便于使用iteration遍历
    36         Set<Entry<Character,Integer>> set = map.entrySet();
    37         Iterator<Entry<Character, Integer>> it = set.iterator();
    38         while (it.hasNext()) {
    39             Entry<Character, Integer> next = it.next();
    40             Character k = next.getKey();
    41             Integer v = next.getValue();
    42             System.out.println(k + "(" + v + ")");
    43         }
    44     }
    45 
    46 }

    --------------------------------------

    d(1)
    b(2)
    c(1)
    a(2)

     -------------------------------------

     HashSet:不允许重复,其中对象的HashCode值决定了在哈希表中的存储位置

                  

         

     1 package set;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 
     7 class Student{
     8     private int sn;
     9     private String name;
    10     private int age;
    11     public Student(int sn, String name, int age) {
    12         super();
    13         this.sn = sn;
    14         this.name = name;
    15         this.age = age;
    16     }
    17     public String toString() {
    18         return "Student [sn=" + sn + ", name=" + name + ", age=" + age + "]";
    19     }
    20     public int hashCode() {
    21         final int prime = 31;
    22         int result = 1;
    23         result = prime * result + sn;
    24         return result;
    25     }
    26     public boolean equals(Object obj) {
    27         if (this == obj)
    28             return true;
    29         if (obj == null)
    30             return false;
    31         if (getClass() != obj.getClass())
    32             return false;
    33         Student other = (Student) obj;
    34         if (sn != other.sn)
    35             return false;
    36         return true;
    37     }
    38     
    39     
    40 }
    41 public class HashSetDemo {
    42 
    43     /**
    44      * @param args
    45      */
    46     public static void main(String[] args) {
    47         Set<Student> stu = new HashSet<>();
    48         stu.add(new Student(1,"zhangsan",17));
    49         stu.add(new Student(1,"zhangsan",17));
    50         stu.add(new Student(3,"lisi",18));
    51         stu.add(new Student(4,"lisi",18));
    52         System.out.println(stu.size());
    53         System.out.println(stu.toString());
    54     }
    55 
    56 }

     ------------------------------------------------------

    3
    [Student [sn=3, name=lisi, age=18], Student [sn=4, name=lisi, age=18], Student [sn=1, name=zhangsan, age=17]]

    --------------------------------------------------------

  • 相关阅读:
    K近邻法
    感知机
    统计学习方法概论
    神经网络的学习 Neural Networks learing
    对线性回归、逻辑回归、各种回归的概念学习
    从零开始创建VUE项目
    Java中Log4j.properties配置文件详解
    Java中Log4j的入门实例
    Java中Log4j的基本使用方法说明
    C#与Java的RSA中的X509EncodedKeySpec、PKCS8EncodedKeySpec
  • 原文地址:https://www.cnblogs.com/wang--lei/p/7289279.html
Copyright © 2011-2022 走看看