12

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.

str1 = [123,456,789];

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

Werner Kvalem Vesterås
  • 10,226
  • 5
  • 43
  • 50
user2986404
  • 121
  • 1
  • 1
  • 4
  • To my knowledge the only method is looping. I would store them in a LinkedList for O(N*M) where M is the length of each number (cost for converting string to long) and N is the number of elements – Vineet Kosaraju Nov 13 '13 at 06:38
  • Can you show the queries you're attempted to run? What RDBMS is this? – Mureinik Nov 13 '13 at 06:40
  • 1
    When you say "comma separated value", are you talking about `.csv` file format, or numbers written for human consumption with a convention of commas between every group of 3 digits? – Robin Green Nov 13 '13 at 06:45
  • @user2986404 If you are asking about limit of characers with IN clause: Check here http://stackoverflow.com/questions/1869753/maximum-size-for-a-sql-server-query-in-clause-is-there-a-better-approach – Nitin Dandriyal Nov 13 '13 at 07:37
  • or if you could use jdbctemplate: http://stackoverflow.com/questions/1327074/how-to-execute-in-sql-queries-with-springs-jdbctemplate-effectivly – Nitin Dandriyal Nov 13 '13 at 07:39

7 Answers7

12

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

    String string = "1, 2, 3, 4";
    List<Long> list = Arrays.asList(string.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
virag
  • 562
  • 6
  • 9
  • 1
    A slightly improved version: Stream.of(input.split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()); – naren May 23 '18 at 07:35
9

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

List<Long> list = Arrays.stream(str.split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
Stephan
  • 41,764
  • 65
  • 238
  • 329
Bienvenido David
  • 4,118
  • 1
  • 25
  • 16
6

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<Long> longs = Lists.newArrayList(Lists.transform(stringList, new Function<String, Long>() {
    public Long apply(final String in) {
        return in == null ? null : Longs.tryParse(in);
    }
}));
  • Hi Robin, this is giving me numberFormatException when I am passing the string. Actaully its List that I changed to String from toString() method. Or is there a way I can directlt use thie List to change to List? – user2986404 Nov 13 '13 at 07:32
2

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);
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
2

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

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

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

if str1="123,456,789"

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

and,

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

Long x=Long.parseLong(str1.replace(",|\\[|\\]",""));
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
0

Use String.replace assuming str1 contains "[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);
thermite
  • 502
  • 6
  • 19
  • I am assuming the questioner means comma separated value in the sense of `.csv` file format, not in the sense of a number written for human consumption with commas between every 3 digits, but you could be right! – Robin Green Nov 13 '13 at 06:44