//1.集合是一组不相同的实例的无序组合正好和数组相反
//2.跟字典一样,集合内的值是无序的
//3.字典的键必须唯一,集合也不允许有重复值
//4.为了确保元素单一,就要符合Hashable协议,类似就跟字典的键一样(一键多值但不可以一值多键)?
//5.集合存储单个元素
//6.创建Set集合实例
vargroceryBag =Set(["Apples","Oranges","Pineapple"])
//groceryBag.insert("Apples")
//groceryBag.insert("Oranges")
//groceryBag.insert("Pineapple")
//7.循环Set集合里面的元素
forfoodingroceryBag{
print(food)
}
//8.集合提供了另一种更方便的语法创建实例这种方法把实例为Set类型的生命和Array的字面量语法相结合起来
//var groceryBag: Set = ["Apples", "Oranges", "Pineapple"]
//9.集合里是否包含某个元素
lethasBanans =groceryBag.contains("Bananas")
//10.集合的合并去除重复的,留下剩下的元素
letfriendsGroceryBag =Set(["Bananas","Cereal","Milk","Oranges","Apples"])
letcommonGroceryBag =groceryBag.union(friendsGroceryBag)
print(commonGroceryBag)
//union(——:)是Set类型的一个方法,接受一个SequenceType参数,并返回一个Set实例。包含了两个集合去除重复后的元素
//11.交集留下两个集合里都有的元素
letroommatesGroceryBag =Set(["Apples","Bananas","Cereal","Toothpaste"])
letitemsToReturn =commonGroceryBag.intersection(roommatesGroceryBag)
//集合提供了intersection(_:)方法来找出同时存在于两个容器中的元素,并用新的Set实例返回这些元素
//12.如果你想知道两个元素是否包含共同元素
letyourSecondBag =Set(["Berries","Yogurt"])
letroommatesSecondBag =Set(["Grapes","Honey"])
letdisjoint =yourSecondBag.isDisjoint(with:roommatesSecondBag)
//yourSecondBag里面的元素没有出现在roommatesSecondBag返回true否则返回false