• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 14:49
CEST 20:49
KST 03:49
  • 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
RSL Season 1 - Final Week6[ASL19] Finals Recap: Standing Tall12HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0
Community News
Esports World Cup 2025 - Brackets Revealed10Weekly Cups (July 7-13): Classic continues to roll4Team TLMC #5 - Submission extension3Firefly given lifetime ban by ESIC following match-fixing investigation17$25,000 Streamerzone StarCraft Pro Series announced7
StarCraft 2
General
The GOAT ranking of GOAT rankings RSL Revival patreon money discussion thread Who will win EWC 2025? Weekly Cups (July 7-13): Classic continues to roll Esports World Cup 2025 - Brackets Revealed
Tourneys
FEL Cracov 2025 (July 27) - $8000 live event RSL: Revival, a new crowdfunded tournament series $5,100+ SEL Season 2 Championship (SC: Evo) WardiTV Mondays Sparkling Tuna Cup - Weekly Open Tournament
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
External Content
Mutation # 482 Wheel of Misfortune Mutation # 481 Fear and Lava Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome
Brood War
General
Flash Announces (and Retracts) Hiatus From ASL BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion Starcraft in widescreen A cwal.gg Extension - Easily keep track of anyone
Tourneys
[Megathread] Daily Proleagues Cosmonarchy Pro Showmatches CSL Xiamen International Invitational [BSL20] Non-Korean Championship 4x BSL + 4x China
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile Nintendo Switch Thread CCLP - Command & Conquer League Project The PlayStation 5
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Future of Porn Stop Killing Games - European Citizens Initiative Summer Games Done Quick 2025!
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread [\m/] Heavy Metal Thread
Sports
Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023 2024 - 2025 Football Thread NBA General Discussion NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Men Take Risks, Women Win Ga…
TrAiDoS
momentary artworks from des…
tankgirl
from making sc maps to makin…
Husyelt
StarCraft improvement
iopq
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 723 users

The Big Programming Thread - Page 782

Forum Index > General Forum
Post a Reply
Prev 1 780 781 782 783 784 1031 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
Spain17976 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 1031 Next
Please log in or register to reply.
Live Events Refresh
WardiTV European League
16:00
Swiss Groups Day 4
Harstem vs HeRoMaRinELIVE!
WardiTV1046
TKL 307
IndyStarCraft 279
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 566
TKL 307
IndyStarCraft 279
UpATreeSC 156
BRAT_OK 82
MindelVK 35
StarCraft: Brood War
EffOrt 687
Rush 615
BeSt 354
firebathero 346
Larva 60
PianO 52
Movie 42
Aegong 26
JulyZerg 23
sas.Sziky 23
[ Show more ]
yabsab 15
sSak 8
SilentControl 5
Dota 2
qojqva3888
canceldota66
League of Legends
Grubby3649
Counter-Strike
pashabiceps1197
flusha300
Heroes of the Storm
Liquid`Hasu137
Other Games
FrodaN4790
Beastyqt722
ceh9685
mouzStarbuck167
oskar140
Skadoodle137
elazer114
C9.Mang084
ArmadaUGS82
Mew2King62
Trikslyr61
QueenE60
Organizations
Other Games
gamesdonequick3389
StarCraft 2
angryscii 9
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• davetesta23
• intothetv
• sooper7s
• Migwel
• AfreecaTV YouTube
• LaughNgamezSOOP
• IndyKCrew
• Kozan
StarCraft: Brood War
• 80smullet 18
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 2615
League of Legends
• Nemesis2831
• Jankos1904
• TFBlade829
Other Games
• imaqtpie1592
• Shiphtur214
Upcoming Events
PiGosaur Monday
5h 11m
uThermal 2v2 Circuit
21h 11m
Replay Cast
1d 5h
The PondCast
1d 15h
WardiTV European League
1d 21h
Replay Cast
2 days
Epic.LAN
2 days
CranKy Ducklings
3 days
Epic.LAN
3 days
CSO Contender
3 days
[ Show More ]
BSL20 Non-Korean Champi…
3 days
Bonyth vs Sziky
Dewalt vs Hawk
Hawk vs QiaoGege
Sziky vs Dewalt
Mihu vs Bonyth
Zhanhun vs QiaoGege
QiaoGege vs Fengzi
Sparkling Tuna Cup
4 days
Online Event
4 days
BSL20 Non-Korean Champi…
4 days
Bonyth vs Zhanhun
Dewalt vs Mihu
Hawk vs Sziky
Sziky vs QiaoGege
Mihu vs Hawk
Zhanhun vs Dewalt
Fengzi vs Bonyth
Esports World Cup
6 days
ByuN vs Astrea
Lambo vs HeRoMaRinE
Clem vs TBD
Solar vs Zoun
SHIN vs Reynor
Maru vs TriGGeR
herO vs Lancer
Cure vs ShoWTimE
Liquipedia Results

Completed

2025 ACS Season 2: Qualifier
RSL Revival: Season 1
Murky Cup #2

Ongoing

JPL Season 2
BSL 2v2 Season 3
Copa Latinoamericana 4
Jiahua Invitational
BSL20 Non-Korean Championship
Championship of Russia 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters

Upcoming

CSL Xiamen Invitational
CSL Xiamen Invitational: ShowMatche
2025 ACS Season 2
CSLPRO Last Chance 2025
CSLPRO Chat StarLAN 3
BSL Season 21
K-Championship
RSL Revival: Season 2
SEL Season 2 Championship
uThermal 2v2 Main Event
FEL Cracov 2025
Esports World Cup 2025
Underdog Cup #2
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
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 © 2025 TLnet. All Rights Reserved.