我正在寻找一种有效的方法来如何轻松地将其转换BitSet为二进制字符串.让我们说它的通常长度将是数千位.
例如,让我们:
BitSet bits = new BitSet(8);
bits.set(1);
bits.set(3);
Run Code Online (Sandbox Code Playgroud)
这是理想的结果:
String result = toBinaryString(bits);
// expected: result = "01010000"
Run Code Online (Sandbox Code Playgroud)
我总体上有一些想法(溪流等),但可能有一些明显的标准方法,我只是缺少.
我读理查德·伯德的书这样的问题:找到五种最常用的词在战争与和平(或任何其他文本为此事).
这是我目前的尝试:
public class WarAndPeace {
public static void main(String[] args) throws Exception {
Map<String, Integer> wc =
Files.lines(Paths.get("/tmp", "/war-and-peace.txt"))
.map(line -> line.replaceAll("\\p{Punct}", ""))
.flatMap(line -> Arrays.stream(line.split("\\s+")))
.filter(word -> word.matches("\\w+"))
.map(s -> s.toLowerCase())
.filter(s -> s.length() >= 2)
.collect(Collectors.toConcurrentMap(
w -> w, w -> 1, Integer::sum));
wc.entrySet()
.stream()
.sorted((e1, e2) -> Integer.compare(e2.getValue(), e1.getValue()))
.limit(5)
.forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
}
}
Run Code Online (Sandbox Code Playgroud)
这绝对看起来很有趣并且运行得相当快.在我的笔记本电脑上打印以下内容:
$> time java -server -Xmx10g -cp target/classes tmp.WarAndPeace
the: 34566
and: …Run Code Online (Sandbox Code Playgroud) 在IntelliJ IDEA(15.0.5)中,在类名上,我看到一个窗口图标,点击它会弹出这个弹出菜单:
有谁知道"[类名]的并发图"应该做什么?当我实际选择菜单项时,它显示:
运行[班级名称]时出错:找不到[班级名称]的跑步者.
扩展接口的任何类都必须实现接口中声明的方法.不确定这是否可行,但我想做的是以下内容:
interface test {
____ get();
}
class A extends test {
int val;
A(int x) {
val = x;
}
int get() {
return Val;
}
class B extends test {
String val;
B(String x) {
val = x;
}
String get() {
return Val;
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用能够返回两种不同数据类型的方法签名?
我使用a CountDownLatch来处理两个Java线程.我的班级结构如下:
MainClass.java
ThreadOne.java
ThreadTwo.java
MainClass:
CountDownLatch latch = new CountDownLatch(2);
Thread thread = new Thread(new ThreadOne(latch));
thread.start();
Thread thread1 = new Thread(new ThreadTwo(latch));
thread1.start();
latch.await(20, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
主类等待其他两个线程完成其工作.一旦他们完成工作,它就不会等到超时值(20秒).我的问题是,如果任何线程被破坏或损坏,那么CountDownLatch等待其超时值.有没有办法忽略那个被打断的线程并继续前进而不等待20秒?
假设我使用的库提供了以下形状:
class Shape
class Circle extends Shape
class Square extends Shape
Run Code Online (Sandbox Code Playgroud)
但我想介绍红色形状的概念.我无法修改库但我可以创建:
class RedShape extends Shape
Run Code Online (Sandbox Code Playgroud)
然而,这不能轻易扩展到RedCircle因为它不能同时延伸RedShape和Circle.
我不认为装饰器模式在这里工作得很好但是有没有实现这个的技术?
我非常喜欢 Ruby 的一项功能是能够利用调用链。它提供了一种调试管道中正在发生的事情的简单方法。我tap用一个模拟map:
/** Searches recursively and returns the path to the dir that has a file with given extension,
* null otherwise.
* Returns the given dir if it has a file with given extension.
* @param dir Path to the start folder
* @param ext String denotes the traditional extension of a file, e.g. "*.gz"
* @return {@linkplain Path} of the folder containing such a file, null otherwise
*/
static Path getFolderWithFilesHavingExtension(Path …Run Code Online (Sandbox Code Playgroud) 我非常确定A* 算法中的* (星号)意味着该算法是可接受的,即如果该路径存在(当采用的启发式是乐观的时),则保证它找到图中的最短路径。
我对吗?我未能成功寻找有关该主题的任何信息,但找不到任何参考资料。希望这个社区中最有经验的用户比我更了解 A* 的历史。
顺便说一句,我认为其他基于 A* 的算法,如 IDA*、D*、SMA*、MOA*、NAMOA* 等,都遵循相同的名称约定。
search artificial-intelligence heuristics a-star graph-algorithm
编写一个方法,该方法接受一个整数数组并返回其最长子数组的长度,其中包含不同的整数。
例如,[1,2,3,4,2,3]它应该返回4。