값이있는 문자열이 있습니다. || HelpDesk || IT 직원 || IT 직원 || 관리 || 감사 || HelpDesk ||구분 기호가있는 문자열에서 중복 값을 제거하는 방법
중복을 제거하고 다음과 같은 데미 리어를 유지하는 고유 값을 반환해야하는 코드를 작성하려고합니다. || HelpDesk || IT 직원 || 관리자 || 감사 ||
내 코드는 중복을 제거하기 위해 HashSet을 사용하지만 문제는 구분 기호를 제거한다는 것입니다. 중복 값만 제거하여 분리 문자를 유지하려면 어떻게해야합니까?
다음은 중복을 제거하고 구분 기호를 다시 추가 한 후의 코드입니다. 그러나 이것을하는 쉬운 방법이 있는지 확실하지 않습니다.
public static void main(String[] args) {
TestDuplicates testDuplicates = new TestDuplicates();
String bRole = "||HelpDesk||IT Staff||IT Staff||Admin||Audit||HelpDesk||";
List<String> listWithoutDuplicates = new ArrayList<String>();
String noDup = "";
List<String> splittedStringList =
new ArrayList<String>();
SplitOperations splitOperations =
new SplitOperations();
splittedStringList =
splitOperations.splitString(bRole);
for (int i = 0; i < splittedStringList.size(); i++) {
HashSet<String> listToSet = new HashSet<String>(splittedStringList);
listWithoutDuplicates = new ArrayList<String>(listToSet);
}
for(int i=0;i<listWithoutDuplicates.size();i++){
noDup = noDup + "||"+listWithoutDuplicates.get(i);
System.out.println(listWithoutDuplicates.get(i));
}
System.out.println("No Duplicate is::"+ noDup+"||");
}
감사
같은
분할'||'로 문자열 (당신이 필요합니다 이 문자들을 이스케이프하거나,'Pattern.quote'를 사용하여). Set의 요소에서 String을 구성 할 때 구분 기호를 다시 가져올 수 있습니다. –
우리가 알기를 바랍니다. 지금까지 어떤 시도를 했습니까? – SMA
HashSet에서 유일한 요소를 추출한 후에 구분 기호를 다시 추가하는 것이 좋습니다. – user314104