, , , , ,



https://leanpub.com/learndartthehardway In Dart, a Set is an unordered collection of unique items. There are small difference in syntaxes between List and Set. Let us see an example first to know more about the difference. //code 2.18 main(List arguments) { var fruitCollection = {'Mango', 'Apple', 'Jack fruit'}; print(fruitCollection.lookup('Apple')); } //output of code 2.18 Apple We can search the Set using the lookup() method. If we search something else, it returns ‘null’. //code 2.19 main(List arguments) { var fruitCollection = {'Mango', 'Apple', 'Jack fruit'}; print(fruitCollection.lookup('Something Else')); } //output of code 2.19 null Remember one key point regarding Set and Map. When we write: var myInteger = {}; It does not create a Set, but a Map. The syntax for map literals is similar to that of for set literals. Why does it happen? Because map literals came first, the literal {} is a default to the Map type. We can prove this by a simple test: //code 2.20 main(List arguments) { var myInteger = {}; if(myInteger.isEmpty){ print("It is a map that has no key, value pair."); } else print("It is a set that has no key, value pair."); } Watch the output: //output of code 2.20 It is a map that has no key, value pair. It means the map is empty. If it was a set, we would have got the output in that direction. We will see lots of examples of Sets in future, while we build our mobile application. At present just remember, in general, a map is an object that associates keys and values. The set has also keys, but that are implicit. In cases of Sets, we call it indexes. Let us see one example of Map type by map literals. While writing keys and values, it is important to note that each key occurs only once, but you can use the same value many times. //code 2.21 main(List arguments) { var myProducts = { 'first' : 'TV', 'second' : 'Refrigerator', 'third' : 'Mobile', 'fourth' : 'Tablet', 'fifth' : 'Computer' }; print(myProducts['third']); } The output is obvious : ‘Mobile’. Dart understands that the ‘myProducts’ has the type Map(Map); we could have made the key integers or number type, instead of a string type. //code 2.22 main(List arguments) { var myProducts = { 1 : 'TV', 2 : 'Refrigerator', 3 : 'Mobile', 4 : 'Tablet', 5 : 'Computer' }; print(myProducts[3]); } The output is the same as before – mobile. Can we add a Set type collection of value inside a Map? Yes, we can. Consider this code: //code 2.23 main(List arguments) { Set mySet = {1, 2, 3}; var myProducts = { 1 : 'TV', 2 : 'Refrigerator', 3 : mySet.lookup(2), 4 : 'Tablet', 5 : 'Computer' }; print(myProducts[3]); } In the above code (2.23) we have injected a collection of Set type and we also have looked up for the defining value through the Map key. Here, inside the Map key, value pair we have added the set element number 2, this way: 3 : mySet.lookup(2), and later we have told our Android Studio editor to display the value of the Map type ‘myProducts’ . The output is quite expected: 2. You can create the same products lists by Map constructor. For the beginners, the term “constructor” might seem difficult. We will discuss this term in detail in our object-oriented programming category. Consider this code: //code 2.24 main(List arguments) { var myProducts = Map(); myProducts['first'] ='TV'; myProducts['second'] ='Mobile'; myProducts['third'] ='Refrigerator'; if(myProducts.containsValue('Mobile')){ print("Our products’ list has ${myProducts['second']}"); } } And here is the output: //output of code 2.24 Our products’ list has Mobile Since we have had an instance (in code 2.24) of Map class, the seasoned programmer might have expected ‘new Map()’ instead of only ‘Map()’. As of Dart 2, the new keyword is optional. We will learn about these, in detail, in the coming object-oriented programming chapter. We will also have a separate “Collections” chapter later, where we will learn more about List, Set and Map.