1 00:00:00,390 --> 00:00:02,820 The hash map is another useful collection type. 2 00:00:05,660 --> 00:00:11,990 Array List and Hashmat are collection types, the main differences, every entry in an array list is 3 00:00:11,990 --> 00:00:12,770 one value. 4 00:00:15,210 --> 00:00:17,910 Every entry and a Hashmat is a pair of data. 5 00:00:22,260 --> 00:00:24,540 In this lesson, you will learn to use Hashmat. 6 00:00:29,070 --> 00:00:33,960 Inside your Java bootcamp, resources open up the folder for this lesson by following this path. 7 00:00:40,150 --> 00:00:42,820 A hash map stores key value pairs. 8 00:00:45,530 --> 00:00:51,500 The hash map is useful when there's parity between data, every entry and a hash map is a key value 9 00:00:51,500 --> 00:00:51,950 pair. 10 00:00:57,080 --> 00:01:02,570 Every key is like a variable that points to a value, for example, short is 499. 11 00:01:04,360 --> 00:01:09,060 Pens are twelve ninety nine and shoes are twenty three ninety nine. 12 00:01:12,630 --> 00:01:18,330 To create a hash map, you need to write the class type hash map and inside a generic the find that 13 00:01:18,330 --> 00:01:21,110 type of each key, the type of each value. 14 00:01:21,540 --> 00:01:24,690 And remember that generics do not accept primitive types. 15 00:01:27,290 --> 00:01:31,070 And the third step is to make space for a new object of the heightmap class. 16 00:01:34,090 --> 00:01:39,160 All right, inside key values Java, there are two arrays, one is an array of vegetables, the other 17 00:01:39,160 --> 00:01:40,450 is an array of prices. 18 00:01:42,470 --> 00:01:45,770 There is parity between each element in both the race. 19 00:01:48,410 --> 00:01:52,550 So instead of having to erase create a new object of the Hashmat class. 20 00:01:56,350 --> 00:02:00,640 Every key in the hash map is a string, and each value is going to be a double. 21 00:02:02,690 --> 00:02:04,670 And I'm going to call the heightmap items. 22 00:02:21,080 --> 00:02:26,720 Hash maps have a method called put, and you can use it to put an entry into the hash map. 23 00:02:30,210 --> 00:02:33,360 Inside main, we're going to put three entries into the hash map. 24 00:02:38,800 --> 00:02:42,910 The first entry we're going to put is cauliflower with a price of four point ninety nine. 25 00:03:11,560 --> 00:03:12,930 This looks a lot better to me. 26 00:03:12,970 --> 00:03:15,100 I'll play some breakpoints and run the debugger. 27 00:03:32,130 --> 00:03:35,610 And as you can see, every entry in the hash map is a key value pair. 28 00:03:46,320 --> 00:03:48,200 Hazmats also have a method. 29 00:03:49,490 --> 00:03:53,150 Get allows you to access a value from the hash map using its key. 30 00:03:57,480 --> 00:04:00,480 So inside mean I can get the price of cauliflower. 31 00:04:09,690 --> 00:04:14,100 I can get the price of spaghetti squash and I can get the price of parsley. 32 00:04:21,160 --> 00:04:22,360 I'll print each price. 33 00:04:37,870 --> 00:04:38,860 And run the code. 34 00:04:45,140 --> 00:04:48,230 And as you can see, get returns, the price of each key. 35 00:04:49,900 --> 00:04:55,960 Hash map entries don't have an order when you add a new entry, it's placement is random. 36 00:04:59,230 --> 00:05:04,900 That's why house maps don't have an index number, you can't index a value in a Hashmat. 37 00:05:06,140 --> 00:05:08,210 You can only get a value using its key. 38 00:05:10,030 --> 00:05:15,070 So inside key values that Java, I can run through every entry in the hash map using for each. 39 00:05:16,560 --> 00:05:22,090 For each expecta lambda expression, the lambda for this, for each receives every key value pair. 40 00:05:22,140 --> 00:05:25,980 And once again, an arrow points to a block of code where we can just print. 41 00:05:31,640 --> 00:05:32,990 Kee Colon. 42 00:05:35,140 --> 00:05:36,250 Followed by the value. 43 00:05:43,840 --> 00:05:50,080 You'll notice they appear in a random order, we added spaghetti squash second, but it appears last. 44 00:05:54,370 --> 00:05:56,710 Put can add or update an entry. 45 00:06:02,020 --> 00:06:04,950 It adds an entry if the key doesn't already exist. 46 00:06:07,000 --> 00:06:10,150 It updates an entry if the key already exists. 47 00:06:13,020 --> 00:06:17,040 Inside Kivel Java, if you add another entry where the key is partially. 48 00:06:19,150 --> 00:06:21,400 It's just going to update the price for Pursley. 49 00:06:40,160 --> 00:06:40,830 There you go. 50 00:06:40,850 --> 00:06:42,110 Now it's three ninety nine. 51 00:06:49,980 --> 00:06:52,800 Let's recap in this lesson, you learn to create a hash map. 52 00:06:56,120 --> 00:07:02,120 And a hash map is useful when there's parity between data, every entry and a hash map is a key value 53 00:07:02,120 --> 00:07:02,570 pair. 54 00:07:05,630 --> 00:07:09,920 You can use the put method to add a new entry or update an existing one. 55 00:07:13,000 --> 00:07:19,210 Finally, hash maps don't have an order, so you can't index a value from a hash map, but you can use 56 00:07:19,210 --> 00:07:21,940 the get method to get a value based on its key.