1 00:00:00,650 --> 00:00:03,530 There are collection types that implement the map interface. 2 00:00:03,890 --> 00:00:07,400 For example, a hash map directly implements the map interface. 3 00:00:08,660 --> 00:00:13,430 Linked to hash map as a child class of hash map, which implements the map interface. 4 00:00:15,920 --> 00:00:21,320 Tree map implements these sort map interface, which in turn implements the map interface. 5 00:00:21,620 --> 00:00:27,560 So one way or another, all three classes implement the map interface, whether it's directly or indirectly. 6 00:00:30,000 --> 00:00:33,120 In this lesson, you're in a see polymorphism from the lens of map. 7 00:00:36,470 --> 00:00:40,430 Open the folder for this lesson by following this path in your course resources. 8 00:00:45,990 --> 00:00:50,740 In our map is an interface defines methods that a class must implement. 9 00:00:52,720 --> 00:00:58,390 The hash map class implements this interface, it also implement a bunch of other interfaces, but namely 10 00:00:58,390 --> 00:00:58,840 map. 11 00:01:01,980 --> 00:01:04,290 Here are some of the methods that map the phones. 12 00:01:04,440 --> 00:01:10,680 Once again, the familiar sighs method hash map implements the interface, so we know the hash map class 13 00:01:10,680 --> 00:01:11,970 overrides this method. 14 00:01:12,480 --> 00:01:14,880 You can go back here and call size from the hash map. 15 00:01:22,810 --> 00:01:26,620 Here is another one is empty, and it follows that we can call is empty. 16 00:01:34,620 --> 00:01:40,320 Contains key, and so we know hash map already overrides contains key, which we can use to check for 17 00:01:40,320 --> 00:01:41,280 a particular key. 18 00:01:47,260 --> 00:01:53,710 Anyways, the hash map class implements this interface, so it's basically signing a contract that it's 19 00:01:53,710 --> 00:01:56,860 going to override all of the methods inside the interface. 20 00:01:57,100 --> 00:01:57,820 And it does. 21 00:01:58,480 --> 00:02:03,970 For example, we know our hash map is going to be forced to override or remove books that remove. 22 00:02:09,250 --> 00:02:09,940 And yeah. 23 00:02:13,500 --> 00:02:17,340 So a separate hash map from link to hash map and tree map. 24 00:02:18,030 --> 00:02:22,380 The most striking difference is that hash map has no order length. 25 00:02:22,380 --> 00:02:24,960 Hash map and tree map do follow in order. 26 00:02:31,510 --> 00:02:35,440 If you print the hash map, you'll notice that they appear in a random order. 27 00:02:58,160 --> 00:03:02,060 Tree map entries are sorted according to the compare to method. 28 00:03:05,740 --> 00:03:11,290 Based on the book classes compared to Method, we can expect tree map to sort its entries from lowest 29 00:03:11,290 --> 00:03:12,370 the highest price. 30 00:03:14,400 --> 00:03:16,380 So declare this as a true map instead. 31 00:03:36,130 --> 00:03:42,310 And check it out, all of the entries inside the tree map are sorted from lowest to highest price. 32 00:03:46,530 --> 00:03:50,880 Now for a link to hash map, the entries appear according to their insertion order. 33 00:03:54,540 --> 00:03:56,700 If I declare this as a linked hash map. 34 00:04:04,550 --> 00:04:05,420 And run the app. 35 00:04:10,500 --> 00:04:14,190 The entries appear according to the order that you inserted them in. 36 00:04:17,250 --> 00:04:22,170 So tree map and link to hash map, follow and order hash map doesn't have an order. 37 00:04:25,180 --> 00:04:27,100 Here I can convert this to a key set. 38 00:04:29,640 --> 00:04:30,660 And to an array. 39 00:04:31,950 --> 00:04:33,690 And grabbed the object index to. 40 00:04:36,220 --> 00:04:38,380 And I know it's going to be Game of Thrones. 41 00:04:51,750 --> 00:04:53,400 If I make this into a tree map. 42 00:04:59,900 --> 00:05:04,610 I know in next two is going to return Harry Potter, because that's the way it's going to be sorted. 43 00:05:14,320 --> 00:05:15,790 But if we use a hash map. 44 00:05:24,050 --> 00:05:28,760 I have no idea what in next two is going to give me because everything is going to be out of order. 45 00:05:28,790 --> 00:05:29,420 Random. 46 00:05:31,270 --> 00:05:35,260 So when do you use a map, when there is parity between data? 47 00:05:37,930 --> 00:05:40,240 But specifically, when do we use a hash map? 48 00:05:40,600 --> 00:05:44,920 If you don't care about order, use a hash map because it offers the best performance. 49 00:05:47,680 --> 00:05:52,420 Use a tree map if you need the entries to be sorted, according to what you specify. 50 00:05:55,590 --> 00:06:00,810 Use a linked hash map if you need the entries to be sorted according to their insertion order. 51 00:06:03,060 --> 00:06:07,710 Now, it's worth noting that I always use hash mark because it offers the best performance out of the 52 00:06:07,710 --> 00:06:10,770 three, and it's quite rare that indeed the entries to be sorted. 53 00:06:11,010 --> 00:06:13,740 And in this course, we're probably going to keep using hash map. 54 00:06:18,570 --> 00:06:23,760 Now, it's interesting, as you remember, that interfaces allow for polymorphism, why is polymorphism 55 00:06:23,760 --> 00:06:26,330 important, as always, flexibility. 56 00:06:30,560 --> 00:06:33,140 In this case, print map expects a map. 57 00:06:34,070 --> 00:06:37,790 And our hash map can take the form of a map so we can directly pass it in. 58 00:06:39,290 --> 00:06:42,320 We can even go a step further and declare this as type map. 59 00:06:44,830 --> 00:06:47,380 And even here, I can define this as a trauma. 60 00:06:48,070 --> 00:06:53,290 And what I have to change anything because I know Trump can take the form of map once again. 61 00:06:53,290 --> 00:06:57,190 I can define this as a linked hash map and everything should still work. 62 00:06:57,940 --> 00:07:04,210 So once again, we see that polymorphism helps make our code reusable because all three of our collections 63 00:07:04,210 --> 00:07:05,800 share one common form. 64 00:07:10,170 --> 00:07:13,380 Use a map collection when there is parity between data. 65 00:07:15,000 --> 00:07:20,670 We can drill further and say, use a hash map if you don't care about order because it offers the best 66 00:07:20,670 --> 00:07:21,510 performance. 67 00:07:23,810 --> 00:07:28,280 Use a tree map if you need the entries to be sorted, according to what you specify. 68 00:07:28,880 --> 00:07:34,310 Use a linked hash map if you need the entries to be sorted according to their insertion order. 69 00:07:36,790 --> 00:07:41,740 In this course, we're likely going to keep using hash map because it offers the best performance out 70 00:07:41,740 --> 00:07:45,580 of the three, and it's not common that we're going to need the entries to be sorted.