0

I'm trying to convert the values of a linked list's nodes into a single string. For example:

"c" -> "a" -> "t"

So when I use the built in toString method, I get this as an output.

"[c, a, t]"

Where the entire thing is a string. Is there any method that allows me to merge this into a single string, like so?

"cat"
Maroun
  • 91,013
  • 29
  • 181
  • 233
Lang Tran
  • 1
  • 1
  • 1
    Override the `toString` method and use `replaceAll("\\W+", "")` to remove all non [a-zA-Z0-9_] characters. – Maroun Feb 23 '16 at 09:36
  • [`String.join()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#join-java.lang.CharSequence-java.lang.Iterable-) – khelwood Feb 23 '16 at 09:41
  • @MarounMaroun, so I run `super()` in my override method, and then I add the `replaceall` to my override method? – Lang Tran Feb 23 '16 at 19:21

2 Answers2

0

If you use Java 8, you can use joining() collector:

LinkedList<String> strings = new LinkedList<>();
strings.add("c");
strings.add("a");
strings.add("t");

System.out.println(strings.stream().collect(Collectors.joining()));

output: "cat".

Hoopje
  • 12,378
  • 7
  • 31
  • 48
0
        LinkedList<String> list=new LinkedList<String>();
        list.add("c");
        list.add("a");
        list.add("t");
        System.out.println(list.toString().replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\, ", ""));
Musaddique
  • 1,435
  • 2
  • 13
  • 30