在Java 8中逐行将LinkedHashSet追加到TextArea(Append a LinkedHashSet to a TextArea line by line in Java 8)

编程入门 行业动态 更新时间:2024-10-11 09:30:40
在Java 8中逐行将LinkedHashSet追加到TextArea(Append a LinkedHashSet to a TextArea line by line in Java 8) java

我试图将LinkedHashSet附加到java 8中的textArea。下面的代码可以工作,但它不会逐行添加内容,而是将它们全部添加到一行中。

Code: textArea.setText(textArea.getText() + linkedHashSet); [ lin1, line2, line3, line4]

我希望将它们附加到下面列出的textArea

line1 line2 line3 line4

I am trying to append a LinkedHashSet to a textArea in java 8. The code below works but it doesn't add the content line by line, and it adds them all in one line.

Code: textArea.setText(textArea.getText() + linkedHashSet); [ lin1, line2, line3, line4]

I am looking to append them to the textArea as listed below

line1 line2 line3 line4

最满意答案

由于这是Java 8,您可以使用:

textArea.setText( textArea.getText() + String.join( System.lineSeparator(), linkedHashSet ));

各种形式的String.join ,这个接受分隔符和Iterable ,允许您将元素的字符串表示与分隔符连接在一起 - 在本例中为行分隔符。

通常,如果您的Iterable (set,list)包含元素“A”,“B”和“C”,并且您想要将它们加入,则使用:

String.join( ",", myIterable );

这会给你:

A,B,C
 

如果第一个参数是---BIG DELIMITER---那么你会得到:

A---BIG DELIMITER---B---BIG DELIMITER---C
 

但在我们的例子中,我们放入了System.lineSeparator() ,这意味着你的字符串将由\n (在Linux机器上)或\r\n (在Windows机器上)分隔。 这意味着每个值都将在一个单独的行上:

A
B
C

Since this is Java 8, you can use:

textArea.setText( textArea.getText() + String.join( System.lineSeparator(), linkedHashSet ));

The various forms of String.join, this one accepting a delimiter and an Iterable, let you join together the elements' string representations with a delimiter - in this case a line separator.

Usually, if your Iterable (set, list) has elements "A", "B" and "C", and you want to join them with ,, you use:

String.join( ",", myIterable );

And this will give you:

A,B,C
 

If the first parameter was ---BIG DELIMITER---, then you would get:

A---BIG DELIMITER---B---BIG DELIMITER---C
 

But in our case, we put in the System.lineSeparator(), which means that your strings will be separated by \n (on Linux machines), or \r\n (on Windows machines). This means that each of the values will be on a separate line:

A
B
C

                    
                     
          

更多推荐

本文发布于:2023-07-15 07:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LinkedHashSet   Java   TextArea   line   Append

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!