HashMap<int[],string>通过考虑它们的值来映射整数数组

HashMaplt;int[],stringgt; mapping integer array by considering their values(HashMaplt;int[],stringgt;通过考虑它们的值来映射整数数组)
本文介绍了HashMap<int[],string>通过考虑它们的值来映射整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

Given the following code, I get a null (what I want is "1234"). But I wish to have a map that could consider the key as equals, if the contents of int[] are equals (rather than consider that the references of int[]), how should I do it?

HashMap<int[], String> maps=new HashMap<int[], String>();
int[] i=new int[]{1,2,3};
int[] j=new int[]{1,2,3};
maps.put(i,"1234");
System.out.print(maps.get(j));

I am opened to any map that allows keeping int[] as the key (including TreeMap) and so on, with the side condition that if that does not hamper on the effectiveness of the map accessing time.

解决方案

No way to do that with arrays, because they don't override equals() and hashCode(). You should define your own class wrapping the array, which would override equals() and hashCode() or use a List<Integer> as key instead.

这篇关于HashMap&lt;int[],string&gt;通过考虑它们的值来映射整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Show progress during FTP file upload in a java applet(在 Java 小程序中显示 FTP 文件上传期间的进度)
How to copy a file on the FTP server to a directory on the same server in Java?(java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?)
FTP zip upload is corrupted sometimes(FTP zip 上传有时会损坏)
Enable logging in Apache Commons Net for FTP protocol(在 Apache Commons Net 中为 FTP 协议启用日志记录)
Checking file existence on FTP server(检查 FTP 服务器上的文件是否存在)
FtpClient storeFile always return False(FtpClient storeFile 总是返回 False)