简体   繁体   English

如何从 String 中过滤掉 Unique 元素并将它们存储到 ArrayList

[英]How filter out Unique elements from a String and store them into an ArrayList

Given a filtered string.给定一个过滤的字符串。 I want to add its substrings to an ArrayList only if it doesn't contain it already.我只想将其子字符串添加到ArrayList中,前提是它尚未包含它。

My code:我的代码:

Pattern reg = Pattern.compile(",");
ArrayList<String> SANR = reg.splitAsStream(joiner.toString())    
        .filter(role -> role.contains("ABB"))
        .map(str -> str.replaceAll("ABB", ""))
        .collect(Collectors.toCollection(ArrayList::new));

As input, it gets all the values with ABB appended such as [123,123,244,244,255] , so I want to make sure the output is just:作为输入,它获取所有附加了ABB的值,例如[123,123,244,244,255] ,所以我想确保输出只是:

[123,244,255]

You need to apply distinct() operation to ensure that the result doesn't contain duplicates.您需要应用distinct()操作以确保结果不包含重复项。 To achieve it and preserve the initial order of elements in the stream, it uses LinkedHashSet under the hood.为了实现它并保留流中元素的初始顺序,它在后台使用LinkedHashSet

Don't use ArrayList as a type, it makes your code rigid.不要将ArrayList用作类型,它会使您的代码变得僵硬。 See What does it mean to "program to an interface"?请参阅“编程到接口”是什么意思?

By the way, Collectors.toList() as well as Java 16 toList() operation will actually give you an ArrayList , but your code should not depend on it.顺便说一句, Collectors.toList()以及 Java 16 toList()操作实际上会给你一个ArrayList ,但你的代码不应该依赖它。 If the in future there would be designed a better general purpose implementation of the List interface which will be capable of everything that ArrayList does, you'll get it for free if you would not demand specifically an ArrayList .如果将来会设计一个更好的List接口的通用实现,它能够完成ArrayList所做的一切,如果您不特别要求ArrayList ,您将免费获得它。 Write your code against interfaces.针对接口编写代码。

Pattern reg = Pattern.compile(",");
List<String> sanr = reg.splitAsStream(joiner.toString())
    .filter(role -> role.contains("ABB"))
    .map(str -> str.replaceAll("ABB" , ""))
    .distinct()
    .collect(Collectors.toList());

Sidenote: by convention uppercase-names should be used for constant fields (ie marked as final ), for local values use camel-case starting with a lower-case letter.旁注: 按照惯例,大写名称应该用于常量字段(即标记为final ),对于本地值,使用以小写字母开头的驼峰式命名。 And names by itself should be meaningful, if sanr is a some kind abbreviation it should be documented somewhere.并且名称本身应该是有意义的,如果sanr是某种缩写,它应该记录在某处。

If it fits your use case, I would suggest using a Set abstract data type instead of an ArrayList for this purpose.如果它适合您的用例,我建议为此使用 Set 抽象数据类型而不是 ArrayList。 It allows you to not store any duplicates in the list of Strings.它允许您不在字符串列表中存储任何重复项。

Check out: Java Sets签出: Java 集

If you'd like to keep it as an ArrayList you can do:如果您想将其保留为 ArrayList ,您可以执行以下操作:

myArrayList = new ArrayList<String>(new LinkedHashSet<String>(myArrayList));

to delete any duplicates from "myArrayList"从“myArrayList”中删除任何重复项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 Arraylist<string> 还包含数字,我该如何过滤掉它们?</string> - If an Arraylist<String> also contains numbers, how can I filter them out? 过滤掉以给定 4 位开头的元素并将它们存储到列表中 - Filter out the elements that Start with given 4 digit and store them into a List 从文件中读取元素,并将其作为对象存储在ArrayList中 - Read elements from a file and store them in ArrayList as Objects 如何从arrayList中删除唯一元素 - how to remove unique elements from arrayList 如何从ArrayList获取String值并将它们存储在Java 8中用逗号分隔的单个字符串中? - How to get String values from ArrayList and store them in a single string separated by commas, in Java 8? 如何从 ArrayList 存储字符串值<String>到一个 ArrayList<Object> 对象 - How to store String values from an ArrayList<String> to an ArrayList<Object> of Objects 从Java中的ArrayList导出唯一元素 - import unique elements out of ArrayList in Java 如何从ArrayList中获取所有元素 <HashMap<String, String> &gt;安卓 - How to get all the elements out of ArrayList<HashMap<String, String>> android 如何从 arraylist 中过滤部分字符串 - How to filter parts of a string from an arraylist 如何将数据从Excel存储到ArrayList <ArrayList<String> &gt; - How to store data from excel to ArrayList<ArrayList<String>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM