CodeGym /Courses /Java Collections /Big task: Create a URL shortener

Big task: Create a URL shortener

Java Collections
Level 6 , Lesson 15
Available

"Hi, Amigo!"

"Hello, Captain Squirrels, sir!"

"You have a new secret mission today. In order to encrypt correspondence between our units, you need to implement your own URL shortener service."

"Cool! Uhh, I mean, I am ready, sir. But why do we need this?"

"What do you mean why? Soldier, we're surrounded by enemies. We need to protect our lines of communication. Proceed to the task."

"Yes, sir! Implement a URL shortener service."

"Go to our secret agent IntelliJ IDEA. You'll receive all of the instructions there."

"It shall be done, Captain!"

"Proceed."

Big task: Create a URL shortener - 1
16
Task
Java Collections, level 6, lesson 15
Locked
Shortener (part 1)
Let's write a Shortener. It will be similar to Google URL Shortener (https://goo.gl), but we'll extend its functionality and make it console-based. It will shorten not only links, but also any string.
9
Task
Java Collections, level 6, lesson 15
Locked
Shortener (part 2)
Shortener will support various data storage strategies (for strings and string identifiers). All these strategies will be inherited from the StorageStrategy interface. Read more about the Strategy pattern on Wikipedia. Our repository will rely on two concepts: key and value.
16
Task
Java Collections, level 6, lesson 15
Locked
Shortener (part 3)
Let's return to the Shortener class: 3.1. Add a Long lastId field to it. Initialize it to zero. This field will hold the identifier of the last string added to the repository. 3.2. Add a StorageStrategy storageStrategy field that will store the data storage strategy.
16
Task
Java Collections, level 6, lesson 15
Locked
Shortener (4)
We'll need several helper classes: 4.1. Create a Helper class. 4.1.1. Add a static String generateRandomString() method, which will generate a random string. Use the SecureRandom and BigInteger classes to do this. Hint: search for "random string Java" on Google.
16
Task
Java Collections, level 6, lesson 15
Locked
Shortener (5)
Let's write our first repository (storage strategy). Internally, it will have an ordinary HashMap. We'll keep all the strategies in the strategy package. 5.1. Create a HashMapStorageStrategy class that implements the StorageStrategy interface.
32
Task
Java Collections, level 6, lesson 15
Locked
Shortener (6)
The first strategy is ready. It's time to test it. To do this: 6.1. Create a Solution class if you haven't already done so. 6.2. Add implementations of the static helper methods to the Solution class: 6.2.1. Set<Long> getIds(Shortener shortener, Set<String> strings).
32
Task
Java Collections, level 6, lesson 15
Locked
Shortener (7)
Let's start implementing the second strategy: OurHashMapStorageStrategy. It won't use the ready-made HashMap from the standard library, but rather will itself be a collection. 7.1. Understand how the standard HashMap works. Look at its source code or google for articles about it.
32
Task
Java Collections, level 6, lesson 15
Locked
Shortener (8)
Add and implement a OurHashMapStorageStrategy class, using the Entry class from the previous subtask. The OurHashMapStorageStrategy class must implement the StorageStrategy interface. 8.1. Add the following fields to the class: 8.1.1. static final int DEFAULT_INITIAL_CAPACITY = 16.
32
Task
Java Collections, level 6, lesson 15
Locked
Shortener (9)
Let's write another strategy. We'll call it FileStorageStrategy. It will be very similar to OurHashMapStorageStrategy, but files will be the buckets. I'm sure you know what I mean by buckets. If not, copy the internal workings of HashMap. 9.1. Create a FileBucket class in the strategy package.
32
Task
Java Collections, level 6, lesson 15
Locked
Shortener (10)
Create and implement a FileStorageStrategy class. It must: 10.1. Implement the StorageStrategy interface. 10.2. Use FileBucket as a buckets. Hint: The class must have a FileBucket[] table field.
16
Task
Java Collections, level 6, lesson 15
Locked
Shortener (11)
As you've noticed, getting an identifier using a string takes a lot more time than getting a string using an identifier. This is expected and is a consequence of HashMap's implementation. Let's write a fourth strategy: OurHashBiMapStorageStrategy, which will eliminate this shortcoming.
16
Task
Java Collections, level 6, lesson 15
Locked
Shortener (12)
It's not all that uncommon to need to create a Map that works in two directions (from key to value, and from value to key). Implementations of such collections already exist in various third-party libraries. One such is guava from Google. 12.1. Download and connect the guava library, version 19.0.
16
Task
Java Collections, level 6, lesson 15
Locked
Shortener (13)
Consider another implementation of BiMap — this time from Apache Commons Collections. 13.1. Download and connect Apache Commons Collections 4.0. 13.2. Implement a DualHashBidiMapStorageStrategy. It must: 13.2.1. support the StorageStrategy interface.
32
Task
Java Collections, level 6, lesson 15
Locked
Shortener (14)
We've tested our strategies many times using the Solution class's testStrategy() method. It's time to use JUnit to write real unit tests. 14.1. Read about unit tests. 14.2. Download and connect the JUnit 4.12 library. Figure out how to use it. The JUnit library depends on the hamcrest-core library.
16
Task
Java Collections, level 6, lesson 15
Locked
Shortener (15)
We're going to write another test that verifies that using HashBiMapStorageStrategy to get the identifier for a string can be faster than using HashMapStorageStrategy. 15.1. Create a SpeedTest class in the tests package.
9
Task
Java Collections, level 6, lesson 15
Locked
Shortener (16)
Stuff you can do on your own (we won't test you on any of these items): - Add a strategy for working with a database. Google JDBC. - Make a web service that returns an identifier for any URL or string, and returns the string for an identifier.
Comments (16)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Boat Level 40, Germany, Germany
15 May 2023
For part 3, the Requirements state that the field should be named LastID but this won't verify, it has to be LastId as the main Conditions state. Just some confusing inconsistency.
Justin Smith Level 41, Greenfield, USA, United States
28 December 2022
Part 4 doesn't say how long the random string should be. Isn't that an important piece of information, or is it up to us to arbitrarily decide how long? Also, this is another problematic example of "google the answer"... In this case, googling exactly as they say returns results that don't even use the BigInteger class, so it's not helpful. Here are results I get when searching for this. https://www.baeldung.com/java-random-string https://stackoverflow.com/questions/20536566/creating-a-random-string-with-a-z-and-0-9-in-java https://www.geeksforgeeks.org/generate-random-string-of-given-size-in-java/ Since I'm not really sure how BigInteger would play a role in solving this, and the google results aren't helping, I'm going to press forward on a solution without BigInteger and see if it passes. Hopefully it's not one of the tasks that specifically checks for a certain class. I'm also just going to assume a length of 25, since it doesn't say. EDIT: My solution seems to work, but I can confirm that it won't pass validation unless you use BigInteger. The validation page unfortunately says "use your favorite search engine" but the search results don't use BigInteger. Thankfully, the validation page also essentially provides the solution they want. The we come to part 8, where I can see other people have also had a lot of problems. Part of the problem with this one is that they say that if you don't know what the methods should do, look up the corresponding methods for HashMap. But HashMap doesn't have any of those methods we're adding, so I'm not really sure what they are talking about. I worked on this on my own for a while and I figured I would do what I can and then use the solution for methods where I didn't even know what they were looking for. When I got to that point, I found that all of my work was very far off from what they were asking for. Found this part very discouraging, overall.
Alex Buyanov Level 47, Netherlands
14 October 2022
For part 14 (with JUnit tests), your support method testStrategy() MUST be public, or verification will fail. Though in the real world, I would have personally prefer to make all "helper" methods private... Also, for JUnit itself it does not matter.
Lisa Level 41
25 January 2022
part 8, here it starts that I say: boahh... this is really insane. I mentioned it before that I implemented already a lot of data structures including my own hash set. So I know how this is working (should work... in theory). However without any hint what the to implement methods should do this is haaaaarddd. Reaaaaalllly haaaard. Have just passed part 8 and now feel the need to share this. hihihi, now I've read part 9 requirements, getting interesting, too ohh, if your're German, than this is excellent study material from the HdM Stuttgart. Take a timeout for approx 3-4 weeks, watch the vids and code all the data structures he's talking of. Then you're prepared for this task. lil update: 9 and 10 are OK if you managed to code 8 and if you're fit with Files and Path classes (nio).
TheLordJackMC Level 39, Princeton, idk somewhere
12 August 2021
"almost impossible" they said
Andrei Level 41
23 July 2021
Not gonna lie, from about Task 9-10 I started copy pasting the solution... No background information, no prior explanations, just 'google' and expect me to solve only difficult tasks on my own. Sure buddy.
MaGaby2280 Level 41, Guatemala City, Guatemala
6 July 2021
Read more on Wikipedia ... lol Don´t give up... al repositories needed are in Maven
BlueJavaBanana Level 37
9 April 2021
Norbert Level 41, Kielce, Poland
28 November 2022
I think the easier way is open Project Structure, then choose libraries, then click in the upper left corner '+' click from maven, write there guava and choose it, click OK.
25 June 2020
This task is madness )
BlueJavaBanana Level 37
8 April 2021
Yep another CodeGym classic. Here is a simple lesson on lists that a child could understand. Now read through the 3000 lines of professional Java source code for the HashMap and make your own versions! Need help? Type in "HashMap" in google. I do get frustrated when we are given really simple lessons followed by tasks that are extremely difficult.
Tom Level 41, San Jose, Sweden
7 June 2021
But definitily we learn a lot!
yz Level 37, Jakarta, Indonesia
16 April 2020
for 7th task: Create Entry class; dont create OurHashMap.... class and inside the class make Entry class ...
BlueJavaBanana Level 37
7 April 2021
Helpful tip! The entry class should not be nested!