当前位置:网站首页>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边栏推荐
- mm_ Cognition of struct structure
- . Does net 4 have a built-in JSON serializer / deserializer- Does . NET 4 have a built-in JSON serializer/deserializer?
- AI should take code agriculture? Deepmind offers a programming version of "Alpha dog" alphacode that surpasses nearly half of programmers!
- Global and Chinese markets for soluble suture 2022-2028: Research Report on technology, participants, trends, market size and share
- 16.内存使用与分段
- [Chongqing Guangdong education] National Open University spring 2019 2727 tax basis reference questions
- C语言:围圈报号排序问题
- 昨天的事情想说一下
- Introduction to random and threadlocalrandom analysis
- Detailed explanation of NPM installation and caching mechanism
猜你喜欢
![[the way of programmer training] - 2 Perfect number calculation](/img/fd/4bb8560f601daddaa8895f20215be4.jpg)
[the way of programmer training] - 2 Perfect number calculation

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

C語言函數
![[data clustering] section 3 of Chapter 4: DBSCAN performance analysis, advantages and disadvantages, and parameter selection methods](/img/e6/2b46d72049ea50f89d0234eab88439.png)
[data clustering] section 3 of Chapter 4: DBSCAN performance analysis, advantages and disadvantages, and parameter selection methods

When synchronized encounters this thing, there is a big hole, pay attention!

16. Memory usage and segmentation

Flet教程之 02 ElevatedButton高级功能(教程含源码)(教程含源码)

Leetcode day 17

Memory computing integration: AI chip architecture in the post Moorish Era

R language -- readr package reads and writes data
随机推荐
MySQL advanced review
Entity framework calls Max on null on records - Entity Framework calling Max on null on records
Fly tutorial 02 advanced functions of elevatedbutton (tutorial includes source code) (tutorial includes source code)
Workplace liquor bureau must pay attention to
Guava ImmutableSet. Builder source code analysis, shift original code, complement code, reverse code review
Fastlane one click package / release app - usage record and stepping on pit
[Android reverse] function interception instance (③ refresh CPU cache | ④ process interception function | ⑤ return specific results)
Play Sanzi chess easily
Global and Chinese market for naval vessel maintenance 2022-2028: Research Report on technology, participants, trends, market size and share
When to use pointers in go?
The solution of permission denied
昨天的事情想说一下
I want to talk about yesterday
Servlet learning notes
Map container
13、 C window form technology and basic controls (3)
French Data Protection Agency: using Google Analytics or violating gdpr
16.内存使用与分段
Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution
It's hard to hear C language? Why don't you take a look at this (V) pointer