0

I have an array list

List<String> items = Arrays.asList("date=2017-01-10", "date=2017-02-10");

I want to convert this list and have quotes for each item in the list so date=2017-01-10 can be converted to date='2017-01-10'

List<String> items = Arrays.asList("date='2017-01-10'", "date='2017-02-10'");

How can i do in Java 8 ?

UPDATE: I want to use regex to achieve this.

6
  • To do it in Java 8, try first doing it in Java 7 with regex, and then... leave it as is, because Java8 doesn't bring anything new to this table other than minor quality of life improvements. Commented Aug 15, 2017 at 10:13
  • 1
    @M.Prokhorov, Streams? Commented Aug 15, 2017 at 10:24
  • @Clijsters, the definition of "quality of life improvement" for this task. There's nothing you can achieve with Stream that you couldn't achieve with Java7-style code. Commented Aug 15, 2017 at 10:28
  • Sure, I'd however tell it bad style, not use java8-style code in a java-8 code environment. Commented Aug 15, 2017 at 10:30
  • 1
    @Clijsters, The bad style in my opinion is trying to shoe-horn lambdas and streams literally everywhere, regardless of how they look and behave. For example, a Stream creates way more garbage than side-effectful forEach, and the actual for-each Java7-style creates almost no garbage. Commented Aug 15, 2017 at 10:34

3 Answers 3

3

You can use replaceAll with this regex ^(.*?)=(.*?)$ which return two groups one before the = the second after, so you can replace the input with group 1 $1 followed by =' then group 2 between two quotes '$2':

List<String> items = Arrays.asList("date=2017-01-10", "date=2017-02-10");
items = items.stream()
        .map(t -> t.replaceAll("(.*?)=(.*?)$", "$1='$2'"))
        .collect(Collectors.toList());

items.forEach(System.out::println);

Outputs

date='2017-01-10'
date='2017-02-10'
Sign up to request clarification or add additional context in comments.

Comments

2

I have seen your profile, I found you start to Java. so I'll give you the answer just for learning purpose.

  • use lookbehind to skip replacement. e.g: (?<==)
  • use group to capture the matched result in group, e.g: (.+)
  • use $ sign to references to captured group, e.g: $1.

    List<String> result = items.stream()
    //                                 match `=`            ---v
                               .map(it -> it.replaceFirst("(?<==)(.+)", "'$1'"))
    //                     capture the rest substring in group  ---^
                               .collect(Collectors.toList());
    

You also can simply to use String#substring to achieve your way. for example:

int prefix = "date=".length();
List<String> result =items.stream()
                          .map(it->String.format("date='%s'",it.substring(prefix)))
                          .collect(Collectors.toList());

Comments

0

you can use the following code

    List<String> items = Arrays.asList("date=2017-01-10", "date=2017-02-10"), result = new ArrayList<String>();
    for(String item : items){
        item = item.replace("date=", "date='");
        item = item + "'";
        result.add(item);
    }
    for(String item : result){
        System.out.println(item);
    }

demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.