Java Tutorial For Beginners #14 - Maps Example

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·7y ago

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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This Java tutorial teaches the basics of maps and how to use them to store and retrieve data. It also covers array sorting using Java's Arrays.sort() method. By following this tutorial, you can learn how to create and use maps to solve real-world problems.

Key Takeaways
  1. Create a string to count letters
  2. Convert the string to a character array
  3. Loop through the character array
  4. Check if a key exists in the map
  5. Update the value if the key exists
  6. Create a map to count the occurrences of each letter in a string
  7. Use the MDOT remove method to remove elements from the map
  8. Sort an array using the arrays dot sort method
  9. Type Arrays.sort() with optional start and end indices
  10. Use a for loop to print the sorted array elements
💡 Maps are a powerful data structure in Java that can be used to store and retrieve data efficiently. By using maps, you can solve complex problems and improve the performance of your code.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →