当前位置:网站首页>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边栏推荐
- Star leap plan | new projects are continuously being recruited! MSR Asia MSR Redmond joint research program invites you to apply!
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
- PKCs 5: password based cryptography specification version 2.1 Chinese Translation
- Fly tutorial 02 advanced functions of elevatedbutton (tutorial includes source code) (tutorial includes source code)
- Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- Experiment 7. IPv6
- Leetcode day 17
- 01. Basics - MySQL overview
- Uva536 binary tree reconstruction tree recovery
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
猜你喜欢

LVS load balancing cluster deployment - Dr direct routing mode

轻松玩转三子棋

Leetcode: 408 sliding window median

R language -- readr package reads and writes data
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17](/img/85/2635afeb2edeb0f308045edd1f3431.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17

记一次 Showing Recent Errors Only Command /bin/sh failed with exit code 1 问题

17.内存分区与分页

Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution

Show recent errors only command /bin/sh failed with exit code 1

MPLS experiment
随机推荐
IIS error, unable to start debugging on the webserver
It's hard to hear C language? Why don't you take a look at this (V) pointer
Openssl3.0 learning 20 provider KDF
Entitas learning [3] multi context system
Iterm tab switching order
认知的定义
The most robust financial products in 2022
C language: find the palindrome number whose 100-999 is a multiple of 7
16. Memory usage and segmentation
Practice of retro SOAP Protocol
记一次 Showing Recent Errors Only Command /bin/sh failed with exit code 1 问题
World document to picture
Clockwise rotation method of event arrangement -- PHP implementation
Fly tutorial 02 advanced functions of elevatedbutton (tutorial includes source code) (tutorial includes source code)
[Chongqing Guangdong education] National Open University spring 2019 2727 tax basis reference questions
Entitas learning [iv] other common knowledge points
VIM, another program may be editing the same file If this is the solution of the case
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
[solve the error of this pointing in the applet] SetData of undefined
Introduction to the button control elevatedbutton of the fleet tutorial (the tutorial includes the source code)