我目前对 Map 何时优于 HashMap 或其他方式感到困惑,对于列表/数组列表也是如此...
有人可以请 ELI5 吗?我知道如何使用它们,但我需要有人在应该使用它们时为我清理它,谢谢。
如果我有以下内容:
public enum Attribute {
ONE, TWO, THREE
}
private Map<String, Integer> mAttributesMap = new HashMap<>();
mAttributesMap.put(Attribute.ONE.name(), 5);
mAttributesMap.put(Attribute.TWO.name(), 5);
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能得到钥匙mAttibutesMap?以及如何增加它?
我想计算一个以给定数字开头的HashMap中的所有键.每个键的大小并不总是相同的.例:
给定数字(长):
long l = 9988776655
Run Code Online (Sandbox Code Playgroud)
找到以该数字开头的键(长),如:
9988776655xxxxxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)
其中x代表任何整数.
我该如何处理这个问题?由于键的长度并不总是相同,因此我不能使用多个模运算.(或者我可以吗?)
我正在尝试HashMap使用LinkedHashMap和排序输出TreeMap.
当我用它TreeMap来理清HashMap它就像一个魅力.
Map<Integer, String> hMap = new HashMap<Integer, String>();
hMap.put(40, "d");
hMap.put(10, "a");
hMap.put(30, "c");
hMap.put(20, "b");
System.out.println(" ");
System.out.println("before");
for (Map.Entry m1 : hMap.entrySet()) {
System.out.print(m1.getKey() + " " + m1.getValue() + " ");
}
System.out.println("after");
Map<Integer, String> hTree = new TreeMap<Integer, String>(hMap);
for (Map.Entry m2 : hTree.entrySet()) {
System.out.print(m2.getKey() + " " + m2.getValue() + " ");
}
Run Code Online (Sandbox Code Playgroud)
输出:before
20 b 40 d 10 a 30 c …
假设有如下图所示:
Map<String, List<String>> myMap =
{
k1: [ v1, v2, v3, v3],
k2: [ v1, v2],
k3: [ v1, v2, v6, v7]
}
Run Code Online (Sandbox Code Playgroud)
我想找到所有键通用的值。
List<String> commonValues = {v1,v2}
Run Code Online (Sandbox Code Playgroud)
我想了解使用 java 8 的有效方法。我可以在 java 5 中使用 for 循环实现相同的效果,但我确信 Java 8 有更好的方法来做到这一点。
我有一个Dictionary自定义散列函数。我想测试散列函数,因为即使它为我的测试值返回不同的散列结果,由于模%运算,其中一些可能仍然映射到同一个存储桶。
这是一个微调hash函数的开发测试,不会投入生产,所以不用担心其他版本内部实现的变化!!!
在 C++ 中,可以获取地图的桶大小来检查碰撞状态,但我找不到在 C# 中执行此操作的方法。我怎么知道是否Dictionary发生了碰撞?
我有一个这样的清单
[A-Apple.txt,B-Ball.txt,A-Axe.txt,B-Box.txt]
Run Code Online (Sandbox Code Playgroud)
由此我想创建一个如下所示的地图:
{A=[A-Apple.txt,A-Axe.txt], B= [B-Ball.txt, B-Box.txt]
Run Code Online (Sandbox Code Playgroud)
我试过
Map<String,List<String>> inputMap = new HashMap<>();
inputFCSequenceFileList.forEach(value ->{
List newList = new ArrayList();
newList.add(value);
inputMap.put(value.split("-")[0], newList);
}
);
Run Code Online (Sandbox Code Playgroud)
但没有得到预期值。我只得到最后一个元素。如果我将列表创建移到 foreach 循环之外,那么我将获得所有值。
传统代码运行良好,如下所示:
Map<Integer, List<Integer>> map = new HashMap<>();
if (!map.containsKey(1)) {
map.put(1, new ArrayList<>());
}
map.get(1).add(2);
Run Code Online (Sandbox Code Playgroud)
现在我想尝试一下 getOrDefault 的神奇之处:
map.getOrDefault(1, new ArrayList<>()).add(2);
Run Code Online (Sandbox Code Playgroud)
但是如果我使用上面的行,map.get(1)则为空。
为什么?
相对较新到Java 8,我想知道为什么它允许第一个变体(merge功能不是必要的)Collectors.toMap()一起工作时List:
static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Run Code Online (Sandbox Code Playgroud)
AList允许完全重复的值。想象一个用例,其中开发人员用于stream转换List为MapJava 8 将 RUNTIME 公开为:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key ...
Run Code Online (Sandbox Code Playgroud)
不应该要求在编译时捕获这种情况吗?AFAIK,哈希图用于在放置Entry重复项时简单地替换旧的key。如果开发人员确定数据中存在重复值并希望将此异常作为警告处理,他会不会首先使用Set代替List?例如。:-
public class Employee{
public String name;
public String surname;
public Employee(String firstname, String secondName) {
this.name=firstname;this.surname=secondName;
}
public boolean equals(Object o){
return (o instanceof Employee) && ((Employee)o).name.equals(this.name) …Run Code Online (Sandbox Code Playgroud) 为什么下面给出了以下输出?$a1 定义...
package A1;
use Hash::Merge;
use Data::Dumper;
sub new
{
my $class = shift;
my $self =
{
length => 2,
};
return bless $self, $class;
}
1;
my $a1 = A1->new();
print("a1 = " . ref($a1) . "\n");
my %a = {'1' => $a1};
my $a3 = \%a;
print( Dumper($a3));
Run Code Online (Sandbox Code Playgroud)
输出:
a1 = A1
$VAR1 = {
'HASH(0x247c568)' => undef
};
Run Code Online (Sandbox Code Playgroud)
我原以为该值是“A1(0x...)”,而不仅仅是未定义的...
hashmap ×10
java ×8
dictionary ×3
java-8 ×3
arraylist ×2
list ×2
c# ×1
collections ×1
collectors ×1
java-stream ×1
key ×1
perl ×1
treemap ×1