Stringbuilder与String的对象,在HashMap的get方法差异.

 

 Stringbuilder与String的对象,在HashMap的get方法差异.

 

引言:之前需要使用对象来跨越不同方法进行传递,结果返回后再map方法中始终无法获得到...

经过源码分析,是由于hash()方法导致的,使用StringBuilder对象即使值与String一致,产生的索引

也是不同的;

 

public class Test{ 

 

  public static void main(String[] args) {

 

    StringBuilder name=new StringBuilder();

 

    name.append("key");

 

    Map<String, Object> tempMap = new HashMap<String, Object>();

 

    tempMap.put("key","value");

 

      //StringBulider对象的"key"并未获取到值

      System.out.println(tempMap.get(name));//输出:null

       

      System.out.println(tempMap.get("key"));//输出:value

        

      //查看源码分析原因,是由hash方法导致的索引值指向不同位置,使得返回结果不一致;

      System.out.println(hash(name));//输出:1883673307

 

      //只需要将StringBuilder对象转换为String即可正确获取;

      System.out.println(hash(String.valueOf(name)));//输出:99486

 

      System.out.println(hash("key"));//输出:99486

        

  }

    

 /**

  *hash方法,源码不是static方法,此处加上方便测试

  **/

 static final int hash(Object k) {

   //源码中声明:transient int hashSeed = 0;  其初始值=0;且put方法未涉及此字段,所以还是0;

 

   int h = 0;  //int h = hashSeed;  

   if (0 != h && k instanceof String) {

       return sun.misc.Hashing.stringHash32((String) k);

   }

    h ^= k.hashCode();

    h ^= (h >>> 20) ^ (h >>> 12);

      return h ^ (h >>> 7) ^ (h >>> 4);

   }

 

}   

 

//-----------------------------------------源码分析:查看HashMap的get方法----------------------------------------------

public V get(Object key) {

      if (key == null)

         return getForNullKey();

      Entry<K,V> entry = getEntry(key); //获取值的方法

 

      return null == entry ? null : entry.getValue();

}

 

final Entry<K,V> getEntry(Object key) {

        if (size == 0) {

            return null;

        }

 

        int hash = (key == null) ? 0 : hash(key);//底层是table,索引值导致返回结果的不一致

        for (Entry<K,V> e = table[indexFor(hash, table.length)];

             e != null;

             e = e.next) {

            Object k;

            if (e.hash == hash &&

                ((k = e.key) == key || (key != null && key.equals(k))))

                return e;

        }

        return null;

}

 

 

final int hash(Object k) {

        int h = hashSeed;

        if (0 != h && k instanceof String) {

            return sun.misc.Hashing.stringHash32((String) k);

        }

 

        h ^= k.hashCode();

 

        h ^= (h >>> 20) ^ (h >>> 12);

        return h ^ (h >>> 7) ^ (h >>> 4);

}

 

static int indexFor(int h, int length) {

        // assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";

        return h & (length-1);

}

 

<select id="getdatatypeconfig" parameterType="java.util.HashMap" resultType="java.util.HashMap"> SELECT a.[datatypeconfig_colname] AS colName, b.datavalue_colvalue AS colValue FROM [JxBivAppUserDB].[dbo].[jxbivtob_settlement_datatypeconfig] a LEFT JOIN [JxBivAppUserDB].[dbo].[jxbivtob_settlement_datavalue] b ON a.datatypeconfig_id = b.datatypeconfig_id WHERE b.datafile_id=(SELECT TOP (1) [datafile_id] FROM [JxBivAppUserDB].[dbo].[jxbivtob_settlement_datafile] WHERE datatype_id=9 ORDER BY datafile_id DESC) AND b.datatype_id=(SELECT TOP (1) datatype_id FROM [JxBivAppUserDB].[dbo].[jxbivtob_settlement_datafile] WHERE datatype_id=9 ORDER BY datafile_id DESC) </select>public Map<String,Object> insert_fundsettlementdays(HttpServletRequest request, HttpServletResponse response){ response.setCharacterEncoding("UTF-8"); Map<String,Object> map =new HashMap<>(); List<HashMap> datatypeList=userDao.selectList("basessm.mapper.fundsettlementdays.getdatatypeconfig"); if (datatypeList == null || datatypeList.isEmpty()) { // 处理查询结果为空的情况 map.put("info", "No data available"); return map; } // 构建插入的列名和对应的 StringBuilder columnNames = new StringBuilder(); StringBuilder columnValues = new StringBuilder(); for (HashMap<String, Object> datatype : datatypeList) { String colName = (String) datatype.get("colName"); String colValue = (String) datatype.get("colValue"); columnNames.append(colName).append(","); columnValues.append("'").append(colValue).append("',"); } columnNames.deleteCharAt(columnNames.length() - 1); columnValues.deleteCharAt(columnValues.length() - 1); // 构建参数并调用插入方法 HashMap paramMap = new HashMap<>(); paramMap.put("columnNames", columnNames.toString()); paramMap.put("columnValues", columnValues.toString()); userDao.insert("basessm.mapper.fundsettlementdays.insert_fundsettlementdays",paramMap); map.put("info", "success"); return map; }需要datatypeList返回的每16条数据的colName跟jxbivtob_settlement_fundsettlementdays的列进行对比,如果相等那么就把colName对应的colValue插入到jxbivtob_settlement_fundsettlementdays表
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值