我有一个字符串的集合,我想将它转换为字符串集合全部为空或null删除字符串,其他所有字符串都被修剪.
我可以分两步完成:
final List<String> tokens =
Lists.newArrayList(" some ", null, "stuff\t", "", " \nhere");
final Collection<String> filtered =
Collections2.filter(
Collections2.transform(tokens, new Function<String, String>(){
// This is a substitute for StringUtils.stripToEmpty()
// why doesn't Guava have stuff like that?
@Override
public String apply(final String input){
return input == null ? "" : input.trim();
}
}), new Predicate<String>(){
@Override
public boolean apply(final String input){
return !Strings.isNullOrEmpty(input);
}
});
System.out.println(filtered);
// Output, as desired: [some, stuff, here]
Run Code Online (Sandbox Code Playgroud)
但是有没有一种将这两种行为合并为一步的番石榴方式?
我一直在JPA 1.0(Hibernate驱动程序)中使用Hibernate Restrictions.定义了Restrictions.ilike("column","keyword", MatchMode.ANYWHERE)哪些测试关键字是否匹配列的任何位置,并且它不区分大小写.
现在,我使用JPA 2.0和EclipseLink作为驱动程序,因此我必须使用"Restrictions"内置JPA 2.0.我发现CriteriaBuilder和方法like,我也发现了如何使它匹配任何地方(尽管它是令人讨厌和手动),但我仍然没有想出如何做它不区分大小写.
我目前有一个很好的解决方案:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User> query = builder.createQuery(User.class);
EntityType<User> type = em.getMetamodel().entity(User.class);
Root<User> root = query.from(User.class);
// Where
// important passage of code for question
query.where(builder.or(builder.like(root.get(type.getDeclaredSingularAttribute("username", String.class)), "%" + keyword + "%"),
builder.like(root.get(type.getDeclaredSingularAttribute("firstname", String.class)), "%" + keyword + "%"),
builder.like(root.get(type.getDeclaredSingularAttribute("lastname", String.class)), "%" + keyword + "%")
));
// Order By
query.orderBy(builder.asc(root.get("lastname")),
builder.asc(root.get("firstname")));
// Execute
return em.createQuery(query).
setMaxResults(PAGE_SIZE + 1).
setFirstResult((page - 1) * PAGE_SIZE).
getResultList();
Run Code Online (Sandbox Code Playgroud)
问题: …
def map = [name:"Gromit", likes:"cheese", id:1234]
Run Code Online (Sandbox Code Playgroud)
我想以这样一种方式访问地图,以便获得密钥
类似输出的东西应该是
map.keys返回字符串数组.基本上我只想拿钥匙
输出:
name
likes
id
Run Code Online (Sandbox Code Playgroud) 对于表示字符串,数字和布尔值的请求参数,Spring MVC容器可以将它们绑定到开箱即用的类型属性.
你如何让Spring MVC容器绑定一个表示Date的请求参数?
说到这一点,Spring MVC如何确定给定请求参数的类型?
谢谢!
我想这个从我的WAR文件中排除整个目录($ {BASEDIR}/src目录/主/ web应用/ webscripts),但它失败了.怎么了?
这不起作用:
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/webscripts</directory>
<excludes>
<exclude>**/*.*</exclude>
</excludes>
</resource>
</webResources>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这个也是:
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
<excludes>
<exclude>**/webscripts</exclude>
</excludes>
</resource>
</webResources>
</configuration>
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
我有一个带模块的maven项目
/myProject
pom.xml
/myModule
pom.xml
/foo
bar.txt
Run Code Online (Sandbox Code Playgroud)
考虑myModule中需要打开bar.txt的Junit ,使用maven,basedir是模块目录.
所以要打开文件bar.txt :
new File("foo/bar.txt")
Run Code Online (Sandbox Code Playgroud)
当你在intellij中启动相同的junit 时执行mvn test BUT时,这很有效,因为Intellij在项目目录中设置了basedir,而不是模块目录.
Intellij尝试打开myProject/foo/bar.txt而不是myProject/myModule/foo/bar.txt
有办法解决这个问题吗?
m2eclipse(0.10.0)和eclipse galileo(Build id:20090920-1017)有问题.
我总是收到错误消息:"Eclipse正在JRE中运行,但需要JDK".我尝试了几件事,但没有任何作用.错误消息仍然存在.以下是我尝试过的事情:
在Window> Preferences> Java> Installed JREs中,我检查了JDK1.6.0_20.什么都不行
在Window> Preferences> Java> Installed JRE中,我删除了所有JRE.只有已检查的JDK1.6.0_20仍然存在.什么都不行
在Window> Preferences> Java> Installed JREs> Execution Environments中我选择了JavaSE-1.6并检查了JDK1.6.0_20 [完全匹配].什么都不行.
在eclipse桌面启动图标的首选项中,我添加了-vm参数(C:\ Program\eclipse_galileo\eclipse\eclipse.exe -vm C:\ Program\Java\jdk1.6.0_20\bin).什么都不行.
我添加了clean参数(C:\ Program\eclipse_galileo\eclipse\eclipse.exe -vm C:\ Program\Java\jdk1.6.0_20\bin -clean).什么都不行.
我将-vm参数添加到eclipse.ini文件中,并在-vm后添加回车符,并在新行中添加C:/Programme/Java/jdk1.6.0_20/bin/javaw.exe.什么都不行.
完成所有这些操作后,我删除了m2eclipse插件并再次安装它.什么都不行.
我尝试过的新想法:
在eclipse桌面启动图标的首选项中,我将可执行文件放在最后(C:\ Program\eclipse_galileo\eclipse\eclipse.exe -vm C:\ Program\Java\jdk1.6.0_20\bin\javaw.exe).什么都不行.
我在eclipse.ini中将斜杠更改为反斜杠.什么都不行.
这是我的eclipse.ini文件:
-startup
plugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.0.200.v20090519
-product
org.eclipse.epp.package.jee.product
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
-vm
C:\Programme\Java\jdk1.6.0_20\bin\javaw.exe
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m
Run Code Online (Sandbox Code Playgroud)
是否有人有其他想法?任何帮助表示赞赏.
非常感谢你.GernoK
我之前从未使用过JodaTime,但回答了这个问题,如何在一个月内获得序数平日.
我尝试了它并想出了这个丑陋的代码来取消下面的所有字段:
DateTime startOfMonth =
input.withDayOfMonth(1)
.withHourOfDay(0) // there
.withMinuteOfHour(0) // has got to
.withSecondOfMinute(0) // be a shorter way
.withMillisOfSecond(0); // to do this
Run Code Online (Sandbox Code Playgroud)
Date startOfMonth = DateUtils.truncate(input, Calendar.MONTH);
Run Code Online (Sandbox Code Playgroud)
在JodaTime中,首选的成语是什么?
好吧,所以我现在已经看了一会儿,现在就没有了.我有一个Spring MVC servlet,我需要从JavaScript前端Web应用程序接受JSON.要解析JSON,我需要使用Jackson.我需要获取JSON中的值,并按照它们在JSON中出现的顺序将它们存储到List中.我已经尝试将JsonFactory与JsonParser和JsonNode对象一起使用,但可以让它完全正常工作.我还试图打开一个BufferedReader并逐行遍历请求体,但是再次也无法完成.我在这里看了几个相关的问题,但到目前为止都没有对我有用.
知道的任何人都可以在这里找到正确的方向吗,一个带有示例的网页会很棒!
我想将双引号附加到数组中的字符串,然后将它们作为单个字符串连接(保留引号).有没有这样做的字符串库?我已经尝试过Apache commons StringUtils.join和Google番石榴中的Joiner类,但找不到附加双引号的内容.
我的输入将是一个数组,如下所述:
String [] listOfStrings = {"day", "campaign", "imps", "conversions"};
Run Code Online (Sandbox Code Playgroud)
所需的输出应如下所述:
String output = "\"day\", \"campaign\", \"imps\", \"conversions\"";
Run Code Online (Sandbox Code Playgroud)
我知道我可以遍历数组并附加引号.但是如果有的话,我想要一个更清洁的解决方案.