JAVA 8: INTRODUCING STRINGJOINER

import java.util.StringJoiner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;

public class StringJoinerTest {

    @Test
    public void join() {
        String expected = "duke,java";
        StringJoiner sj = new StringJoiner(",");
        sj.add("duke").add("java");
        String actual = sj.toString();
        assertThat(actual, is(expected));
    }

    @Test
    public void shortCut() {
        String expected = "duke,java";
        String actual = String.join(",", "duke", "java");
        assertThat(actual, is(expected));

    }
}

<source: adam bien>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s