• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 09:19
CET 14:19
KST 22:19
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Team Liquid Map Contest #22 - Presented by Monster Energy4ByuL: The Forgotten Master of ZvT30Behind the Blue - Team Liquid History Book19Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13
Community News
Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool22Weekly Cups (March 9-15): herO, Clem, ByuN win32026 KungFu Cup Announcement6BGE Stara Zagora 2026 cancelled12Blizzard Classic Cup - Tastosis announced as captains18
StarCraft 2
General
Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Serral: 24’ EWC form was hurt by military service Weekly Cups (March 9-15): herO, Clem, ByuN win Team Liquid Map Contest #22 - Presented by Monster Energy Weekly Cups (August 25-31): Clem's Last Straw?
Tourneys
KSL Week 87 [GSL CK] #2: Team Classic vs. Team Solar 2026 KungFu Cup Announcement [GSL CK] #1: Team Maru vs. Team herO RSL Season 4 announced for March-April
Strategy
Custom Maps
Publishing has been re-enabled! [Feb 24th 2026] Map Editor closed ?
External Content
The PondCast: SC2 News & Results Mutation # 517 Distant Threat Mutation # 516 Specter of Death Mutation # 515 Together Forever
Brood War
General
ASL21 General Discussion BGH Auto Balance -> http://bghmmr.eu/ JaeDong's form before ASL Gypsy to Korea BSL Season 22
Tourneys
Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL22] Open Qualifiers & Ladder Tours IPSL Spring 2026 is here!
Strategy
Simple Questions, Simple Answers Soma's 9 hatch build from ASL Game 2 Fighting Spirit mining rates Zealot bombing is no longer popular?
Other Games
General Games
Nintendo Switch Thread Path of Exile General RTS Discussion Thread Stormgate/Frost Giant Megathread Dawn of War IV
Dota 2
Official 'what is Dota anymore' discussion The Story of Wings Gaming
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Five o'clock TL Mafia Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Mexico's Drug War Canadian Politics Mega-thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Req][Books] Good Fantasy/SciFi books [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion Tokyo Olympics 2021 Thread General nutrition recommendations Cricket [SPORT]
World Cup 2022
Tech Support
Laptop capable of using Photoshop Lightroom?
TL Community
The Automated Ban List
Blogs
Funny Nicknames
LUCKY_NOOB
Money Laundering In Video Ga…
TrAiDoS
Iranian anarchists: organize…
XenOsky
FS++
Kraekkling
Shocked by a laser…
Spydermine0240
Unintentional protectionism…
Uldridge
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 9379 users

The Big Programming Thread - Page 782

Forum Index > General Forum
Post a Reply
Prev 1 780 781 782 783 784 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
October 18 2016 20:30 GMT
#15621
I need a hand with this little java lab I need to do

I missed the lecture that goes with this, so I am pretty in the dark

I've been provided with a class (called database)

It has a member, public Map<String, List<String>>

The class has a constructor to make an empty map

It has an add method, which adds a student and a course, or just a course if the student is already in the map

it has a remove method, which removes the provided course from the student

and a boolean method which checks if anyone is taking a given course



And so, I don't really know where to begin. I really just need some help getting started. For example, the constructor. I have no idea how to create an empty map. Just "Map name = new Map" ? But it seems like there are many types of maps.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2016-10-18 20:49:11
October 18 2016 20:45 GMT
#15622
On October 19 2016 05:30 travis wrote:
I need a hand with this little java lab I need to do

I missed the lecture that goes with this, so I am pretty in the dark

I've been provided with a class (called database)

It has a member, public Map<String, List<String>>

The class has a constructor to make an empty map

It has an add method, which adds a student and a course, or just a course if the student is already in the map

it has a remove method, which removes the provided course from the student

and a boolean method which checks if anyone is taking a given course



And so, I don't really know where to begin. I really just need some help getting started. For example, the constructor. I have no idea how to create an empty map. Just "Map name = new Map" ? But it seems like there are many types of maps.


Are you required to use List<String> (assuming this is the list which holds courses for student)? If not, then I suggest OOP approach. Create a class which represents course or an enum at the very least.

It's been a while since I wrote code in Java, but I believe this should do it:

HashMap<String, List<String>> students = new HashMap<String, ArrayList<String>>();

Add and remove are trivial. We shouldn't really do your homework.

Your last method is to check if anyone takes a given course. If you really have to use List<String> for courses, then the simple approach might be to iterate your map and check values (type is List<String>) if it contains the course that you want to search for. Accumulate results and done.

Remember that map is based on key : value.
Your key's type is String. Your value's type is List<String>. List<String> is just an interface though. Your concrete type is ArrayList<String> in this example. As long as you follow interface of List and don't do any casts for it, you could later replace it with something else without much code change.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
October 18 2016 20:47 GMT
#15623
On October 19 2016 05:45 Shield wrote:
Show nested quote +
On October 19 2016 05:30 travis wrote:
I need a hand with this little java lab I need to do

I missed the lecture that goes with this, so I am pretty in the dark

I've been provided with a class (called database)

It has a member, public Map<String, List<String>>

The class has a constructor to make an empty map

It has an add method, which adds a student and a course, or just a course if the student is already in the map

it has a remove method, which removes the provided course from the student

and a boolean method which checks if anyone is taking a given course



And so, I don't really know where to begin. I really just need some help getting started. For example, the constructor. I have no idea how to create an empty map. Just "Map name = new Map" ? But it seems like there are many types of maps.


Are you required to use List<String> (assuming this is the list which holds courses for student)? If not, then I suggest OOP approach. Create a class which represents course or an enum at the very least.

It's been a while since I wrote code in Java, but I believe this should do it:

HashMap<String, List<String>> students = new HashMap<String, List<String>>();

Add and remove are trivial. We shouldn't really do your homework.

Your last method is to check if anyone takes a given course. If you really have to use List<String> for courses, then the simple approach might be to iterate your map and check its value (type is List<String>) if it contains the course that you want to search for. Accumulate results and done.



yeah yeah I don't want you guys to do my homework, just get me started

The handout is so weird. It says "you will see a public instance variable, defined as follows(don't change it):

public Map<String, ArrayList<String>> courses;


but then in the actual lab, when I checked it out, the code says

public Map<String, List<String>> courses;


I'll try checking with my TA
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2016-10-18 20:52:54
October 18 2016 20:49 GMT
#15624
List is a superclass of ArrayList so that doesn't matter.

For starting questions:

Do you actually understand what a map is?
Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.

If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it.
I'll always be your shadow and veil your eyes from states of ain soph aur.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2016-10-18 20:56:40
October 18 2016 20:50 GMT
#15625
On October 19 2016 05:47 travis wrote:
Show nested quote +
On October 19 2016 05:45 Shield wrote:
On October 19 2016 05:30 travis wrote:
I need a hand with this little java lab I need to do

I missed the lecture that goes with this, so I am pretty in the dark

I've been provided with a class (called database)

It has a member, public Map<String, List<String>>

The class has a constructor to make an empty map

It has an add method, which adds a student and a course, or just a course if the student is already in the map

it has a remove method, which removes the provided course from the student

and a boolean method which checks if anyone is taking a given course



And so, I don't really know where to begin. I really just need some help getting started. For example, the constructor. I have no idea how to create an empty map. Just "Map name = new Map" ? But it seems like there are many types of maps.


Are you required to use List<String> (assuming this is the list which holds courses for student)? If not, then I suggest OOP approach. Create a class which represents course or an enum at the very least.

It's been a while since I wrote code in Java, but I believe this should do it:

HashMap<String, List<String>> students = new HashMap<String, List<String>>();

Add and remove are trivial. We shouldn't really do your homework.

Your last method is to check if anyone takes a given course. If you really have to use List<String> for courses, then the simple approach might be to iterate your map and check its value (type is List<String>) if it contains the course that you want to search for. Accumulate results and done.



yeah yeah I don't want you guys to do my homework, just get me started

The handout is so weird. It says "you will see a public instance variable, defined as follows(don't change it):

public Map<String, ArrayList<String>> courses;


but then in the actual lab, when I checked it out, the code says

public Map<String, List<String>> courses;


I'll try checking with my TA


Check my edited comment. List is an interface. ArrayList is concrete implementation of List. See https://docs.oracle.com/javase/7/docs/api/java/util/List.html

Concrete classes are listed after this line: "All Known Implementing Classes:".

Edit:

It doesn't matter if you use one of the following:

HashMap<String, ArrayList<String>> courses = new HashMap<String, ArrayList<String>>();
Map<String, ArrayList<String>> courses = new HashMap<String, ArrayList<String>>();
Map<String, List<String>> courses = new HashMap<String, ArrayList<String>>();

Hopefully, they should all compile. I say hopefully because I haven't developed Java applications for more than a year. The only difference is the last one gives you the flexibility to change concrete implementations later (the right side where you initialise map) without much changes to code.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
October 18 2016 20:59 GMT
#15626
On October 19 2016 05:49 Blitzkrieg0 wrote:
List is a superclass of ArrayList so that doesn't matter.

For starting questions:

Do you actually understand what a map is?
Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.

If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it.


My current understanding is that a map is a data structure that is actually a combination of two data structures.

The first part is a set, with "key" values. Each key is a unique value.

The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys.

And then I imagine the map has some sort of method that looks up the objects or values based on the keys.

Is this kind of right?
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2016-10-18 21:04:21
October 18 2016 21:02 GMT
#15627
On October 19 2016 05:59 travis wrote:
Show nested quote +
On October 19 2016 05:49 Blitzkrieg0 wrote:
List is a superclass of ArrayList so that doesn't matter.

For starting questions:

Do you actually understand what a map is?
Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.

If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it.


My current understanding is that a map is a data structure that is actually a combination of two data structures.

The first part is a set, with "key" values. Each key is a unique value.

The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys.

And then I imagine the map has some sort of method that looks up the objects or values based on the keys.

Is this kind of right?


Map<Key, Value>

As you've said, the first parameter is key. The second part happens to be a collection (also known as container). Technically, it's called value if you want to be abstract. Collection is just a data structure which holds your data. (Hash)Set is a container. ArrayList is a container. (Hash)Map is a container. (Linked)List is a container and so on.

Edit: No specific order seems to be guaranteed. Check http://stackoverflow.com/a/17708526
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
October 18 2016 21:04 GMT
#15628
On October 19 2016 05:59 travis wrote:
Show nested quote +
On October 19 2016 05:49 Blitzkrieg0 wrote:
List is a superclass of ArrayList so that doesn't matter.

For starting questions:

Do you actually understand what a map is?
Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.

If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it.


My current understanding is that a map is a data structure that is actually a combination of two data structures.

The first part is a set, with "key" values. Each key is a unique value.

The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys.

And then I imagine the map has some sort of method that looks up the objects or values based on the keys.

Is this kind of right?


Yes, but you also want to think about why you'd use such a data structure. What's the purpose of mapping key and value pairs?
I'll always be your shadow and veil your eyes from states of ain soph aur.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
October 18 2016 21:23 GMT
#15629
On October 19 2016 06:04 Blitzkrieg0 wrote:
Show nested quote +
On October 19 2016 05:59 travis wrote:
On October 19 2016 05:49 Blitzkrieg0 wrote:
List is a superclass of ArrayList so that doesn't matter.

For starting questions:

Do you actually understand what a map is?
Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.

If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it.


My current understanding is that a map is a data structure that is actually a combination of two data structures.

The first part is a set, with "key" values. Each key is a unique value.

The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys.

And then I imagine the map has some sort of method that looks up the objects or values based on the keys.

Is this kind of right?


Yes, but you also want to think about why you'd use such a data structure. What's the purpose of mapping key and value pairs?


Okay, thinking about it..

Sets are fast but un-ordered. So we can look up our key quickly. If we get a reference from this, we are able to look in a much smaller collection (one that corresponds to the key), than if all the keys and values were stored in a giant collection together. Is this the idea?
RoomOfMush
Profile Joined March 2015
1296 Posts
Last Edited: 2016-10-18 21:29:08
October 18 2016 21:26 GMT
#15630
On October 19 2016 05:59 travis wrote:
Show nested quote +
On October 19 2016 05:49 Blitzkrieg0 wrote:
List is a superclass of ArrayList so that doesn't matter.

For starting questions:

Do you actually understand what a map is?
Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.

If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it.


My current understanding is that a map is a data structure that is actually a combination of two data structures.

The first part is a set, with "key" values. Each key is a unique value.

The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys.

And then I imagine the map has some sort of method that looks up the objects or values based on the keys.

Is this kind of right?

Its not quite right. You name several things that exist, but I dont think you understand their roles and relationships yet.

A map is simply a data structure which "connects" two objects. A key is mapped ("connected") to a value. If you have the key you can look up the value. It does not go the other way. Each key can only be mapped to one value but any number of keys can map to the same value.

Imagine a map which connects letters to numbers:
A -> 5
B -> 12
C -> 7
D -> 5
E -> 42

If we have a letter (the key) we can find out the mapped number (the value). We can ask our map now: "What is the number for the letter 'C'?" and our map will give us the answer 7. This is what the "get"-method of the Map interface does.

The things you said about sets and collections and whatnot is not important. That is implementation specific. You should not worry how the map actually works, at least not on your level. Just know that there are different map implementations, the most commonly used one is the HashMap.

In your example you want to map Strings to Lists of Strings. So every one String can have a List of other Strings mapped to it. For example:
"abc" -> {"a", "b", "c"}
"acd" -> {}
"abd" -> null
"bad" -> {"b", "c"}

How exactly this can be used to model Students and courses is your task to find out now.

Edit:
On October 19 2016 06:23 travis wrote:
Show nested quote +
On October 19 2016 06:04 Blitzkrieg0 wrote:
On October 19 2016 05:59 travis wrote:
On October 19 2016 05:49 Blitzkrieg0 wrote:
List is a superclass of ArrayList so that doesn't matter.

For starting questions:

Do you actually understand what a map is?
Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.

If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it.


My current understanding is that a map is a data structure that is actually a combination of two data structures.

The first part is a set, with "key" values. Each key is a unique value.

The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys.

And then I imagine the map has some sort of method that looks up the objects or values based on the keys.

Is this kind of right?


Yes, but you also want to think about why you'd use such a data structure. What's the purpose of mapping key and value pairs?


Okay, thinking about it..

Sets are fast but un-ordered. So we can look up our key quickly. If we get a reference from this, we are able to look in a much smaller collection (one that corresponds to the key), than if all the keys and values were stored in a giant collection together. Is this the idea?

You should not continue with this because it is wrong. A map has very little to do with a set.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2016-10-18 21:41:29
October 18 2016 21:40 GMT
#15631
Okay, so a map is less specific than I was thinking. I understand your explanation.

But then, as for what the purpose of a map is.

I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.


I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right?
Acrofales
Profile Joined August 2010
Spain18239 Posts
October 18 2016 21:56 GMT
#15632
On October 19 2016 06:40 travis wrote:
Okay, so a map is less specific than I was thinking. I understand your explanation.

But then, as for what the purpose of a map is.

I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.


I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right?

You could make a map implementation using two arrays, if that's what you're saying. It just wouldn't be very efficient. For instance, in your example, you'd have an array with student names (ids) and another array of the same length with lists of classes. To then add a class to a student "John", you search in the first array and find the index of student "John" and then retrieve the list in the second array at that index, and add the class to it.

But that wasn't even what people were getting at. The point is to abstract away from efficiency: the way you understand Map should be at the same level where you understand List, or Set. These are data structures with a specific behavior you expect from them, and you shouldn't care (at this level) whether a list is implemented as a linked list or as an array (for instance).
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2016-10-18 22:28:40
October 18 2016 22:03 GMT
#15633
On October 19 2016 06:56 Acrofales wrote:
Show nested quote +
On October 19 2016 06:40 travis wrote:
Okay, so a map is less specific than I was thinking. I understand your explanation.

But then, as for what the purpose of a map is.

I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.


I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right?

You could make a map implementation using two arrays, if that's what you're saying. It just wouldn't be very efficient. For instance, in your example, you'd have an array with student names (ids) and another array of the same length with lists of classes. To then add a class to a student "John", you search in the first array and find the index of student "John" and then retrieve the list in the second array at that index, and add the class to it.

But that wasn't even what people were getting at. The point is to abstract away from efficiency: the way you understand Map should be at the same level where you understand List, or Set. These are data structures with a specific behavior you expect from them, and you shouldn't care (at this level) whether a list is implemented as a linked list or as an array (for instance).


Well, couldn't you just make an implementation with one array, checking some "key" value within the object at each index of array until you find the right "key" ? Therefore just using a list to do the same thing that the map does?

(my point is that I think I understand the function of it, but that doesn't explain *why* it's used)
RoomOfMush
Profile Joined March 2015
1296 Posts
October 18 2016 22:30 GMT
#15634
On October 19 2016 07:03 travis wrote:
Show nested quote +
On October 19 2016 06:56 Acrofales wrote:
On October 19 2016 06:40 travis wrote:
Okay, so a map is less specific than I was thinking. I understand your explanation.

But then, as for what the purpose of a map is.

I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.


I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right?

You could make a map implementation using two arrays, if that's what you're saying. It just wouldn't be very efficient. For instance, in your example, you'd have an array with student names (ids) and another array of the same length with lists of classes. To then add a class to a student "John", you search in the first array and find the index of student "John" and then retrieve the list in the second array at that index, and add the class to it.

But that wasn't even what people were getting at. The point is to abstract away from efficiency: the way you understand Map should be at the same level where you understand List, or Set. These are data structures with a specific behavior you expect from them, and you shouldn't care (at this level) whether a list is implemented as a linked list or as an array (for instance).


Well, couldn't you just make an implementation with one array, checking some "key" value within the object at each index of array until you find the right "key" ? Therefore just using a list to do the same thing that the map does?

How do you know a map doesnt do that?
Thats the point. You dont need to know. A map is not an implementation. It has nothing to do with arrays, lists, sets or whatnot. Its just a definition for methods and how these methods work. What happens in the background is unimportant.

There are many many great uses for maps. For example dictionaries where you look up translations for a given term. Or perhaps grading where you map students to the grades they got in their exam. How exactly does it do that? You dont need to know. Not at the moment. Just know that a HashMap is really damn fast in all common use cases.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
October 18 2016 22:39 GMT
#15635
Okay so my problem is that I am thinking that a map is more than just an abstraction. A map is literally just the concept of key --> value ? of course it's also an interface but I guess there are potentially endless ways to implement it.
FunkyLich
Profile Blog Joined August 2010
United States107 Posts
Last Edited: 2016-10-18 22:55:07
October 18 2016 22:47 GMT
#15636
You should not continue with this because it is wrong. A map has very little to do with a set.


Half a map is a set, it has very very much to do with a set. In fact the standard implementation of Set -- HashSet -- is based on the implementation of HashMap.

On October 19 2016 06:40 travis wrote:
I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.

I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right?


A map is literally just the concept of key --> value ?


You are right. That's all Map specifies. the Map enforces unique keys, which is valuable because your data structure is maintaining a database with a uniqueness constraint: no student should appear more than once.

it isn't doing something you can't do with other data structures right?


This is a tricky question to answer. In the strict sense, you really don't *need* any data structure for anything. As you can implement any function without any defined data structures. But in the loose sense yes, a map allows you to stucture your data that allows you to do things *better* than an array for example, particularly when doing look-up operations. That coupled with their simplicity and ease of modelling relationships makes them *utterly ubiquitous* in the programming world. So definitely get familiar with it.


So I just wanted to respond to those quickly, because I didn't want there to be any misconceptions about how Maps worked. I really think you should take a step back though, because this conversation is focusing way too much on implementations of core data structures, which only have accidental relevance to the problem you're solving: No matter what the implementation is, as long as it adheres to the contract of its interface you will have a correct solution. A HashMap is usually the default implementation of Map. It has very fast lookups. That's literally all you need to know about HashMaps.

If I were looking at this problem, I would start by modelling some example data. From your description, here's what I'm picturing. Basically you are representing a relationship between one student and many courses. one-to-many

Student [keyed by student id]
Course [keyed by course id]
...

Tom [id=100]
CS-101
MATH-300

Jerry [id=101]
CS-105
LIT-300

So a Map<String, List<String>> models this pretty easily. The String key is the student id which is unique, and the value is a List of course ids ("CS-105", etc...). Knowing this you know what your data structure is supposed to look like *all the time*. Now you just need to implement each piece.

The class has a constructor to make an empty map


You already know exactly the question to ask for this:

For example, the constructor. I have no idea how to create an empty map. Just "Map name = new Map" ?


So you're one stack overflow / google search away, but I'll save you some time. Just use "Map<String, List<String>> name = new HashMap<String, List<String>>()". HashMaps are good, fast, and standard, and you don't have any crazy scaling, performance, or concurrency requirements. It's a simple choice.

It has an add method, which adds a student and a course, or just a course if the student is already in the map


Implementing this method is just a matter of taking into account the two conditions you've already described. If the student is NOT present in the map (which you can look up how to do) add the key with a new list containing the one course. So for this part, you'll need know how to "new up" a list (may as well use the ArrayList implementation), add a value to the list, and add a key-value pair to a map. If the student IS present in the map, add a course to the list which can safely be assumed to exist, since you added it when you initialized the student key, and you never need to null out the array.

it has a remove method, which removes the provided course from the student


Know how to look up a student's course, the value associated with that key. And know how to remove something from a list.

a boolean method which checks if anyone is taking a given course


Implementing this means iterating through the map and iterating through each list: a nested for-loop. Although, not required, I highly recommend looking up "java for each loop", which is an extremely useful syntax for looping through data structures that implement the Iterable interface, which is every Collection implements.

I didn't read every response to this thread, just kind of skimmed, so sorry if some of that's already been discussed. I just recommend start hammering away at it with a debugger, and ask google questions as they come up. Don't worry about doing everything *right* for now. Just make it work, and the "doing things *right*" part will come later.

Happy hacking

EDIT: forgot the generic part for the newing up the Map
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2016-10-18 23:00:06
October 18 2016 22:53 GMT
#15637
On October 19 2016 07:39 travis wrote:
Okay so my problem is that I am thinking that a map is more than just an abstraction. A map is literally just the concept of key --> value ? of course it's also an interface but I guess there are potentially endless ways to implement it.


For your task, all you really need to know is that map is a container of key-value structured data. You shouldn't care about efficiency unless they ask you. As others said, maps could be used for dictionaries. They could also be used as a telephone register (e.g. Map<NameOfPerson, TelephoneNumber>) and more than that. The point is, that's all you should be concerned about how maps work. Really, if I were you, I'd be more concerned about add, remove and search at this point (even though they are trivial from that description).

In your free time, try to understand when you need each collection. I wouldn't say every programmer knows every collection, but I'd say there is no serious programmer who doesn't know what map, array list (vector), set and linked list are.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2016-10-19 00:02:55
October 18 2016 23:56 GMT
#15638
Okay good news. My program passed all public tests. But 50% of it is secret tests.

But.. I am a bit confused about something.

my add method:


public void add(String student, String course) {
if(courses.containsKey(student)) {
courseList.add(course);
courses.put(student, courseList);
} else {
courseList = new ArrayList<String>();
courseList.add(course);
courses.put(student, courseList);
}
}


I had already defined courseList as a public class member.
and I am confused how this works..
In particular:

if(courses.containsKey(student)) {
courseList.add(course);
courses.put(student, courseList);
}


how does it keep track of which courseList is being added to?
am I doing this wrong an I am just lucky(or unlucky rather) that the tests didn't test that properly?



edit:

Instead of courseList.add(course);

should I be doing:

courseList = courses.get(student);
courseList.add(course);
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2016-10-19 00:15:36
October 19 2016 00:05 GMT
#15639
You're on the right track already with "how does it keep track of which courseList is being added to?"

Why did you make courseList a field in your class? Having an ArrayList field in Database class means that the database has a unique List. Each value in your map should have an ArrayList so it should not be a member of the Database class.

edit:
Personally I would leverage the fact that the get method returns NULL if the key is not in the map, but there are many different implementations you can use that all work.
I'll always be your shadow and veil your eyes from states of ain soph aur.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
October 19 2016 00:17 GMT
#15640
On October 19 2016 09:05 Blitzkrieg0 wrote:
You're on the right track already with "how does it keep track of which courseList is being added to?"

Why did you make courseList a field in your class? Having an ArrayList field in Database class means that the database has a unique List. Each value in your map should have an ArrayList so it should not be a member of the Database class.


Well, courseList isn't instantiated until it needs to be added.
Prev 1 780 781 782 783 784 1032 Next
Please log in or register to reply.
Live Events Refresh
WardiTV Team League
12:00
Group B
WardiTV667
TKL 167
IndyStarCraft 124
3DClanTV 42
musti20045 33
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko358
ProTech200
TKL 167
SortOf 141
IndyStarCraft 124
StarCraft: Brood War
Britney 39738
Sea 2068
EffOrt 669
BeSt 646
Jaedong 595
Larva 551
Killer 543
Mini 525
Rush 517
Soma 428
[ Show more ]
Stork 328
Light 309
Snow 250
ZerO 234
hero 104
Sharp 85
Mind 84
ToSsGirL 60
Barracks 45
Sea.KH 44
Shine 38
Backho 34
[sc1f]eonzerg 30
Bale 30
soO 21
Movie 18
Icarus 16
Terrorterran 15
eros_byul 1
Dota 2
Gorgc745
BananaSlamJamma253
League of Legends
JimRising 88
Counter-Strike
fl0m2349
Fnx 1328
x6flipin502
oskar51
Super Smash Bros
Mew2King63
Westballz33
Other Games
singsing3397
B2W.Neo928
hiko505
shoxiejesuss363
crisheroes278
XaKoH 182
Happy128
ToD116
QueenE73
djWHEAT27
Trikslyr23
ZerO(Twitch)15
Organizations
Dota 2
PGL Dota 2 - Main Stream142
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Michael_bg 1
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV259
• lizZardDota2120
League of Legends
• Jankos1715
Upcoming Events
Big Brain Bouts
3h 41m
LetaleX vs Babymarine
Harstem vs GgMaChine
Clem vs Serral
Korean StarCraft League
13h 41m
RSL Revival
20h 41m
Maru vs Zoun
Cure vs ByuN
uThermal 2v2 Circuit
1d 1h
BSL
1d 6h
RSL Revival
1d 20h
herO vs MaxPax
Rogue vs TriGGeR
BSL
2 days
Replay Cast
2 days
Replay Cast
2 days
Afreeca Starleague
2 days
Sharp vs Scan
Rain vs Mong
[ Show More ]
Wardi Open
2 days
Monday Night Weeklies
3 days
Sparkling Tuna Cup
3 days
Afreeca Starleague
3 days
Soulkey vs Ample
JyJ vs sSak
Replay Cast
4 days
Afreeca Starleague
4 days
hero vs YSC
Larva vs Shine
Kung Fu Cup
4 days
Replay Cast
5 days
The PondCast
5 days
WardiTV Team League
5 days
Replay Cast
6 days
WardiTV Team League
6 days
Liquipedia Results

Completed

Proleague 2026-03-18
WardiTV Winter 2026
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
Jeongseon Sooper Cup
BSL Season 22
CSL Elite League 2026
RSL Revival: Season 4
Nations Cup 2026
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual

Upcoming

ASL Season 21
Acropolis #4 - TS6
2026 Changsha Offline CUP
CSL 2026 SPRING (S20)
CSL Season 20: Qualifier 1
Acropolis #4
IPSL Spring 2026
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
NationLESS Cup
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.