下面的代码编译没有错误...一次我会更喜欢它失败:/
Map <Character, Double> m = new HashMap <Character, Double>();
m.get(new String());
Run Code Online (Sandbox Code Playgroud)
由于编译器知道此映射中使用的键是Character类型,因此使用String键应标记为不正确.
我错过了什么?
我有这个不应该编译.
public boolean foo(final Map<String, String> map) {
map.get(1); // No error here?
}
Run Code Online (Sandbox Code Playgroud) 所以我试图通过 int 索引获取 TreeMap 的键。
这是我的代码:
TreeMap<String, Integer> map = new TreeMap<String, Integer>(Collections.reverseOrder());
map.put("hi", 1);
map.put("hi2", 5);
System.out.println("Key: " + (String) map.keySet().toArray()[0] + "\nValue: " + map.get(0));
Run Code Online (Sandbox Code Playgroud)
但是当我执行这个时,我收到错误:
java.lang.Integer cannot be cast to java.lang.String
Run Code Online (Sandbox Code Playgroud)
所以我试图得到钥匙:“hi2”
我正在使用键T和值Long创建一个HashMap,我的remove方法(从AbstractCollection类中重写)看起来像这样:
public boolean remove(Object o) {
if(denseBag.containsKey(o)){
if(denseBag.get(o) == 1L){
denseBag.remove(o);
} else {
Long removed = denseBag.get(o);
T theO = (T) o;
denseBag.replace(theO, removed, removed--);
}
} else {
return false;
}
Run Code Online (Sandbox Code Playgroud)
我收到消息:"输入安全性:从对象到T的未选中状态".我只是想确保它能正常工作.谢谢.
更改Collections.unmodifiableList为return List<? extends T>而不是List<T>将阻止在编译时添加和删除元素,而不是抛出运行时异常.
是否存在由此替代方案引起的严重问题,从而排除它?
我有这个String to String映射,并且我试图char作为键传递
Map<String, String> phone = new HashMap<String, String>() {{
put("2", "abc");
put("3", "def");
put("4", "ghi");
put("5", "jkl");
put("6", "mno");
put("7", "pqrs");
put("8", "tuv");
put("9", "wxyz");
}};
String letterList = phone.get('2'); //null
String letterList = phone.get(String.valueOf('2')); //it works
Run Code Online (Sandbox Code Playgroud)
为什么第一种情况不起作用?以我的理解,char可以隐式转换为字符串“ 2”,并且HashMap用于equals()比较键,以便它应该在map中检索键吗?
如代码所示:我不明白为什么在第一张图中,大小是100(remove()之后),但在第二张图中,大小是1而不是2。是不是因为数据类型的不同存储在哈希图中还是?
public class test {
public static void main(String[] args) {
Map<Short, String> map = new HashMap<Short, String>();
for (short i = 0; i < 100; i++) {
map.put(i, String.valueOf(i));
map.remove(i - 1);
}
Map<Integer, Integer> hashmap = new HashMap<>();
hashmap.put(3,4);
hashmap.put(4,5);
hashmap.remove(3);
System.out.println(hashmap.size());
System.out.println(map.size());
}
}
Run Code Online (Sandbox Code Playgroud)
帮我!
让我们看一些Set<E>方法声明.
public boolean add(E e);
public E get(int index);
Run Code Online (Sandbox Code Playgroud)
让我们尝试使用它.
List<Boolean> list = new ArrayList<Boolean>();
Integer i = list.get(0); //Predictably, here we get a compile error.
list.contains(new Integer(3)); //But this line is allowed. Why?
Run Code Online (Sandbox Code Playgroud)
即使在这个代码的非泛型等价物中(我知道,它只会转换成它),我们在两行中都会遇到编译错误.
List s = new ArrayList();
s.contains((Boolean)(new Integer(3)));
Integer i = (Boolean)s.get(3);
Run Code Online (Sandbox Code Playgroud)
那么为什么我不能在通用案例中得到错误?
Why does containsAll method on a HashSet does not remain consistent if remove is called on the Set whereas a containsValue method on a HashMap remains consistent after a value is removed After a value is removed from a HashSet containsAll returns false even if all values were present where as in case of HashMap the containsValue method returns correct value
public static void main(String[] args)
{
HashSet<String> lookup=new HashSet<String>();
HashMap<Integer,String> findup=new HashMap<Integer,String>();
String[] Alltokens={"This","is","a","programming","test","This","is","a","any","language"};
for(String s:Alltokens)
lookup.add(s);
String[] tokens={"This","is","a"}; …Run Code Online (Sandbox Code Playgroud)