给出一个位置列表,如
<td>El Cerrito, CA</td>
<td>Corvallis, OR</td>
<td>Morganton, NC</td>
<td>New York, NY</td>
<td>San Diego, CA</td>
Run Code Online (Sandbox Code Playgroud)
使用每个位置的图钉生成Google Map的最简单方法是什么?
我不确定如何确定功能接口的相等/不变性.我想在java 8中使用这个语法糖时可能无法保证平等,如果你有任何提示,请告诉我任何提示.
我为我的问题制作了一个简短的代码片段.
public interface Element {
void doSomething(int a);
}
Run Code Online (Sandbox Code Playgroud)
我试图以功能方式添加此接口的实例
public class FunctionSet {
public void doubleUp(int a) {
System.out.println(a*2);
}
public void square(int a) {
System.out.println(a*a);
}
public static void main(String[] args) {
HashSet<Element> set = new HashSet<>();
FunctionSet functionSet = new FunctionSet();
set.add(functionSet::doubleUp);
set.add(functionSet::square);
System.out.println(set.add(functionSet::doubleUp));
}
}
Run Code Online (Sandbox Code Playgroud)
它打印为true,这意味着没有任何相等检查,并且我添加它后也无法从Set中删除任何实例.
如果我使用功能接口作为参数,有什么方法可以以某种方式比较这些实例?
将提供任何帮助,谢谢!
我是Java 8的新手.我无法理解下面这段代码中的错误.Collection<User>如果它不是空的,那就是发送.但如果集合为空,则发送HttpStatus.NOT_FOUND实体响应.
@RequestMapping(value = "/find/pks",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<User>> getUsers(@RequestBody final Collection<String> pks)
{
return StreamSupport.stream(userRepository.findAll(pks).spliterator(), false)
.map(list -> new ResponseEntity<>(list , HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
Run Code Online (Sandbox Code Playgroud)
Eclipse在以下方面向我显示错误 .orElse
该
orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND))类型的方法未定义Stream<ResponseEntity<User>>
我的基本接口方法如下所示
Iterable<T> findAll(Iterable<PK> pks);
Run Code Online (Sandbox Code Playgroud) 我NameValuePair使用a lookupFunction(返回a Function)映射s 流,如下所示:
List<NameValuePair> paramPairs = getParamPairs();
List<NameValuePair> newParamPairs = paramPairs.stream()
.map((NameValuePair nvp) -> lookupFunction(nvp.getName()).apply(nvp))
.flatMap(Collection::stream)
.collect(toList());
Run Code Online (Sandbox Code Playgroud)
但是,如果lookupFunction返回了一个Collection<Function>,我希望对.map()每个返回的Functions 执行一次.我该怎么办?
现在我只能实现两个集合的笛卡尔积,这里是代码:
public static <T1, T2, R extends Collection<Pair<T1, T2>>>
R getCartesianProduct(
Collection<T1> c1, Collection<T2> c2,
Collector<Pair<T1, T2>, ?, R> collector) {
return c1.stream()
.flatMap(e1 -> c2.stream().map(e2 -> new Pair<>(e1, e2)))
.collect(collector);
}
Run Code Online (Sandbox Code Playgroud)
这段代码在IntelliJ中工作正常,但在Eclipse中没有(编译器合规级别为1.8):
The method collect(Collector<? super Object,A,R>)
in the type Stream<Object> is not applicable for
the arguments (Collector<Pair<T1,T2>,capture#5-of ?,R>)
Run Code Online (Sandbox Code Playgroud)
这是Pair.java:
public class Pair<T1, T2> implements Serializable {
protected T1 first;
protected T2 second;
private static final long serialVersionUID = 1360822168806852921L;
public Pair(T1 first, T2 second) …Run Code Online (Sandbox Code Playgroud) 我想减去2个double值,我尝试了以下代码.
double val1 = 2.0;
double val2 = 1.10;
System.out.println(val1 - val2);
Run Code Online (Sandbox Code Playgroud)
我得到了输出,
0.8999999999999999
Run Code Online (Sandbox Code Playgroud)
为了获得输出,0.9我尝试BigDecimal如下,
BigDecimal val1BD = new BigDecimal(val1);
BigDecimal val2BD = new BigDecimal(val2);
System.out.println(val1BD.subtract(val2BD));
Run Code Online (Sandbox Code Playgroud)
我得到了输出,
0.899999999999999911182158029987476766109466552734375
Run Code Online (Sandbox Code Playgroud)
然后我尝试了 BigDecimal.valueOf()
val1BD = BigDecimal.valueOf(val1);
val2BD = BigDecimal.valueOf(val2);
System.out.println(val1BD.subtract(val2BD));
Run Code Online (Sandbox Code Playgroud)
最后我得到了输出0.9.
我的问题是案例2和案例3有什么区别?
在案例2中为什么我得到这样的输出?
我有一个简单的分支定界算法,适用于旅行商问题的变体,我认为尝试将其转换为使用Java 8 Stream API会很有趣.然而,我很难在不依赖副作用的情况下弄清楚如何做到这一点.
初始代码
int bound = Integer.MAX_VALUE;
List<Location> bestPath = null;
while(!queue.isEmpty()) {
Node curr = queue.poll();
//bound exceeds best, bail
if (curr.getBound() >= bound) {
return bestPath;
}
//have a complete path, save it
if(curr.getPath().size() == locations.size()) {
bestPath = curr.getPath();
bound = curr.getBound();
continue;
}
//incomplete path - add all possible next steps
Set<Location> unvisited = new HashSet<>(locations);
unvisited.removeAll(curr.getPath());
for (Location l : unvisited) {
List<Location> newPath = new ArrayList<>(curr.getPath());
newPath.add(l);
Node newNode = new Node(newPath, …Run Code Online (Sandbox Code Playgroud) 这是代码:
public class LogService {
private final BlockingQueue<String> queue;
private final LoggerThread loggerThread;
private final PrintWriter writer;
@GuardedBy("this") private boolean isShutdown;
@GuardedBy("this") private int reservations; // <-- counter
public void start() { loggerThread.start(); }
public void stop() {
synchronized (this) { isShutdown = true; }
loggerThread.interrupt();
}
public void log(String msg) throws InterruptedException {
synchronized (this) {
if (isShutdown)
throw new IllegalStateException(...);
++reservations;
}
queue.put(msg);
}
private class LoggerThread extends Thread {
public void run() {
try {
while (true) …Run Code Online (Sandbox Code Playgroud) 我正在编写简单的程序,它最终会绘制用Java编写的各种排序算法的运行时间.排序算法的一般接口是通过一种方法:public void sort(Comparable[] xs)
我试图使用Java 8的流机制生成以下几行的随机测试用例:
public static IntStream testCase(int min, int max, int n) {
Random generator = new Random();
return generator.ints(min, max).limit(n);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何将类型的对象转换IntStream为Integer[]?
在这本书中,它说:
Arrays.asList()的一个限制是它对List的结果类型进行了最佳猜测,并没有注意你将它分配给它的内容.
这本书是由Java思考作者:Bruce Eckel
但是,以下代码工作正常,与本书第280页中显示的代码相反
public class Main{
public static void main(String[] args) {
List<Snow> snow = Arrays.asList(new Light(), new Heavy());
}
}
class Snow {}
class Powder extends Snow {}
class Light extends Powder {}
class Heavy extends Powder {}
Run Code Online (Sandbox Code Playgroud)
任何想法都表示赞赏.
java ×9
java-8 ×5
java-stream ×5
lambda ×3
algorithm ×1
bigdecimal ×1
collections ×1
concurrency ×1
equality ×1
generics ×1
google-maps ×1
html ×1
immutability ×1
inheritance ×1