当前位置:网站首页>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边栏推荐
- Guava ImmutableSet. Builder source code analysis, shift original code, complement code, reverse code review
- VIM, another program may be editing the same file If this is the solution of the case
- What if the chat record is gone? How to restore wechat chat records on Apple Mobile
- PKCs 5: password based cryptography specification version 2.1 Chinese Translation
- [ES6] template string: `string`, a new symbol in es2015
- Complementary knowledge of auto encoder
- priority_ queue
- Jetson TX2 configures common libraries such as tensorflow and pytoch
- C语言函数
- When synchronized encounters this thing, there is a big hole, pay attention!
猜你喜欢
DC-5 target
When synchronized encounters this thing, there is a big hole, pay attention!
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
Data communication and network: ch13 Ethernet
13、 C window form technology and basic controls (3)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
What if the chat record is gone? How to restore wechat chat records on Apple Mobile
0x15 string
随机推荐
Workplace liquor bureau must pay attention to
World document to picture
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
'using an alias column in the where clause in PostgreSQL' - using an alias column in the where clause in PostgreSQL
Iframe to only show a certain part of the page
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
C fonctions linguistiques
Global and Chinese market of dental elevators 2022-2028: Research Report on technology, participants, trends, market size and share
C语言:求字符串的长度
Entitas learning [3] multi context system
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
SAP ui5 date type sap ui. model. type. Analysis of the display format of date
LVS load balancing cluster deployment - Dr direct routing mode
Complementary knowledge of auto encoder
Kivy tutorial 08 countdown app implements timer call (tutorial includes source code)
MySQL advanced review
A few words explain redis cache penetration, breakdown, avalanche, and redis sentinel
Netgear switch basic configuration command set
When synchronized encounters this thing, there is a big hole, pay attention!
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24