如何在Jython中使用Java 8 Lambda

编程入门 行业动态 更新时间:2024-10-09 18:17:59
本文介绍了如何在Jython中使用Java 8 Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在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

本文发布于:2023-11-28 04:49:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Jython   Lambda   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!