1. 程式人生 > >set特性,去除list重複資料。

set特性,去除list重複資料。

List<String> listStr  = new ArrayList<>();
		listStr.add("伊利");
		listStr.add("蒙牛");
		listStr.add("優酸乳");
		listStr.add("優酸乳");
		Set<String> set = new LinkedHashSet<>();
		//list裡裝的有重複的資料,用set來去掉重複
		//set中的資料不可以重複,會發生覆蓋
		set.addAll(listStr);
		listStr.clear();
		//將原來的list的資料全部清空
		listStr.addAll(set);
		System.out.println(listStr);
		System.out.println(set);