Easy way to change Iterable into Collection

In my application I use 3rd party library (Spring Data for MongoDb to be exact). Methods of this library return Iterable<T> , while the rest of my code expects Collection<T> . Is there any utility method somewhere that will let me quickly convert one to the other. I would like to avoid making a banch of foreach loops in my code for such a simple thing. With Guava you can use Li

简单的方法将Iterable更改为Collection

在我的应用程序中,我使用第三方库(准确地说,Spring Data for MongoDb)。 这个库的方法返回Iterable<T> ,而我的其他代码需要Collection<T> 。 有什么实用方法可以让我快速将一个转换为另一个。 我想避免在我的代码中使用foreach循环来实现这样一个简单的事情。 使用番石榴,您可以使用Lists.newArrayList(Iterable)或Sets.newHashSet(Iterable)等方法。 这当然会将所有元素复制到内存中。 如果这是

What is the best way to filter a Java Collection?

我想过滤一个基于谓词的java.util.Collection 。 Java 8 (2014) solves this problem using streams and lambdas in one line of code: List<Person> beerDrinkers = persons.stream() .filter(p -> p.getAge() > 16).collect(Collectors.toList()); Here's a tutorial. Use Collection#removeIf to modify the collection in place. (Notice: In this case, the predicate will remove objects who sat

过滤Java集合的最佳方法是什么?

我想过滤一个基于谓词的java.util.Collection 。 Java 8(2014)在一行代码中使用流和lambdas解决了这个问题: List<Person> beerDrinkers = persons.stream() .filter(p -> p.getAge() > 16).collect(Collectors.toList()); 这是一个教程。 使用Collection#removeIf修改集合。 (注意:在这种情况下,谓词将删除满足谓词的对象): persons.removeIf(p -> p.getAge() <= 16); lambdaj允许在不编写

Programming in Java bytecode

I'm looking to write a short program (maybe a Hello World) in Java bytecode. I just want to write the bytecode using my text editor and run it. How would I do this? Got an example? Thanks! You could try Jasmin! .class public HelloWorld .super java/lang/Object .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 1 getstatic java/lang/System/out Ljav

用Java字节码编程

我正在用Java字节码编写一个简短的程序(也许是一个Hello World)。 我只想用我的文本编辑器编写字节码并运行它。 我将如何做到这一点? 有一个例子吗? 谢谢! 你可以试试茉莉! .class public HelloWorld .super java/lang/Object .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 1 getstatic java/lang/System/out Ljava/io/PrintStream; ldc "Hello World.

Thread Safe Object Fields in a Thread

I've got a problem with Java concurrency. Yes, I looked at questions with almost the exact same title, but they all seemed to be asking subtly different things. Yes, I've read Java Concurrency in Practice. Yes, I can see why it's the defacto reference for the topic. Yes, I've read the section specifically on publishing fields in thread-safe classes. Yes, I'm still going t

线程中的线程安全对象字段

我遇到了Java并发的问题。 是的,我用几乎完全相同的标题来看问题,但他们似乎都在提出细微差别的问题。 是的,我已阅读Java Concurrency in Practice。 是的,我可以看到为什么它是该主题的事实参考。 是的,我已经阅读了关于线程安全类中发布字段的专门章节。 是的,我仍然会问一个关于Java的并发问题,而不管我知道有人会直接指向那本书。 这让我难以忍受 - 我知道,通过确保正确的读/写顺序具有波动性和/或同步访问,

How do I format a string with properties from a bean

I want to create a String using a format, replacing some tokens in the format with properties from a bean. Is there a library that supports this or am I going to have to create my own implementation? Let me demonstate with an example. Say I have a bean Person ; public class Person { private String id; private String name; private String age; //getters and setters } I want to be abl

我如何使用bean的属性格式化字符串

我想使用格式创建一个字符串,用格式中的属性替换一些格式的标记。 有没有一个库支持这个或者我将不得不创建我自己的实现? 让我以一个例子来展示。 说我有一个豆Person ; public class Person { private String id; private String name; private String age; //getters and setters } 我希望能够指定类似的格式字符串; "{name} is {age} years old." "Person id {id} is called {name}." 并使用bean中的值自

(MapView and 1700 Overlays Items).equals(" Slow")

I have a MapView and I'm overlaying 1,700 points onto it, each with the same drawable but with different information. I'm currently using Itemized Overlay to add all the overlays then populate once fished. This works, but the performance is slow. Changing zoom level and focus is jumpy. Now, would it be any better to use ArrayItemizedOverlay since it's the same drawable, or would t

(MapView和1700覆盖项目).equals(“Slow”)

我有一个MapView ,我将1,700个点叠加到它上面,每个都有相同的可绘图,但具有不同的信息。 我目前使用Itemized Overlay来添加所有覆盖层,然后一旦捕获就填充。 这有效,但性能很慢。 改变缩放级别和焦点是跳跃式的。 现在,使用ArrayItemizedOverlay会更好吗,因为它是相同的drawable,否则地图会一样慢? import java.util.ArrayList; import android.app.AlertDialog; import android.content.Context; import android.

How to get JSON data in chunks to report on progress?

I need to download contact data via a REST API, which I get in JSON format. Issue is, it might be many many contacts, so I want to observe the progress (how many contacts have already been downloaded) and report back to the user (with a progress bar, the code below runs in a thread). However, it seems that the line client.execute(getRequest); establishes the connection and downloads the whole

如何获取块中的JSON数据来报告进度?

我需要通过REST API下载联系人数据,我使用JSON格式。 问题是,它可能是许多联系人,所以我想观察进度(已下载了多少联系人)并向用户报告(使用进度条,下面的代码在一个线程中运行)。 但是,似乎该行client.execute(getRequest); 建立连接并一次下载整个内容,即我的InputStream阅读器(以块形式获取内容)似乎没用。 现在我想知道如何使它在大块中工作,所以我可以报告进展情况? /** prepare HTML get request */ Ht

IRC: No Ident response

I am currently working on a IRC client written in Java. As soon as the connection to the server is established I send these messages: /NICK test /JOIN #chat The first tests went pretty well - the server is responding. But every time it says: NOTICE AUTH :*** Checking Ident NOTICE AUTH :*** No Ident response ERROR :Closing Link ... (Registration timed out) I found a related qu

IRC:无身份响应

我目前正在研究用Java编写的IRC客户端。 一旦建立到服务器的连接,我发送这些消息: / NICK测试 / JOIN#聊天 第一次测试进行得非常顺利 - 服务器正在响应。 但每次它说: 注意事项:*** 检查标识 注意事项:*** 没有标识响应 错误:关闭链接...(注册超时) 我发现一个相关的问题有一点帮助。 它说我需要在端口113上侦听连接,并从IRC-Server接收和响应消息。 我实现了一个侦听该端口的ServerSocket,但

Java socket output has delayed first message

I'm having a very strange problem with sockets in Java. It could be caused because of my lack of knowledge about sockets but here it is: I'm using a socket to connect to an IRC-server. The connection is made perfectly and I receive all the messages the IRC-server is sending me. When the connection is made, I authenticate to the server and I start a seperate thread to receive what the

Java套接字输出延迟了第一条消息

我在Java中使用套接字时遇到了一个非常奇怪的问题。 这可能是由于我对套接字缺乏了解而造成的,但这里是: 我使用套接字连接到IRC服务器。 连接完美,我收到IRC服务器发给我的所有消息。 在建立连接时,我向服务器进行身份验证,然后启动一个单独的线程来接收服务器发送给我的内容。 在该线程中,我连接时会发送一条消息,以使程序加入某个通道。 boolean joined = false; while ((line = getInput().readLine())

phonegap 1.0 android facebook plugin login fail

i'm adding facebook post button to my app (phonegap 1.0), it seems that the only method to have this working is using the plugin here: https://github.com/jos3000/phonegap-plugins/tree/master/Android/Facebook But it's not working for me, first i saw in the log PluginManager not found, reading the docs it seems plugins are not being loaded like that anymore. But the intent and dialog of

phonegap 1.0 android facebook插件登录失败

我将facebook post按钮添加到我的应用程序(phonegap 1.0)中,似乎只有这样做才能使用插件:https://github.com/jos3000/phonegap-plugins/tree/master/Android / Facebook的 但它没有为我工作,首先我看到日志PluginManager没有找到,阅读文档似乎插件不会像这样加载。 但是,当调用window.plugins.facebook.authorize(appID,function(res){...})时,facebook的意图和对话框会出现,我可以看到正在加载的Facebook页