任何人都可以告诉我如何从
中删除重复值String s="Bangalore-Chennai-NewYork-Bangalore-Chennai";
和输出应该像
String s="Bangalore-Chennai-NewYork-";
使用Java ..
任何帮助都将不胜感激。
答案 0 :(得分:36)
这是一行:
public String deDup(String s) {
return new LinkedHashSet<String>(Arrays.asList(s.split("-"))).toString().replaceAll("(^\\[|\\]$)", "").replace(", ", "-");
}
public static void main(String[] args) {
System.out.println(deDup("Bangalore-Chennai-NewYork-Bangalore-Chennai"));
}
输出:
Bangalore-Chennai-NewYork
请注意,订单已保留:)
要点是:
split("-")
为数组提供了不同的值Arrays.asList()
将数组转换为List LinkedHashSet
保留唯一性和插入顺序 - 它完成了为我们提供唯一值的所有工作,这些值通过构造函数传递toString()
为[element1, element2, ...]
replace
命令会从toString()
此解决方案要求值不包含字符序列", "
- 这是对此类简洁代码的合理要求。
当然是1行:
public String deDup(String s) {
return Arrays.stream(s.split("-")).distinct().collect(Collectors.joining("-"));
}
如果您不关心保留订单(即可以删除第一次出现的副本):
public String deDup(String s) {
return s.replaceAll("(\\b\\w+\\b)-(?=.*\\b\\1\\b)", "");
}
答案 1 :(得分:4)
public static String removeDuplicates(String txt, String splitterRegex)
{
List<String> values = new ArrayList<String>();
String[] splitted = txt.split(splitterRegex);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splitted.length; ++i)
{
if (!values.contains(splitted[i]))
{
values.add(splitted[i]);
sb.append('-');
sb.append(splitted[i]);
}
}
return sb.substring(1);
}
用法:
String s = "Bangalore-Chennai-NewYork-Bangalore-Chennai";
s = removeDuplicates(s, "\\-");
System.out.println(s);
打印:
Bangalore-Chennai-NewYork
答案 2 :(得分:2)
您可以将字符串添加到HashSet。
Sinppet:
Set<String> set = new HashSet<String>();
for(int i=0; i < arr.length; i++){
if(set.contains(arr[i])){
System.out.println("Duplicate string found at index " + i);
} else {
set.add(arr[i]);
}
答案 3 :(得分:1)
只是想法:
Collection
Collection
并删除重复项Collection
构建新字符串最棘手的部分应该是3,但并非不可能。如果您使用Set
,则可以跳过此步骤。
编辑也许您可以在添加元素之前使用状态检查替换2&amp; 3
答案 4 :(得分:1)
通过按-
拆分创建字符串数组,然后从中创建一个hashSet。
String s="Bangalore-Chennai-NewYork-Bangalore-Chennai";
String[] strArr = s.split("-");
Set<String> set = new HashSet<String>(Arrays.asList(strArr));
如果您想将其作为字符串数组返回,请执行以下操作:
String[] result = new String[set.size()];
set.toArray(result);
以下是执行此操作的示例代码:
String s="Bangalore-Chennai-NewYork-Bangalore-Chennai";
String[] strArr = s.split("-");
Set<String> set = new LinkedHashSet<String>(Arrays.asList(strArr));
String[] result = new String[set.size()];
set.toArray(result);
StringBuilder res = new StringBuilder();
for (int i = 0; i < result.length; i++) {
String string = result[i];
if(i==result.length-1)
res.append(string);
else
res.append(string).append("-");
}
System.out.println(res.toString());
输出: -
Bangalore-Chennai-NewYork
答案 5 :(得分:1)
static String RemoveDuplicateCharInString(String s){
for (int i = 0; i < s.length(); i++) {
if((s.substring(i+1)).indexOf(s.charAt(i))!=-1){
s=s.substring(0,i+1)+(s.substring(i+1)).replaceAll(""+s.charAt(i),"");
}
}
return s;
}
答案 6 :(得分:0)
wordsArray = s.split("-");
List<String> wordsList = new Arrays.asList(wordsArray);
Set<String> wordsSet = new LinkedHashSet<String>(wordsList);
String[] noDuplicates = new String[wordsSet.size()];
wordsSet.toArray(noDuplicates);
答案 7 :(得分:0)
我更喜欢这比上述所有内容更简单。
public void removeDuplicates() {
String myString = "Bangalore-Chennai-NewYork-Bangalore-Chennai";
String[] array = myString.split("-");
Set<String> hashSet = new HashSet<String>(Arrays.asList(array));
String newString = StringUtils.join(hashSet, "-");
}
答案 8 :(得分:0)
public static void main(String[] args) {
String str="Bangalore-Chennai-Newyork-Bangalore-Chennai";
String output="";
String [] arr=str.split("-");
LinkedHashSet<String> lhs=new LinkedHashSet<String>();
for (int i = 0; i < arr.length; i++) {
lhs.add(arr[i]);
}
for(String s:lhs){
output=output+s+"-";
}
System.out.println(output);
}
答案 9 :(得分:0)
游戏稍晚,但我只想使用HashMap。这很容易理解,并且快速查找键,可能不是最好的方法,但它仍然是一个很好的答案IMO。当我需要快速和脏的格式时,我一直使用它:
String reason = "Word1 , Word2 , Word3";
HashMap<String,String> temp_hash = new HashMap<String,String>();
StringBuilder reason_fixed = new StringBuilder();
//in:
for(String word : reason.split(",")){
temp_hash.put(word,word);
}
//out:
for(String words_fixed : temp_hash.keySet()){
reason_fixed.append(words_fixed + " , ");
}
//print:
System.out.println(reason_fixed.toString());
答案 10 :(得分:0)
public class RemDuplicateWordFromString {
public static void main(String[] args) {
String s1 = "Hello India Hello India Hello India Hello India";
countWords(s1);
}
public static void countWords(String s1) {
String[] s2 = s1.split(" ");
for (int i = 0; i < s2.length; i++) {
for (int j = i + 1; j < s2.length; j++) {
if (s2[i].equals(s2[j])) {
if (i != j) {
s2[i] = "";
}
}
}
}
for (int i = 0; i < s2.length; i++) {
if (s2[i] != "") {
System.out.print(s2[i] + " ");
}
}
}
}
答案 11 :(得分:0)
p_ply <- style(p_ply, fillcolor = "green")
答案 12 :(得分:-1)
import java.util.HashSet;
public class SplitString {
public static void main(String[] args) {
String st = new String("New Delhi-Chennai-New York-Bangalore-Chennai-New Delhi-Chennai-New York");
StringBuffer stb = new StringBuffer();
HashSet<String> hashset = new HashSet<String>();
for (String a : st.split("-"))
hashset.add(a);
Object[] str = (Object[]) hashset.toArray();
for (int i = 0; i < str.length; i++) {
stb.append(str[i]);
if (i < str.length - 1)
stb.append("-");
}
System.out.println(stb);
}
}
答案 13 :(得分:-2)
import java.util.*;
public class RemoveDuplicateWord {
public static void main(String[] args) {
String str = "Hai hello Hai how hello are how you";
removeDupWord(str);
}
public static void removeDupWord(String input) {
List<String> list = Arrays.asList(input.split(" "));
LinkedHashSet<String> lhs = new LinkedHashSet<String>(list);
for(String s : lhs) {
System.out.print(s+" ");
}
}
}