日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java map byte[],java中byte数组不能作为map的key使用

發布時間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java map byte[],java中byte数组不能作为map的key使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天在使用java寫代碼的時候,用byte數組作為map的key來使用,發現在遍歷的時候get到之前傳進去的值總是為空,很是困惑,后來查了下資料發現java中的字節數組不能直接作為map的key來使用.

/**

* 找到當前區塊鏈中所有UTXO

*/

public List findAllUtxos(String sendAddress) {

//遍歷所有的區塊,找出交易發起人能夠解鎖的所有沒有花費的交易輸出

List utxoInfos = new ArrayList<>();

Iterator blockIterator = new Iterator(tailHash);

//byte[]不能直接作為map的key使用,所以需要將txid轉換成string才能使用

Map> spentOutputs = new HashMap<>();

for (; ; ) {

Block block = blockIterator.getBlock();

for (Transaction tx : block.transactions) {

System.out.println("tag2:outputs length is " + tx.outputs.length + " tx id is " + new BigInteger(tx.id).toString(16));

ScanTransaction:

//遍歷所有的交易輸出

for (int i = 0; i < tx.outputs.length; i++) {

//如果發現該交易已經有被引用的交易輸出,就開始判斷

List idList = spentOutputs.get(new String(tx.id));

if (idList != null) {

System.out.println(idList.toString());

for (Integer index : idList) {

if (i == index) {

System.out.println("find spent output,continue scan next input");

continue ScanTransaction;

}

}

}

//如果交易發起人能使用,將其添加進去

TxOutput output = tx.outputs[i];

if (output.canUnlockUTXOWith(sendAddress)) {

UtxoInfo utxoInfo = new UtxoInfo();

utxoInfo.id = tx.id;

utxoInfo.index = i;

utxoInfo.output = output;

utxoInfos.add(utxoInfo);

}

}

//遍歷所有的交易輸入,找到已經花費掉的,每次循環都創建一個List對象

List spentTxIds = new ArrayList<>();

if (!tx.isCoinBase()) {

for (int i = 0; i < tx.inputs.length; i++) {

TxInput input = tx.inputs[i];

if (input.canUnlockWith(sendAddress)) {

spentTxIds.add(i);

//一定要注意這里的txid是輸入中引用的交易id,不是本次循環的txid

System.out.println("find used utxo,tx id is " + new BigInteger(input.txId).toString(16) + "index is " + i);

spentOutputs.put(new String(input.txId), spentTxIds);

}

}

}

}

if (block.prevHash == null || block.prevHash.length == 0) {

break;

}

}

return utxoInfos;

}

原因是這樣的,當使用byte[]作為key的時候,map會對這個字節數組的地址進行hashcode得到一個值作為key,而不是以內容作為它的key,所以兩次byte數組地址不一樣的話,得到的結果就會完全不同.

有三種解決方案:

Wrapping in a String, but then you have to be careful about encoding issues (you need to make certain that the byte -> String -> byte gives you the same bytes).

Use List(can be expensive in memory).

Do your own wrapping class, writing hashCode and equals to use the contents of the byte array.

分別是將byte轉成字符串String,我在代碼里也是這么改的;第二是使用List代替;第三是實現你自己的包裝類,重寫hashcode和equals方法來達到以內容作為key的目的.

再來類比一下go語言中的map,map的key需要能夠進行比較的數據類型.那些內置復雜類型如slice,function和map以及包含這些類型字段的結構體也是不能用來做map的key的.

參考資料:

--EOF--

發表于 2018-12-01 15:25:00

,并被添加「java、go」標簽。

本站使用「署名 4.0 國際」創作共享協議,轉載請注明作者及原網址。更多說明 ?

提醒:本文最后更新于 830 天前,文中所描述的信息可能已發生改變,請謹慎使用。

專題「java相關」的其它文章 ?

總結

以上是生活随笔為你收集整理的java map byte[],java中byte数组不能作为map的key使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。