Java 将字符串转换为 List<Long>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19946980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:45:39  来源:igfitidea点击:

Convert string to List<Long>

java

提问by user2986404

I have a string with comma separated value that I am getting direclty from database. Now I want to pass that entire string to another query but the datatype required is long and i want to use in clause to get this done.

我有一个逗号分隔值的字符串,我直接从数据库中获取。现在我想将整个字符串传递给另一个查询,但所需的数据类型很长,我想使用 in 子句来完成这项工作。

str1 = [123,456,789];

Is there any direct way to do it instead of looping.

有没有什么直接的方法可以做到而不是循环。

采纳答案by Kamlesh Arya

With guava:

随着番石榴

List<Long> longs = Lists.newArrayList(Iterables.transform(Splitter.on(',').split("1,2,3"), new Function<String, Long>() {
    public Long apply(final String in) {
        return in == null ? null : Longs.tryParse(in);
    }
}));

EDIT:

编辑:

With a List<String>(stringList) as input:

List<String>( stringList) 作为输入:

List<Long> longs = Lists.newArrayList(Lists.transform(stringList, new Function<String, Long>() {
    public Long apply(final String in) {
        return in == null ? null : Longs.tryParse(in);
    }
}));

回答by Kamlesh Arya

you can try below code to get long value from a string

您可以尝试以下代码从字符串中获取长值

if str1="123,456,789"

如果 str1="123,456,789"

 Long x=Long.parseLong(str1.replace(",",""));

and,

和,

if str1="[123,456,789]"

如果 str1="[123,456,789]"

Long x=Long.parseLong(str1.replace(",|\[|\]",""));

回答by Ankur Shanbhag

I doubt that you can do this without looping. Here is a sample workaround:

我怀疑您是否可以在不循环的情况下执行此操作。这是一个示例解决方法:

String str = "[123,456,789]";


Pattern pattern = Pattern.compile("\d+");
Matcher matcher = pattern.matcher(str);

List<Long> list = new ArrayList<Long>();

 while (matcher.find()) {
System.out.println(matcher.group());
list.add(Long.parseLong(matcher.group())); // Add the value to the list
}
System.out.println(list);

回答by thermite

Use String.replace assuming str1 contains "[123,123,123]"

使用 String.replace 假设 str1 包含“[123,123,123]”

str1 = str1.replace(",|\[|\]",""); //should replace commas and brackets with nothing;

then you should be able to do

那么你应该能够做到

Long myLong =  Long.parseLong(str1);

回答by Vignesh

Use StringTokenizer and add split tokens to a list with a while loop.

使用 StringTokenizer 并使用 while 循环将拆分标记添加到列表中。

StringTokenizer st = new StringTokenizer("1,2,3", ",");
List<Long> list = new ArrayList<Long>();
while (st.hasMoreTokens()) 
{
     list.add(Long.valueOf(st.nextToken()));
}

回答by virag

You can use the Lambda functions of Java 8 to achieve this without looping

您可以使用 Java 8 的 Lambda 函数来实现此目的而无需循环

    String string = "1, 2, 3, 4";
    List<Long> list = Arrays.asList(string.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());

回答by Bienvenido David

A little shorter than virag's answer, as it skips asList() and doesn't do a string trim.

比 virag 的答案短一点,因为它跳过 asList() 并且不进行字符串修剪。

List<Long> list = Arrays.stream(str.split(",")).map(Long::valueOf).collect(Collectors.toList());