我可以想到为什么HashMap带有整数键的SparseArrays 比s 更好的几个原因:
SparseArray说"它通常比传统的慢HashMap".HashMaps而不是SparseArrays 编写代码,则代码将与Map的其他实现一起使用,您将能够使用为Maps设计的所有Java API.HashMaps而不是SparseArrays 编写代码,你的代码将在非android项目中工作.equals(),hashCode()而SparseArray不是.然而,每当我尝试HashMap在Android项目中使用带整数键的时候,IntelliJ告诉我应该使用一个SparseArray代替.我觉得这很难理解.有谁知道使用SparseArrays的任何令人信服的理由?
前几天我才知道你可以做到这一点
new Object() {
void hello() {
System.out.println("Hello World!");
}
}.hello();
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很奇怪.当然,创建的对象的静态类型是Object,所以没有方法hello()?它几乎完全没有意义(hello例如,不可能调用两次).
我有2个问题.
hello就是这样.反思怎么样?谢谢
假设有两个接口Interface1,Interface2其中Interface2扩展Interface1.
interface Interface1 {
default void method() {
System.out.println("1");
}
// Other methods
}
interface Interface2 extends Interface1 {
@Override
default void method() {
System.out.println("2");
}
// Other methods
}
Run Code Online (Sandbox Code Playgroud)
假设我想创建一个实现的类,Interface2但我想method()成为其中的版本Interface1.如果我写
class MyClass implements Interface1, Interface2 {
public void method() {
Interface1.super.method();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误:
默认超级调用中的错误类型限定符:冗余接口Interface1由Interface2扩展
可以通过创建第三个界面来解决这个问题:
interface Interface3 extends Interface1 {
default void method() {
Interface1.super.method();
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
class MyClass implements Interface1, Interface2, Interface3 …Run Code Online (Sandbox Code Playgroud) 文档ArrayDeque说:
当用作堆栈时,此类可能比Stack快,并且当用作队列时比LinkedList更快.
没有提到使用ArrayDeque堆栈和使用堆栈之间的区别ArrayList.您可以使用ArrayList如下堆栈作为堆栈.
list.add(object); // push
object = list.remove(list.size() - 1); // pop
Run Code Online (Sandbox Code Playgroud)
我发现当我只用ArrayList这种方式时,它的性能比它差ArrayDeque.这种差异的原因是什么?当然,它不仅仅是电话size()?在内部,都ArrayList和ArrayDeque使用的是实现Object[]由更大的阵列需要时更换,所以可靠地性能应该是大约相同的?
为什么这段代码没有抛出ConcurrentModificationException?它修改了一段Collection时间迭代它,而不使用Iterator.remove()方法,这是唯一安全的删除方法.
List<String> strings = new ArrayList<>(Arrays.asList("A", "B", "C"));
for (String string : strings)
if ("B".equals(string))
strings.remove("B");
System.out.println(strings);
Run Code Online (Sandbox Code Playgroud)
如果我ArrayList用a 替换,我会得到相同的结果LinkedList.但是,如果我将列表更改为("A", "B", "C", "D)或只是("A", "B")按预期获得异常.到底是怎么回事?我正在使用,jdk1.8.0_25如果这是相关的.
编辑
我找到了以下链接
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4902078
相关部分是
天真的解决方案是在AbstractList中为hasNext添加编码检查,但这会使编纂检查的成本增加一倍.事实证明,仅在最后一次迭代时进行测试就足够了,这几乎不会增加成本.换句话说,hasNext的当前实现:
Run Code Online (Sandbox Code Playgroud)public boolean hasNext() { return nextIndex() < size; }被此实现取代:
Run Code Online (Sandbox Code Playgroud)public boolean hasNext() { if (cursor != size()) return true; checkForComodification(); return false; }由于Sun内部监管机构拒绝了此项更改,因此不会进行此更改.正式裁决表明,这一变化"已证明可能对现有代码产生重大的兼容性影响." ("兼容性影响"是修复程序有可能用ConcurrentModificationException替换静默不当行为.)
我试图理解java泛型,它们似乎非常难以理解.例如,这很好......
public class Main {
public static void main(String[] args) {
List<?> list = null;
method(list);
}
public static <T> void method(List<T> list) { }
}
Run Code Online (Sandbox Code Playgroud)
......这就是......
public class Main {
public static void main(String[] args) {
List<List<?>> list = null;
method(list);
}
public static <T> void method(List<T> list) { }
}
Run Code Online (Sandbox Code Playgroud)
... 还有这个 ...
public class Main {
public static void main(String[] args) {
List<List<List<?>>> list = null;
method(list);
}
public static <T> void method(List<List<T>> list) { }
} …Run Code Online (Sandbox Code Playgroud) 在摆弄Android Studio中的布局时,我以某种方式将我的logcat窗口变成了一个浮动窗口.我不能为我的生活恢复到停靠模式.

我宁愿不必从头开始重建项目,但找不到任何明显的东西.idea/workspace.xml.
以下程序抛出以下异常:
java.lang.IllegalArgumentException: Comparison method violates its general contract!
Run Code Online (Sandbox Code Playgroud)
我理解了这个问题Comparator.请参阅无法复制:"比较方法违反了其总合同!"
我不明白为什么只有List32或更大的s 才会失败.谁能解释一下?
class Experiment {
private static final class MyInteger {
private final Integer num;
MyInteger(Integer num) {
this.num = num;
}
}
private static final Comparator<MyInteger> COMPARATOR = (r1, r2) -> {
if (r1.num == null || r2.num == null)
return 0;
return Integer.compare(r1.num, r2.num);
};
public static void main(String[] args) {
MyInteger[] array = {new MyInteger(0), new MyInteger(1), new MyInteger(null)};
Random random = new …Run Code Online (Sandbox Code Playgroud) Math.atan2的文档说
计算结果必须在精确结果的2 ulps范围内.
它说2个ulps的事实可能意味着返回的值不是最接近double真实结果的情况.有谁知道是否保证为等效int参数对返回相同的值?换句话说,如果a,b并且k是正值int,既不是a * k也不b * k溢出,那么它是否可以保证
Math.atan2(a, b) == Math.atan2(a * k, b * k)
Run Code Online (Sandbox Code Playgroud)
编辑
请注意,绝对不是非溢出long乘法的情况.例如
long a = 959786689;
long b = 363236985;
long k = 9675271;
System.out.println(Math.atan2(a, b));
System.out.println(Math.atan2(a * k, b * k));
Run Code Online (Sandbox Code Playgroud)
版画
1.2089992287797169
1.208999228779717
Run Code Online (Sandbox Code Playgroud)
但我找不到int价值观的例子.
假设您有一个这样的方法来计算Collection某些方法的最大值ToIntFunction:
static <T> void foo1(Collection<? extends T> collection, ToIntFunction<? super T> function) {
if (collection.isEmpty())
throw new NoSuchElementException();
int max = Integer.MIN_VALUE;
T maxT = null;
for (T t : collection) {
int result = function.applyAsInt(t);
if (result >= max) {
max = result;
maxT = t;
}
}
// do something with maxT
}
Run Code Online (Sandbox Code Playgroud)
使用Java 8,可以将其转换为
static <T> void foo2(Collection<? extends T> collection, ToIntFunction<? super T> function) {
T maxT = collection.stream()
.max(Comparator.comparingInt(function))
.get();
// …Run Code Online (Sandbox Code Playgroud) java ×9
android ×2
java-8 ×2
arraydeque ×1
arraylist ×1
class ×1
collections ×1
generics ×1
hashmap ×1
java-stream ×1
math ×1
stack ×1
types ×1
wildcard ×1