当前位置:网站首页>Is there an elegant way to remove nulls while transforming a Collection using Guava?
Is there an elegant way to remove nulls while transforming a Collection using Guava?
2022-07-04 12:41:00 【Rich in starch】
problem :
I have a question about simplifying some Collection handling code, when using Google Collections ( update : Guava ). I have a question about using Google Collections Simplify when Collection Dealing with code problems ( to update : Guava ).
I've got a bunch of "Computer" objects, and I want to end up with a Collection of their "resource id"s. I have a bunch “ Computer ” object , I want to finally get them “ resources ID” Set .This is done like so: This is done :
Collection<Computer> matchingComputers = findComputers();Collection<String> resourceIds = Lists.newArrayList(Iterables.transform(matchingComputers, new Function<Computer, String>() { public String apply(Computer from) { return from.getResourceId(); }}));Now, getResourceId() may return null (and changing that is not an option right now), yet in this case I'd like to omit nulls from the resulting String collection. Now? , getResourceId() May return to null( And changing it now is not an option ), But in this case , I think from the result String Omit from set null.
Here's one way to filter nulls out: This is a way to filter null values :
Collections2.filter(resourceIds, new Predicate<String>() { @Override public boolean apply(String input) { return input != null; }});You could put all that together like this: You can put all these together like this :
Collection<String> resourceIds = Collections2.filter(Lists.newArrayList(Iterables.transform(matchingComputers, new Function<Computer, String>() { public String apply(Computer from) { return from.getResourceId(); }})), new Predicate<String>() { @Override public boolean apply(String input) { return input != null; }});But this is hardly elegant, let alone readable, for such a simple task! But this is not elegant for such a simple task , Not to mention readable !In fact, plain old Java code (with no fancy Predicate or Function stuff at all) would arguably be much cleaner: in fact , Ordinary old Java Code ( There is no fancy Predicate or Function thing ) It can be said that it will be cleaner :
Collection<String> resourceIds = Lists.newArrayList();for (Computer computer : matchingComputers) { String resourceId = computer.getResourceId(); if (resourceId != null) { resourceIds.add(resourceId); }}Using the above is certainly also an option, but out of curiosity (and desire to learn more of Google Collections), can you do the exact same thing in some shorter or more elegant way using Google Collections ? Of course, using the above is also an option , But out of curiosity ( And hope to learn more Google Collections), You can use Google Collections Do exactly the same thing in a shorter or more elegant way Do you ?
Solution :
Reference resources : https://stackoom.com/en/question/7Ywf边栏推荐
- Fly tutorial 02 advanced functions of elevatedbutton (tutorial includes source code) (tutorial includes source code)
- Flet教程之 按钮控件 ElevatedButton入门(教程含源码)
- Leetcode: 408 sliding window median
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
- Awk getting started to proficient series - awk quick start
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
- The database connection code determines whether the account password is correct, but the correct account password always jumps to the failure page with wrong account password
- Xshell's ssh server rejected the password, failed to skip publickey authentication, and did not register with the server
- Method of setting default items in C # ComboBox control code
- Global and Chinese markets for soluble suture 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Detailed explanation of NPM installation and caching mechanism

Paper notes ACL 2020 improving event detection via open domain trigger knowledge

Introduction to the button control elevatedbutton of the fleet tutorial (the tutorial includes the source code)

Bottom Logic -- Mind Map

Wechat video Number launches "creator traffic package"

C fonctions linguistiques

Single spa, Qiankun, Friday access practice

MySQL advanced review

Fly tutorial 02 advanced functions of elevatedbutton (tutorial includes source code) (tutorial includes source code)

Review of week 278 of leetcode II
随机推荐
Iframe to only show a certain part of the page
queue
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
Jetson TX2配置Tensorflow、Pytorch等常用库
Fly tutorial 02 advanced functions of elevatedbutton (tutorial includes source code) (tutorial includes source code)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
LVS load balancing cluster deployment - Dr direct routing mode
Paper notes ACL 2020 improving event detection via open domain trigger knowledge
[ES6] template string: `string`, a new symbol in es2015
Sort merge sort
MYCAT middleware installation and use
First knowledge of spark - 7000 words +15 diagrams, and learn the basic knowledge of spark
SAP ui5 date type sap ui. model. type. Analysis of the display format of date
R语言--readr包读写数据
Abnormal mode of ARM processor
nn. Exploration and experiment of batchnorm2d principle
C language: the sorting problem of circle number reporting
Translation D29 (with AC code POJ 27:mode of sequence)
Bottom Logic -- Mind Map