小编Tag*_*eev的帖子

制作Google Map mashup的最简单方法是什么?

给出一个位置列表,如

<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的最简单方法是什么?

html google-maps

8
推荐指数
2
解决办法
4679
查看次数

java中功能接口实例的等价性

我不确定如何确定功能接口的相等/不变性.我想在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 equality immutability java-8 functional-interface

8
推荐指数
1
解决办法
866
查看次数

如何使用java Stream检查Collection是否为空

我是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)

java lambda java-8 java-stream

8
推荐指数
2
解决办法
2万
查看次数

将函数列表应用于Java流的.map()方法

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 执行一次.我该怎么办?

java lambda functional-programming java-8 java-stream

8
推荐指数
1
解决办法
2604
查看次数

用Java 8实现集合的Cartesian产品

现在我只能实现两个集合的笛卡尔积,这里是代码:

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)

java generics collections java-8 java-stream

8
推荐指数
1
解决办法
2226
查看次数

BigDecimal减法

我想减去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 bigdecimal

8
推荐指数
1
解决办法
1万
查看次数

转换Branch and Bound循环以使用Java Stream API

我有一个简单的分支定界算法,适用于旅行商问题的变体,我认为尝试将其转换为使用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)

java algorithm lambda java-8 java-stream

8
推荐指数
1
解决办法
576
查看次数

我可以使用Collection.size()替换此代码中的计数器吗?

这是代码:

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 concurrency multithreading

8
推荐指数
1
解决办法
76
查看次数

Java 8:IntStream到Integer []

我正在编写简单的程序,它最终会绘制用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)

我的问题是,如何将类型的对象转换IntStreamInteger[]

java java-stream

8
推荐指数
1
解决办法
4118
查看次数

在Java第4版的Thinking中,Arrays.asList()的限制不正确

在这本书中,它说:

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 1.8,IntelliJ,Windows 7 在此输入图像描述

任何想法都表示赞赏.

java inheritance type-inference

8
推荐指数
1
解决办法
210
查看次数