如何使用 apache Velocity 从列表中删除重复元素
我有一个包含重复元素的列表,我需要使用速度
例如,帖子包含重复元素
#foreach ($p in $posts)
$p.name //will be unique
#end
我想使用速度删除重复元素,
任何帮助将不胜感激
I have a list with duplicate elements,I need to use velocity
For Example, posts contains duplicate elements
#foreach ($p in $posts)
$p.name //will be unique
#end
I want to remove the duplicate using velocity,
Any help would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是可能的,并且这应该取决于您的速度版本。比上面的答案更简洁一些。
This is possible, and this should work depending on your version of velocity. A bit more concise than the answer above.
只是为了争论,因为其他人说 Velocity 不可能,我想证明 Velocity 实际上是可能的,但仍然不推荐。
对于那些有兴趣如何做到这一点的人:
Just for the sake of argument because others said it is not possible with Velocity, I wanted to show that it is actually possible with Velocity, but still not recommended.
For those who are interested how it could be done:
你无法以速度做到这一点。您必须提供一个不包含重复项的模型。最简单的方法是使用 new HashSet(postsList) - 这将消除重复项(基于
equals(..)
方法)如果你真的可以的话如果没有通过正确的模型,您可以尝试定义一个 自定义工具,它接受一个列表并返回一套,但是这并不容易。
You can't do that in velocity. You have to provide a model that contains no duplicates. The easiest way is to use
new HashSet<Post>(postsList)
- this will eliminate the duplicates (based on theequals(..)
method)If you really can't pass the proper model, you can try to define a custom tool that takes a list and returns a set, but that wouldn't be easy.
除了在 Velocity 中不可能实现之外,从架构的角度来看,您想要的根本没有意义。 “删除重复项”部分是某种逻辑,需要在适当的位置进行处理。视图不是执行此操作的正确位置。因此,您应该尽一切努力在 Java 中做到这一点,甚至庆幸这在 Velocity 中是不可能的。
即使您的角色不允许更改 Java 代码,这个问题仍然必须在 Java 中解决。
Aside from not being possible in Velocity, from an architectural point of view what you want doesn't make sense at all. The "removing duplicates" part is some sort of a logic and this needs to be taken care of in the proper place. A view is not the right place for doing that. So you should do it by all means in Java and even be happy it's not possible in Velocity.
Even if your role does not allow for changing Java code, this must still be solved in Java.