MyObject我想根据字段过滤列表values。在这种情况下:如果 on 的任何值MyObject::getValues小于给定value,则谓词为 false。我还没有找到使用 Stream API 的方法,所以我尝试ComparatorPredicate了Apache. 这是我的尝试:
private Predicate<MyObject> valueGreaterThanOrEqualTo(ValueObject value){
return myObject -> myObject.getValues().stream()
.noneMatch(v -> ComparatorPredicate.comparatorPredicate(value, new ValueComparator(), ComparatorPredicate.Criterion.LESS)
.evaluate(v));
}
Run Code Online (Sandbox Code Playgroud)
然而,这会导致:
Caused by: java.lang.ClassCastException: Cannot cast java.lang.Boolean to com.ValueObject
at java.lang.Class.cast(Class.java:3369)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1351)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230)
at java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:196)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.noneMatch(ReferencePipeline.java:459)
Run Code Online (Sandbox Code Playgroud)
所以我的问题是为什么会发生 ClassCastException 以及如何解决它和/或如何使用其他(更好的?)解决方案解决我的问题?
更新: …
java predicate comparator apache-commons-collection java-stream
如何在 Spring JPA 规范中构建以下 SQL 查询?
SELECT col1, col2, MAX(col3)
FROM table_name t1
WHERE col4 IN (1,2,3) AND status IN ('STATUS_1','STATUS_2') AND
NOT EXISTS
(
SELECT 1 FROM table_name t2 WHERE t1.id = t2.parent_id
AND t2.status IN ('STATUS_3','STATUS_4')
)
GROUP BY col1, col2;
Run Code Online (Sandbox Code Playgroud)
爪哇代码:
return new Specification<TableEntity>() {
@Override
public Predicate toPredicate(Root<TableEntity> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
// how to build Predicates for the above query which has self-join and not exists.
}
};
Run Code Online (Sandbox Code Playgroud) 我的应用程序包含 aTextField和 a ListView。允许TextField用户输入搜索词,这些搜索词将在他们键入时过滤内容ListView。
过滤过程将匹配每个DataItem字段中的多个字段ListView,如果其中任何一个匹配,则返回结果。
然而,我想做的是让这些结果优先考虑与一个特定字段匹配的项目而不是其他字段。
例如,在下面的 MCVE 中,我有两项:Computer和Paper。该Computer项目有一个keyword“paper”,因此搜索“paper”应该返回Computer结果。
但是,由于我还有一个名为 的项目Paper,因此搜索应返回Paper到列表顶部。但在 MCVE 中,结果仍然按字母顺序排列:
问题:我如何确保DataItem.name上面列出的任何匹配项都与 a 匹配DataItem.keywords?
编辑:在搜索字段中输入“pap”也应该在顶部返回“Paper”,然后是其余的匹配项,因为部分搜索词与名称部分匹配DataItem。
MCVE
import java.util.List;
public class DataItem {
// Instance properties
private final IntegerProperty id = new SimpleIntegerProperty();
private final StringProperty name = new SimpleStringProperty();
private final StringProperty description = …Run Code Online (Sandbox Code Playgroud) 由于 C++14,我们std::less<void>在大多数情况下都是透明且更有用的,因此是否有原因,例如,默认情况下std::set仍将std::less<Key>其作为谓词,而不是std::less<void>历史原因除外。
有用的案例:std::set<std::string>::findwithstd::string_view等
对于某些文件操作,我需要检查文件是否存在,是否已被修改,然后才对其进行一些操作。我的新手 Haskell 代码如下(简化):
someFileOp ::FileContents -> FilePath -> IO (FileOpResult)
someFileOp contents absFilePath = do
fileExists <- DIR.doesFileExist absFilePath
if fileExists
then do
isMod <- isModified contents absFilePath
if isMod
then return FileModified
else return $ doSomethingWithFile
else return FileNotFound
Run Code Online (Sandbox Code Playgroud)
它确实有效。但是,嵌套的 if 表达式在我看来是错误的- 不像 FP。检查 IO 中的几个布尔条件然后根据它们的结果采取一些行动的惯用方法是什么?
我试图限制 Callable 在评估时返回布尔值。我一直在尝试使用这个概念std::predicate,但它似乎并没有达到我想要的效果。
所以我定义了自己的概念,它是可调用的并返回可转换为布尔值的东西。但同样,我很难理解我能用它做什么或不能做什么,我想知道它的实际用例是什么std::predicate?
#include<concepts>
#include<string>
template<class F, class... Args>
concept Predicate = std::invocable<F, Args...> &&
std::convertible_to<std::invoke_result_t<F, Args...>, bool>;
int main(int argc, char *argv[])
{
constexpr Predicate auto f1 = [](){return true;}; // ok
constexpr std::predicate auto f2 = [](){return true;}; // ok
constexpr int x = 34;
constexpr Predicate auto f3 = [x](){ return x==42;}; // ok
// Pas ok: error: deduced initializer does not satisfy placeholder constraints
//constexpr Predicate auto f4 = [](auto x){ …Run Code Online (Sandbox Code Playgroud) 我正在使用可视化编辑器来创建Core Data模型.我无法弄清楚如何设置一个fetched属性的谓词,以便它在谓词中使用源实体的属性.
例如,我有EntityA一个属性searchId.我也有EntityB一个属性id.我想要做的就是找到的所有实例EntityB,其id等于EntityA的searchIdatrribute.当我尝试编辑谓词时,左侧和右侧的下拉列表仅包含目标的键,在这种情况下是EntityB,并且无法从中选择任何键EntityA.
我知道你可以以编程方式创建谓词,但我想知道是否有办法使用可视化编辑器来完成它.
我有一个类的方法,我也希望能够用作谓词.
class MyClass {
bool ParticleHasAncestor(const Particle &particle, int id) const;
class AncestorPredicate {
int mId;
public:
AncestorPredicate(int idCode) : mId(idCode) { }
bool operator()(const Particle &particle) const { return ParticleHasAncestor(particle, mId); }
};
};
Run Code Online (Sandbox Code Playgroud)
但是,编译器抱怨没有ParticleHasAncestor()MyClass实例就无法使用.我需要使用朋友班吗?或者有更好的解决方案吗?
我没有使用C++ 11,所以不能使用lambda函数.
更新: ParticleHasAncestor()无法使其成为静态,因为它使用MyClass的成员.
在番石榴中,是否有方便将a转换Function<T, Boolean>为Predicate<T>?
在Functions,我可以看到,Function<T, Boolean> forPredicate(Predicate<T> predicate)但我没有看到任何朝向另一个方向的东西.
我现在要编写的代码是:
public static <T> Predicate<T> functionToPredicate(final Function<T, Boolean> func) {
return new Predicate<T>() {
@Override
public boolean apply(T input) {
return func.apply(input);
}
};
}
Run Code Online (Sandbox Code Playgroud) 我有一系列字典,我想找到具有键"guid"所需值的字典.无法使用此代码获取:
NSPredicate *filter = [NSPredicate predicateWithFormat:@"guid = %@", key];
NSArray *filteredContacts = [selectedValue filteredArrayUsingPredicate:filter];
Run Code Online (Sandbox Code Playgroud) predicate ×10
c++ ×3
java ×3
iphone ×2
arrays ×1
associative ×1
c++20 ×1
comparator ×1
containers ×1
core-data ×1
fetch ×1
guava ×1
haskell ×1
io-monad ×1
ios ×1
java-stream ×1
javafx ×1
nsdictionary ×1
stl ×1