问题描述
我正在尝试在jython中使用java 8流和集合,以查看在纯jython中实现时它们是否有效.在我看来,它可以(对此表示赞赏)
我从一些例子开始,计数
from java.util.function import Function
from java.util import ArrayList
from java.util.stream import Collectors
letters = ArrayList(['a','b','a','c']);
cnt=letters.stream().collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
将cnt打印为词典 {u'a':2升,u'b':1升,u'c':1升}
到目前为止,一切都很好.接下来,我找到了一个在java 的流上使用过滤器的示例List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
//get count of empty string
int count = strings.stream().filter(string -> string.isEmpty()).count();
这将如何转换为jython.具体来说,如何在jython中编写类似字符串-> sting.isEmpty()之类的Java lambda表达式?
以下是在流中使用过滤器的示例,该过滤器需要Predicate(java.util.function.Predicate)类型的对象
对于Java代码:
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
//get count of empty string
int count = strings.stream().filter(string -> string.isEmpty()).count();
eqvivalet jython将首先对谓词进行子类化并实现一种测试方法.
from java.util.function import Predicate
from java.util.stream import Collectors
class pred(Predicate):
def __init__(self,fn):
self.test=fn
@pred
def mytest(s):
from java.lang import String
return not String(s).isEmpty() #or just use len(s.strip())==0
strings = ArrayList(["abc", "", "bc", "efg", "abcd","", "jkl"])
count = strings.stream().filter(mytest).count()
lst=strings.stream().filter(mytest).collect(Collectors.toList())
print(count)
print(lst)
然后打印
计数:
5L
第一:
[abc, bc, efg, abcd, jkl]
I am trying to experiment with java 8 streams and collections in jython to see if they are any efficient then when implemented in pure jython. It occurs to me it could (any comments on this also appreciated)
I started with some examples, the counting
from java.util.function import Function
from java.util import ArrayList
from java.util.stream import Collectors
letters = ArrayList(['a','b','a','c']);
cnt=letters.stream().collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
printing cnt as dictionary {u'a': 2L, u'b': 1L, u'c': 1L}
so far so good. Next, I found a example on using filter on streams in java
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
//get count of empty string
int count = strings.stream().filter(string -> string.isEmpty()).count();
how would this translate to in jython. specifically how can one write java lambda expression like string -> sting.isEmpty() in jython?
here is an example for using a filter on a stream need a object of type Predicate (java.util.function.Predicate)
for java code:
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
//get count of empty string
int count = strings.stream().filter(string -> string.isEmpty()).count();
eqvivalet jython would be to first subclassing Predicate and implementing a test method.
from java.util.function import Predicate
from java.util.stream import Collectors
class pred(Predicate):
def __init__(self,fn):
self.test=fn
@pred
def mytest(s):
from java.lang import String
return not String(s).isEmpty() #or just use len(s.strip())==0
strings = ArrayList(["abc", "", "bc", "efg", "abcd","", "jkl"])
count = strings.stream().filter(mytest).count()
lst=strings.stream().filter(mytest).collect(Collectors.toList())
print(count)
print(lst)
then prints
count:
5L
lst:
[abc, bc, efg, abcd, jkl]
这篇关于如何在Jython中使用Java 8 Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!