• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 14:18
CET 19:18
KST 03:18
  • 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
[ASL20] Finals Preview: Arrival12TL.net Map Contest #21: Voting10[ASL20] Ro4 Preview: Descent11Team TLMC #5: Winners Announced!3[ASL20] Ro8 Preview Pt2: Holding On9
Community News
BSL21 Open Qualifiers Week & CONFIRM PARTICIPATION1Crank Gathers Season 2: SC II Pro Teams6Merivale 8 Open - LAN - Stellar Fest3Chinese SC2 server to reopen; live all-star event in Hangzhou22Weekly Cups (Oct 13-19): Clem Goes for Four3
StarCraft 2
General
Could we add "Avoid Matchup" Feature for rankgame RotterdaM "Serral is the GOAT, and it's not close" Smart servos says it affects liberators as well Chinese SC2 server to reopen; live all-star event in Hangzhou The New Patch Killed Mech!
Tourneys
Crank Gathers Season 2: SC II Pro Teams RSL Offline Finals Dates + Ticket Sales! Merivale 8 Open - LAN - Stellar Fest $5,000+ WardiTV 2025 Championship $3,500 WardiTV Korean Royale S4
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 497 Battle Haredened Mutation # 496 Endless Infection Mutation # 495 Rest In Peace Mutation # 494 Unstable Environment
Brood War
General
[ASL20] Finals Preview: Arrival BSL Season 21 BSL Team A vs Koreans - Sat-Sun 16:00 CET ASL20 Pre-season Tier List ranking! ASL Runner-Up Race Stats
Tourneys
[ASL20] Grand Finals BSL21 Open Qualifiers Week & CONFIRM PARTICIPATION ASL final tickets help [ASL20] Semifinal A
Strategy
Current Meta Soma's 9 hatch build from ASL Game 2 Simple Questions, Simple Answers Roaring Currents ASL final
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile General RTS Discussion Thread Nintendo Switch Thread Dawn of War IV
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
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
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
Things Aren’t Peaceful in Palestine US Politics Mega-thread Russo-Ukrainian War Thread YouTube Thread The Chess Thread
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Korean Music Discussion Series you have seen recently... Movie Discussion!
Sports
Formula 1 Discussion 2024 - 2026 Football Thread MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
The Benefits Of Limited Comm…
TrAiDoS
Sabrina was soooo lame on S…
Peanutsc
Our Last Hope in th…
KrillinFromwales
Certified Crazy
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1590 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
Spain18100 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
Monday Night Weeklies
17:00
Open Cup
RotterdaM543
TKL 310
IndyStarCraft 228
SteadfastSC196
ZombieGrub93
BRAT_OK 62
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 543
mouzHeroMarine 459
TKL 310
IndyStarCraft 228
SteadfastSC 196
UpATreeSC 100
ZombieGrub93
ProTech84
MaxPax 74
BRAT_OK 62
MindelVK 28
StarCraft: Brood War
Britney 21552
Sea 638
Yoon 363
Rock 54
Dewaltoss 37
yabsab 20
Dota 2
qojqva4220
Dendi1196
BananaSlamJamma252
PGG 89
Counter-Strike
fl0m2522
ScreaM1151
FunKaTv 64
Other Games
FrodaN1616
Beastyqt562
ceh9443
Lowko291
Liquid`VortiX219
mouzStarbuck210
B2W.Neo153
ArmadaUGS124
C9.Mang079
Mew2King74
QueenE62
rGuardiaN44
Organizations
Counter-Strike
PGL11530
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 22 non-featured ]
StarCraft 2
• kabyraGe 105
• iHatsuTV 6
• Reevou 2
• Kozan
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• LaughNgamezSOOP
• IndyKCrew
StarCraft: Brood War
• HerbMon 41
• FirePhoenix10
• Michael_bg 4
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 2788
League of Legends
• Nemesis3464
• imaqtpie1977
• TFBlade833
Other Games
• WagamamaTV415
• Shiphtur103
Upcoming Events
BSL 21
6h 42m
Replay Cast
15h 42m
Streamerzone vs Shopify Rebellion
Streamerzone vs Team Vitality
Shopify Rebellion vs Team Vitality
WardiTV Invitational
17h 42m
CrankTV Team League
18h 42m
BASILISK vs TBD
Team Liquid vs Team Falcon
BSL 21
1d 6h
Replay Cast
1d 15h
BASILISK vs TBD
Team Liquid vs Team Falcon
OSC
1d 17h
CrankTV Team League
1d 18h
Replay Cast
2 days
The PondCast
2 days
[ Show More ]
CrankTV Team League
2 days
Replay Cast
3 days
WardiTV Invitational
3 days
CrankTV Team League
3 days
Replay Cast
4 days
BSL Team A[vengers]
4 days
Dewalt vs Shine
UltrA vs ZeLoT
BSL 21
5 days
Sparkling Tuna Cup
5 days
BSL Team A[vengers]
5 days
Cross vs Motive
Sziky vs HiyA
BSL 21
6 days
Wardi Open
6 days
Liquipedia Results

Completed

ASL Season 20
WardiTV TLMC #15
Eternal Conflict S1

Ongoing

BSL 21 Points
CSL 2025 AUTUMN (S18)
BSL 21 Team A
C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
CranK Gathers Season 2: SC II Pro Teams
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
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

Upcoming

SC4ALL: Brood War
YSL S2
BSL Season 21
SLON Tour Season 2
BSL 21 Non-Korean Championship
RSL Offline Finals
WardiTV 2025
RSL Revival: Season 3
Stellar Fest
SC4ALL: StarCraft II
META Madness #9
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 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.