本文介绍了用Google Guava过滤JavaBeans列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java程序中,我有一个我想根据特定属性过滤的bean列表。



例如,假设我有一个Person,一个JavaBean的列表,其中Person具有许多属性,其中包括'name'。



我也有一个名字列表。



现在我想查找所有姓名在名单中的人。



使用Google Guava执行此过滤器的最佳方法是什么?



到目前为止,我已经想过将Guava和Apache beanutils结合起来,但这看起来并不高雅。



我还在这里找到了反射扩展库: http://code.google.com/p/guava- / b>

有什么想法? b $ b

ps你能告诉我真的错过Python列表理解吗?

解决方案

(以Guava开发者的身份发言。)

  List< Person> filtered = Lists.newArrayList(); (Person p:allPersons){
if(acceptedNames.contains(p.getName())){
filtered.add(p);




$ b你可以用Guava做到这一点,但是Java不是' t Python,并试图将其转化为Python,只会使难以理解且不可读的代码永久存在。番石榴的功能性应用应该谨慎使用,只有当它们为代码或性能提供具体和可测量的益处时。


In a Java program, I have a list of beans that I want to filter based on a specific property.

For example, say I have a list of Person, a JavaBean, where Person has many properties, among them 'name'.

I also have a list of names.

Now I want to find all the persons whose name is in the name list.

What is the best way to execute this filter using Google Guava?

So far, I've thought about combining Guava with Apache beanutils, but that doesn't seem elegant.

I've also found a reflection extension library here: http://code.google.com/p/guava-reflection/, but I'm not sure how to use it (there's little documentation).

Any thoughts?

p.s. Can you tell I really miss Python list comprehension?

解决方案

Do it the old-fashioned way, without Guava. (Speaking as a Guava developer.)

List<Person> filtered = Lists.newArrayList();
for(Person p : allPersons) {
   if(acceptedNames.contains(p.getName())) {
       filtered.add(p);
   }
}

You can do this with Guava, but Java isn't Python, and trying to make it into Python is just going to perpetuate awkward and unreadable code. Guava's functional utilities should be used sparingly, and only when they provide a concrete and measurable benefit to either lines of code or performance.

这篇关于用Google Guava过滤JavaBeans列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:20