How do I remove repeated elements from ArrayList?

后端 未结 30 1622
难免孤独
难免孤独 2020-11-21 06:24

I have an ArrayList, and I want to remove repeated strings from it. How can I do this?

30条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 06:50

    ArrayList city=new ArrayList();
    city.add("rajkot");
    city.add("gondal");
    city.add("rajkot");
    city.add("gova");
    city.add("baroda");
    city.add("morbi");
    city.add("gova");
    
    HashSet hashSet = new HashSet();
    hashSet.addAll(city);
    city.clear();
    city.addAll(hashSet);
    Toast.makeText(getActivity(),"" + city.toString(),Toast.LENGTH_SHORT).show();
    

提交回复
热议问题