英文:
How to get special part of string using java streams?
问题
我有一个名为abbreviations.txt的文件,其中有特殊信息:
示例 abbreviations.txt 内容:
PCAP_Personal Computer_Apple
NBHP_NoteBook_Hewlett Packard
TVSG_Televisor_Samsung
我需要将所有品牌名称放入一个新的字符串列表中。我尝试使用以下代码:
Stream<String> abbreviations = Files.lines(Paths.get("src/main/resources/raceData/abbreviations.txt"))
.flatMap(Pattern.compile("_")::splitAsStream);
List<String> dates = abbreviations.collect(Collectors.toList());
dates.forEach(System.out::println);
但是作为列表返回的结果是:
PCAP_Personal Computer
Apple
TVSG_Televisor
Samsung
NBHP_NoteBook
Hewlett Packard
英文:
I have file abbreviations.txt where I have special infos:
Example abbreviations.txt :
PCAP_Personal Computer_Apple
NBHP_NoteBook_Hewlett Packard
TVSG_Televisor_Samsung
and I need to get all brand names into new List String. Im trying use this:
Stream<String> abbreviations = Files.lines(Paths.get("src/main/resources/raceData/abbreviations.txt"))
.flatMap(Pattern.compile("_")::splitAsStream);
List<String> dates = abbreviations.collect(Collectors.toList());
dates.forEach(System.out::println);
But as List I get:
PCAP_Personal Computer
Apple
TVSG_Televisor
Samsung
NBHP_NoteBook
Hewlett Packard
答案1
得分: 5
splitAsStream
?
不必要的。只需使用:
.map(line -> line.split("_")[2])
当然,这假设所有行都具有正确的格式。你当然可以过滤掉格式错误的行,例如使用:
.map(line -> line.split("_"))
.filter(parts -> parts.length >= 3)
.map(parts -> parts[2])
然后,你可以将其放入数组中,使用 toArray
。然而,我建议使用 List
,因为绝大多数实现要强大得多。你可以使用 Collector
进行操作:
.collect(Collectors.toList());
英文:
splitAsStream
?
That's not necessary. Just use
.map(line -> line.split("_")[2])
This of course assumes that all lines have the correct format. You could of course filter out malformed lines, for example, using
.map(line -> line.split("_"))
.filter(parts -> parts.length >= 3)
.map(parts -> parts[2])
You could then put it into an array, using toArray
. However, I recommend to use a List
instead, because by far the most implementations are way more powerful. You can do so using a Collector
:
.collect(Collectors.toList());
答案2
得分: 1
如果品牌名称是在最后一个'_'
之后的话,
String s = "PCAP_Personal Computer_Apple\n" +
"NBHP_NoteBook_Hewlett Packard\n" +
"TVSG_Televisor_Samsung" ;
Stream<String> abbreviations = Arrays.stream(s.split("\\n"));
List<String> brandNames = abbreviations
.map(abbr -> abbr.substring(abbr.lastIndexOf('_') + 1))
.collect(Collectors.toList());
brandNames.forEach(System.out::println);
将会打印出:
Apple
Hewlett Packard
Samsung
如果你需要它们作为一个数组,只需使用:
String[] brandNames = abbreviations
.map(abbr -> abbr.substring(abbr.lastIndexOf('_') + 1))
.toArray(String[]::new);
英文:
If the brand names are what is after the last '_'
String s = "PCAP_Personal Computer_Apple\n" +
"NBHP_NoteBook_Hewlett Packard\n" +
"TVSG_Televisor_Samsung" ;
Stream<String> abbreviations = Arrays.stream(s.split("\\n"));
List<String> brandNames = abbreviations
.map(abbr -> abbr.substring(abbr.lastIndexOf('_') + 1))
.collect(Collectors.toList());
brandNames.forEach(System.out::println);
will print you
Apple
Hewlett Packard
Samsung
If you need them to be an array, just use
String[] brandNames = abbreviations
.map(abbr -> abbr.substring(abbr.lastIndexOf('_') + 1))
.toArray(String[]::new);
答案3
得分: 0
尝试一下。它只会删除从最后一个下划线开始的所有内容。
String[] lines = { "PCAP_Personal Computer_Apple",
"NBHP_NoteBook_Hewlett Packard",
"TVSG_Televisor_Samsung",
"And_more_than_two_underscores_IBM" };
List<String> vendors = Arrays.stream(lines)
.map(str -> str.replaceAll(".*(?<=_)", ""))
.collect(Collectors.toList());
vendors.forEach(System.out::println);
输出结果
Apple
Hewlett Packard
Samsung
IBM
英文:
Try this. It just deletes everything up to and including the last underscore.
String[] lines = { "PCAP_Personal Computer_Apple",
"NBHP_NoteBook_Hewlett Packard",
"TVSG_Televisor_Samsung",
"And_more_than_two_underscores_IBM" };
List<String> vendors = Arrays.stream(lines)
.map(str -> str.replaceAll(".*(?>_)", ""))
.collect(Collectors.toList());
vendors.forEach(System.out::println);
Prints
Apple
Hewlett Packard
Samsung
IBM
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论