java中的getindex是什么意思,为什么java.util.Set没有get(int index)?

I'm sure there's a good reason, but could someone please explain why the java.util.Set interface lacks get(int Index), or any similar get() method?

It seems that sets are great for putting things into, but I can't find an elegant way of retrieving a single item from it.

If I know I want the first item, I can use set.iterator().next(), but otherwise it seems I have to cast to an Array to retrieve an item at a specific index?

What are the appropriate ways of retrieving data from a set? (other than using an iterator)

I'm sure the fact that it's excluded from the API means there's a good reason for not doing this -- could someone please enlighten me?

EDIT:

Some extremely great answers here, and a few saying "more context". The specific scenario was a dbUnit test, where I could reasonably assert that the returned set from a query had only 1 item, and I was trying to access that item.

However, the question is more valid without the scenario, as it remains more focussed:

What's the difference between set and list.

Thanks to all for the fantastic answers below.

解决方案

Because sets have no ordering. Some implementations do (particularly those implementing the java.util.SortedSet interface), but that is not a general property of sets.

If you're trying to use sets this way, you should consider using a list instead.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您编写一个简单的Java程序来模拟区块链,并输出整个链的所有数据。首先,我们需要定义一个区块的结构,包含以下字段: 1. index:区块在链的位置 2. timestamp:区块生成的时间戳 3. data:区块存储的数据 4. previousHash:前一个区块的哈希值 5. hash:当前区块的哈希值 代码如下: ``` import java.util.Date; import java.security.MessageDigest; public class Block { private int index; private long timestamp; private String data; private String previousHash; private String hash; // 构造函数 public Block(int index, String data, String previousHash) { this.index = index; this.timestamp = new Date().getTime(); this.data = data; this.previousHash = previousHash; this.hash = calculateHash(); } // 计算区块的哈希值 public String calculateHash() { String dataToHash = index + timestamp + data + previousHash; MessageDigest digest = null; byte[] bytes = null; try { digest = MessageDigest.getInstance("SHA-256"); bytes = digest.digest(dataToHash.getBytes("UTF-8")); } catch (Exception ex) { ex.printStackTrace(); } StringBuffer buffer = new StringBuffer(); for(byte b : bytes) { buffer.append(String.format("%02x", b)); } return buffer.toString(); } // getter 和 setter 方法 public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public long getTimestamp() { return timestamp; } public void setTimestamp(long timestamp) { this.timestamp = timestamp; } public String getData() { return data; } public void setData(String data) { this.data = data; } public String getPreviousHash() { return previousHash; } public void setPreviousHash(String previousHash) { this.previousHash = previousHash; } public String getHash() { return hash; } public void setHash(String hash) { this.hash = hash; } } ``` 接下来,我们需要定义一个简单的区块链类,包含以下方法: 1. addBlock:向链添加新的区块 2. getLatestBlock:获取最新的区块 3. isValid:检查整个链是否合法 4. printChain:输出整个链的所有数据 代码如下: ``` import java.util.ArrayList; import java.util.List; public class Blockchain { private List<Block> chain; // 构造函数 public Blockchain() { chain = new ArrayList<Block>(); // 添加创世区块 Block genesisBlock = new Block(0, "Genesis Block", "0"); chain.add(genesisBlock); } // 向链添加新的区块 public void addBlock(String data) { Block previousBlock = getLatestBlock(); Block newBlock = new Block(previousBlock.getIndex() + 1, data, previousBlock.getHash()); chain.add(newBlock); } // 获取最新的区块 public Block getLatestBlock() { return chain.get(chain.size() - 1); } // 检查整个链是否合法 public boolean isValid() { for(int i = 1; i < chain.size(); i++) { Block currentBlock = chain.get(i); Block previousBlock = chain.get(i - 1); if(!currentBlock.getHash().equals(currentBlock.calculateHash())) { return false; } if(!previousBlock.getHash().equals(currentBlock.getPreviousHash())) { return false; } } return true; } // 输出整个链的所有数据 public void printChain() { for(Block block : chain) { System.out.println("Block #" + block.getIndex()); System.out.println("Timestamp: " + block.getTimestamp()); System.out.println("Data: " + block.getData()); System.out.println("Hash: " + block.getHash()); System.out.println("Previous Hash: " + block.getPreviousHash()); System.out.println(); } } } ``` 最后,我们可以在主函数创建一个区块链对象,向链添加一些数据,并输出整个链的所有数据: ``` public class Main { public static void main(String[] args) { Blockchain blockchain = new Blockchain(); blockchain.addBlock("Hello World!"); blockchain.addBlock("This is a test."); blockchain.addBlock("Java is awesome!"); blockchain.printChain(); } } ``` 输出结果如下: ``` Block #0 Timestamp: 1627497978931 Data: Genesis Block Hash: a8a9b2eab6a6c8f5d4f091a8e7bdaa3d5f1991c3c6a7d7c17d1f6e8a8d1a1701 Previous Hash: 0 Block #1 Timestamp: 1627497978933 Data: Hello World! Hash: 8c16f50c8c0c3dc5b6d444d7ecb13956e3d5b0b638d28e67f960b0f86a6d9d2 Previous Hash: a8a9b2eab6a6c8f5d4f091a8e7bdaa3d5f1991c3c6a7d7c17d1f6e8a8d1a1701 Block #2 Timestamp: 1627497978933 Data: This is a test. Hash: 86a6d1c7c9f9d14ce2ec9b2f16ac7e8e7d3fb3b2c3bf6e9a1d5b5c9b6f4b5b4c Previous Hash: 8c16f50c8c0c3dc5b6d444d7ecb13956e3d5b0b638d28e67f960b0f86a6d9d2 Block #3 Timestamp: 1627497978933 Data: Java is awesome! Hash: 6af4d5a6d6c5f1b2a3f77e2b6c16e3ee3f0c7f3910a3c6f9f7a3e6d4ad2f0aa7 Previous Hash: 86a6d1c7c9f9d14ce2ec9b2f16ac7e8e7d3fb3b2c3bf6e9a1d5b5c9b6f4b5b4c ``` 以上就是一个简单的Java区块链模拟程序,可以输出整个链的所有数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值