Remove duplicate values from a string in java

前端 未结 14 2616
刺人心
刺人心 2020-12-06 05:26

Can anyone please let me know how to remove duplicate values from

String s=\"Bangalore-Chennai-NewYork-Bangalore-Chennai\"; 

and output sh

14条回答
  •  借酒劲吻你
    2020-12-06 06:01

    I'd prefer this which is simpler than all of the above.

    public void removeDuplicates() {
      String myString = "Bangalore-Chennai-NewYork-Bangalore-Chennai";
    
      String[] array = myString.split("-");
    
      Set hashSet = new HashSet(Arrays.asList(array));
    
      String newString = StringUtils.join(hashSet, "-");        
    }
    

提交回复
热议问题