为什么B-Tree是磁盘存储的首选结构.
什么样的质量使它比二叉树更适合二级存储.
具体的"质量"是否是算法本身的一个特征;或者它的实现方式?
任何参考或指针将非常感激.
我们有一个Map<String, Student> studentMap
,其中Student
是一个类,如下所示:
class Student{
String name;
int age;
}
Run Code Online (Sandbox Code Playgroud)
我们需要返回一个包含所有 Ids 的列表eligibleStudents
,其中年龄 > 20。
为什么下面会在 处给出编译错误Collectors.toList
:
HashMap<String, Student> studentMap = getStudentMap();
eligibleStudents = studentMap .entrySet().stream()
.filter(a -> a.getValue().getAge() > 20)
.collect(Collectors.toList(Entry::getKey));
Run Code Online (Sandbox Code Playgroud) 我们有一张学生地图要记录Map<Student, StudentRecord>
。
学生班如下:
Student {
String id;
String grade;
Int age;
}
Run Code Online (Sandbox Code Playgroud)
此外,我们还(List<String>)
提供了学生 ID 列表。
使用 Java 流,过滤出 ID 存在于提供的列表中的学生的记录的最有效方法是什么?
预期结果是映射到 Id(String) 的过滤列表 - <Map<Id, StudentRecord>>
我需要以最有效的方式帮助将以下列表转换为字典:
l = ['A:1','B:2','C:3','D:4']
Run Code Online (Sandbox Code Playgroud)
目前,我做了以下事情:
mydict = {}
for e in l:
k,v = e.split(':')
mydict[k] = v
Run Code Online (Sandbox Code Playgroud)
但是,我认为应该有一种更有效的方法来实现同样的目标.任何的想法 ?
在处理之前,有三种方法可以将整个文件读入内存:
方法A:
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
Run Code Online (Sandbox Code Playgroud)
方法B:
ByteArrayInputStream bi =
new ByteArrayInputStream(
org.apache.commons.io.FileUtils.readFileToByteArray(file))
Run Code Online (Sandbox Code Playgroud)
方法C:
File file = new File(yourFileName);
RandomAccessFile ra = new RandomAccessFile(yourFileName, "rw"):
byte[] b = new byte[(int)file.length()];
try {
ra.read(b);
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
为什么我更喜欢一种方法呢?
是否有任何特定的用例需要一种方法而不是另一种方法?
为什么不使用固定长度byte[]
呢?
是否有内置函数来确定列表中是否存在类的实例?
目前我通过理解来做到这一点
>>> class A:
... pass
...
>>> l1=[5,4,3,A(),8]
>>> e=[e for e in l1 if isinstance(e,A)]
Run Code Online (Sandbox Code Playgroud) 我们有一张地图学生{studentId:StudentData}。
StudentData是具有学生属性的POJO。
要求是使所有具有属性的学生isCurrent==true
。
如果是这样,则为学生提取城市属性。
这将用于增加学生人数地图的数量{ city : count }
Students.entrySet()
.stream()
.filter(x -> true == x.getValue().isCurrent())
.forEach(x -> {
studentpercity(x.getValue().getCity(), (studentpercity.getOrDefault(x.getValue().getCity(), 0) + 1));
});
Run Code Online (Sandbox Code Playgroud)
最终输出将是一张城市地图,以计算“当前”学生的人数。
有没有更有效的方法来实现这一目标?
我需要编写一个JUnit测试用例,它将测试一个传递不同排列的函数,并得到相应的结果.
成功的用例不返回任何内容,而失败的排列会抛出异常(异常类型无关紧要).
例如.testAppleisSweetAndRed(水果,颜色,味道)
测试会调用以下内容 -
testAppleisSweetAndRed(orange,red,sweet)//throws exception
testAppleisSweetAndRed(apple,green,sweet)//throws exception
testAppleisSweetAndRed(apple,red,sour)//throws exception
testAppleisSweetAndRed(apple,red,sweet)//OK
Run Code Online (Sandbox Code Playgroud)
如果调用的行为符合预期,则测试成功.
断言如何捕获前3次调用以确保它们确实引发预期的异常?
我遇到了泛型类的以下定义:
public class binarysearchnode<T extends Comparable<T>> implements Comparable<binarysearchnode<T>>{
.............
}
Run Code Online (Sandbox Code Playgroud)
请帮助解释为什么类在实现类似的接口时将自己指定为Type参数以进行比较?它与以下内容有何不同:
public class binarysearchnode<T extends Comparable<T>> implements Comparable<? super (or extends)T>{
.............
}
Run Code Online (Sandbox Code Playgroud) 这是我心中想到的一种怀疑,并且想要把它彻底打破.
假设我有以下类作为我的应用程序的一部分加载:
class HeavyClass {
static final ArrayList list = new ArrayList(100);
}
Run Code Online (Sandbox Code Playgroud)
静态成员的生命周期是否与应用程序对齐,而不管HeavyClass实例发生了什么.
如果静态将自己与不是垃圾收集的类加载器内存(permgen)对齐 - 那么编程习惯用法是确保这样的对象只在需要的基础上消耗(并清理成员);假设我们必须有静态的在所有实例之间共享)
java ×7
java-8 ×3
java-stream ×3
list ×3
python ×2
algorithm ×1
comparable ×1
dictionary ×1
file-io ×1
generics ×1
inputstream ×1
junit ×1
lambda ×1
string ×1