Java Tutorial For Beginners #14 - Maps Example
Skills:
LLM Foundations80%Prompt Craft70%Prompting Basics60%Advanced Prompting50%Prompt Systems Engineering50%
Key Takeaways
This Java tutorial covers the basics of maps, including creating and using hash maps to store key-value pairs, and provides examples of how to use maps to count the occurrences of each letter in a string. The tutorial also covers array sorting using Java's Arrays.sort() method.
Full Transcript
hello and welcome back so in this video I'm actually not gonna be teaching you guys anything new so if you're more interested in just like speeding through and learning everything I suggest you just move to the next video but what I'm gonna be doing is showing you some common problems that you're gonna want to solve and how you can do that with the information I've showed you so far so kind of bringing everything in now mixing it all together and using all the things and this is a really good way it's kind of like apply all the knowledge that we've just learned into solving like a few common computer problems because a lot of the time you guys learn how to do all this stuff but you don't know how to kind of mix it all together and that's what I'm trying to do in this video so I'm also gonna be showing you sell like useful methods and some things that I may have forgotten to talk about in previous videos so yeah that's what this video is gonna be about so in the last video I mentioned that a good exercise to do would be if you have like I don't know a word and you want to count or like a sentence or string you want to count all the letters and store them in like a hash map and have the letter as the key and the value being how many times they appear so I figured for any of you guys that were brave enough to attempt that on yourself I would show you solution here quickly to doing that and for any of us who didn't see that make sure you pay attention because this is really useful it's gonna help you also to understand kind of how maps work and how you can use them so what I'm gonna do first of all is I'm just gonna create a string let's call STR is equal to and I'll say hello my name is Tim and I hand cool all right very creative string name anyways so we're gonna do that just because we're gonna count all the letters in this string now the human way to count this would be to go through and literally read like each letter and be like okay so I have an L how many times it'll appear but imma show you how we do this with maps and for loops like very efficiently in computing so what I'm gonna do is I'm create a for loop what we're gonna do is we're gonna loop through the string and the way that we can do this is because if I try to do like for char or like X cuz it's gonna be a character right and in STR you see we get this red line and says we can't iterate over a string so the way that we can do this is actually a cool method and a useful method and it is gonna convert this string into a character array for us the way that we can do that is by doing I guess a dot dot - char array and this is if I literally just print this out for you if you want to see what it looks like system.out.println is simply just gonna put every character so including the spaces into a character array for us so let's just run this and you can see it just is literally just printing this entire thing but it's actually in an array and this is gonna allow us to loop through it so you'll see in a second okay so what I'm gonna do now is every character I'm gonna see if that key already exists in the map if it does I'm gonna get the value because that values gonna be how many times it exists so in this case say I'm looking for like m it already exists in the mouth I'm just gonna add one to it and then overwrite that key with a new value and you'll see what I mean here in a second so I'm gonna say if m dot contains key of X then what I'm gonna do is I'm gonna say olds will say int old equals M get and then the key is gonna be X and then what we're gonna do is what's the issue here M add cast and if I need this for getting all my brackets into old equals M gets X and X should be hmm interesting one second saying I have to cast this to an integer okay let's just do this I don't think we actually need to do this but let's just cast it to an int all right anyways and then we'll say M don't put and for the key is gonna be X cuz that's gonna be the character and we're gonna say old plus 1 now what this should do for us is just increment the the amount so that's going to go up by 1 now otherwise so if that key does not exist what we're gonna do is we're gonna put into our map a new key and the keys value is gonna be 1 because we just found the first occurrence of that letter and the key is gonna be X standing for the letter and that should work now after we do that I want to see the counts of all these letters so to do that let's probably help my spell system correctly we're gonna print the wine and we're just gonna print em okay so quickly I know I did this fast and you might not really understand exactly what's going on but we're just looping through every letter in this string we're gonna check the first check is gonna see if the map contains that letter already so it has a key if it does not what we're gonna do is we're simply gonna add in a key and it's going to be equal to that letter and it's gonna have a value of 1 because we just found it it's occurs one time we already know that now if for some reason or if that key does exist what we're gonna do is we're gonna get that previous count and then we're gonna add 1 to that previous count and override the key so like if the count is 4 it goes up to 5 pretty straightforward so if I run this you can see we get space well there's 8 spaces we get a there's 3 AC there's 1 D there's one and it goes through and it counts all of the different letters for us and if we wanted to confirm those correct we could go through and count them like that and just by looking at here it does look as though that is correct so yeah that's a really common way to use a map yeah it's really useful and there's a lot of cases in which you want to do something like that ok so the next thing I want to show you guys is something I forgot to do in the last video and this is really just gonna take like 10 seconds but pretty much to remove an element from the map you probably already guessed but it's literally just MDOT remove and then you just type the key so I just felt like I had to shape say this cuz I watched back my other video and I realized I forgot to say it so to remove an element you do MDOT remove and just put the key so if I want to remove like all the spaces which actually wouldn't be a bad idea to remove from our counts then if I do that hmmm interesting why it's not letting me remove that let's try that maybe oh it's because it's off because the character my bad start guys I've been doing string a character with space is different yes then it actually removes the that character the space from our program or from our map whatever okay now the next thing I want to show actually is sorting so sorting is something you typically want to do on arrays so I'm actually going to remove all this and I'm going to create a new array an integer array let's say int X equal this case is do you like three and let's do a bunch of different values and see if we can get this to sort for us let's do negative 99 semicolon there for us and to sort this is actually really easy all we have to do is just type arrays dot sort and then what takes it takes two arguments now the first argument is obviously going to be our array so this case X and then the other two arguments are actually optional and what these do search those three are even saw to what these arguments do is from a certain index so if we want to sort just lets say like this part of the list that's highlighted we can actually do that and the way that we would do that is we would type one because we're gonna start at one and if we were going to seven one two three four five six seven funny enough we could actually type seven now I know this is index six but the thing is it's gonna sort up but not including to that index so if we type seven it's gonna that's seven but it's only gonna sort up to like the actual number seven okay and what this does and I believe is actually just changes the list we don't need to say like x equals arrays dot sort we just say arrays dot sort and then what we can do is we knew system.out.println and we can print X and let's see if it did end up sorting that section of the list for us okay so we actually man of course we can't do that we're gonna have to set up a basic for loop to print these out just because when we try to print arrays I figure out we get that little loop or we get that message because that's the memory address but anyways let's say for int I in X and then we'll simply just print out I it's got to make it complicated for us doesn't it and instead of printing a line we're just gonna print and we'll just add a comma here just to make it separated okay so let's run that so there we go so it actually did sort this middle section of the list for us right so we got one two three four five six seven and you see eight and zero we're not touched and negative 99 was not touched now if you want to sort the entire thing and you don't want to worry about these indexes you can just put X in this case if we do this we're gonna get an 8 and 99 zero all the way up to 8 and yeah so you could sort like from 3 to 5 or 3 to 6 or whatever okay and you can see it's only gonna sort like a few of the elements in the list for us or in the array for us so that's a really useful way to sort things at quite quickly actually in quite easily using just a raised dot sort and remember you don't have to do like x equals arrays dot sort because what it's actually gonna do is just sort in-place all the elements of X and swap them around rather than creating like a new version of X that is sorted so anyways I know this has been like a quick video but I just wanted to show you and for some of the guys that might have been struggling how we can kind of incorporate some of these things together and how to create like a basic problem-solving program maybe a way you go about doing that hopefully this was useful to you guys let me know in the comments down below and in the next video we're gonna be getting into object-oriented programming and that's gonna be the rest of the tutorial series it's a bit more complex but it's definitely like the heart of Java and it's something that we have to understand so if you guys enjoyed the video please make sure you leave a like and I will see you in the next one [Music]
Original Description
In this java tutorial I will be talking about maps. Maps in java consist of a key value pair. You can index and access a key to change or get a certain value. I will also be giving some very good examples of how to use maps properly
Java beginners programming tutorial. A complete java tutorial meant for absolute beginners. Absolutely no programming experience required. If you are new to programming and want to get started you are in the right place!
**************************************************************
One-Time Donations: https://goo.gl/pbCE9J
Support the Channel: https://www.patreon.com/techwithtim
Join my discord server: https://discord.gg/pr2k55t
**************************************************************
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Tech With Tim Java
- Java tutorials
- Java for beginners
- Java Tutorial for Beginners
- Beginner Java Tutorial
- Java maps
- Java collections
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI