• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 19:57
CEST 01:57
KST 08:57
  • 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
Serral wins HomeStory Cup 2914Serral wins Maestros of the Game 243ByuL, and the Limitations of Standard Play3Team Liquid Map Contest #22: Results and Winners7Code S Season 2 (2026): RO4 and Finals Preview12
Community News
Weekly Cups (July 27-Aug 2): SHIN's big week0SC4ALL II: Brood War - $2500 - Dec 5-68SC4ALL II announced - $10,000 prize pool, Dec 5-64PIG STY FESTIVAL 8.0! (13 - 23 August)14Neeb returns to progaming; rejoins ONSYDE18
StarCraft 2
General
protoss aoe skill expression 5.0.16 patch for SC2 goes live (8 worker start) StarCraft 2 at SC4ALL II Invite #3/8 - Clem Balance hotfix patch 5.0.16b (July 16) Clem: "I don't have that much hope in Blizzard"
Tourneys
PIG STY FESTIVAL 8.0! (13 - 23 August) Tasteless Time Warp: SC Evo Showmatch (May 25) SC4ALL II announced - $10,000 prize pool, Dec 5-6 Sparkling Tuna Cup - Weekly Open Tournament RSL Revival: Season 6 - Qualifiers and Main Event
Strategy
[G] Having the right mentality to improve
Custom Maps
Nexus Wars 2021 GUIDE [M] (2) Industrial Park
External Content
The PondCast: SC2 News & Results Mutation # 537 Hostile Territory Mutation # 536 Railroad Switch Mutation # 535 Assembly of Vengeance
Brood War
General
ASL 22 Proposed Map Pool Making an Online Broodwar Manager Game ASL22 General Discussion [Personal Project Share] Terran Defense v0.60 Data needed
Tourneys
KCM Race Survival 2026 Season 3 Small VOD Thread 2.0 SC4ALL II: Brood War - $2500 - Dec 5-6 [Megathread] Daily Proleagues
Strategy
Any training maps people recommend? Fighting Spirit mining rates Simple Questions, Simple Answers Odyssey Mineral Stack Saturation
Other Games
General Games
General RTS Discussion Thread Stormgate/Frost Giant Megathread Nintendo Switch Thread ZeroSpace Early Access is Now Live! Beyond All Reason
Dota 2
Looking for a Dota Mentor Official 'what is Dota anymore' discussion
League of Legends
[TL LoL EUW IHs] Teemo shall perish TSM pausing esports and CLG Dead
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Power Rank TL Mafia Community Thread NeO.D_StephenKing vs This Guy From 1 Million Dance
Community
General
US Politics Mega-thread European Politico-economics QA Mega-thread The Games Industry And ATVI Russo-Ukrainian War Thread Artificial Intelligence Thread
Fan Clubs
The Clem Fan Club INnoVation Fan Club The Scarlett Fan Club
Media & Entertainment
Anime Discussion Thread Movie Discussion! Series you have seen recently... [Req][Books] Good Fantasy/SciFi books
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion MLB/Baseball 2023 McBoner: A hockey love story
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Simple Questions Simple Answers FPS when play League Of Legend on laptop
TL Community
The Automated Ban List Northern Ireland Global Starcraft
Blogs
Please support my new stand…
Peanutsc
What is a Gamer?
TrAiDoS
Hello guys!
LIN1s
ASL S22 English Commentary…
namkraft
Poker (part 2)
Nebuchad
Customize Sidebar...

Website Feedback

Closed Threads



Active: 5338 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
Spain18405 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
Next event in 3m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ViBE163
NeuroSwarm 129
ProTech105
StarCraft: Brood War
Rain 1544
GuemChi 1406
Artosis 604
Purpose 22
League of Legends
Doublelift4808
Counter-Strike
summit1g9544
tarik_tv3109
Fnx 595
minikerr73
Super Smash Bros
PPMD36
Other Games
gofns18126
Day[9].tv689
C9.Mang0230
UpATreeSC135
Maynarde96
Mew2King32
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 16 non-featured ]
StarCraft 2
• Hupsaiya 116
• RyuSc2 18
• intothetv
• AfreecaTV YouTube
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• masondota21515
League of Legends
• imaqtpie1910
Other Games
• Day9tv689
• Scarra442
Upcoming Events
Replay Cast
3m
Escore
10h 3m
IntoTheTV X SOOP
11h 3m
Big Brain Bouts
16h 3m
DnS vs Rex
Reynor vs Cure
Replay Cast
1d
RSL Revival
1d 9h
herO vs SHIN
Clem vs Solar
Serral vs Rogue
Ladder Legends
1d 18h
Ladder Legends
1d 20h
RSL Revival
2 days
OSC
2 days
[ Show More ]
OSC
3 days
WardiTV Weekly
3 days
The Patches Monday
3 days
Sparkling Tuna Cup
4 days
PiG Sty Festival
4 days
PiGosaur Cup
5 days
Replay Cast
5 days
Kung Fu Cup
5 days
Replay Cast
6 days
The PondCast
6 days
Liquipedia Results

Completed

Proleague 2026-08-05
CranK Gathers Season 4: BW vs SC2 Team League
Eternal Conflict S2 Finale

Ongoing

KCM Race Survival 2026 Season 3
K-JUNGMAN
Acropolis #5
RSL Revival: Season 6
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026

Upcoming

Escore Tournament S3: W6
Escore Tournament S3: W7
CSLAN 4
ASL Season 22
Escore Tournament S3: W8
Acropolis #5 - TRS
Blizzard Classic Cup 2026
HSC XXX
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
PiG Sty Festival 8.0
Big Dog Cup 2026 Div 1
Thunderpick World Champ.
ESL Pro League Season 24
Stake Ranked Episode 4
Logitech G Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #5
BLAST Open Fall 2026
Esports World Cup 2026
Esports World Cup 2026: LCQ
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.