Program 27/1/22_0

Thomas
2 min readJan 28, 2022

and today we start late.

Queues. add and remove will raise an exception if the item being added or removed doesnt exist whereas offer and poll will return false and null respectively. Peak returns the head of the queue.

Queues are Useful for multithreading as you can have one code supplying items to queues and another waiting for items to be added.

Using iterators-

.iterator takes in a list and gives you the option to use hasNext(), next(), remove(). Unlike the for each loop you can remove items as you loop through the list items. For each uses iterator behind the scenes. ONCE AGAIN ** If you are adding or removing items from anywhere in a list besides the end then you want to use a linkedList instead of an arrayList.

So going through the implementing iterable video definitely is a jump up from beginner as he is showing us how to download from actual website using the iterable method. It doesn’t appear to work with the websites as they all have https security but I’m not sure if I did something else wrong. On to the next video.

This video is on how to choose what collection for what purpose. Which sounds extremely useful.

First decide if you want a list, set, or map.

Lists store objects like a shopping list. Duplicates are allowed. Objects remain in order you added them in. Elements are indexed via an integer. Checking for a particular item is slow. Looking an item up by index is fast. Iterating through a list is fast. You can sort lists if you want to.

Sets store only unique values. Great for removing duplicates. Not indexed. Very fast to check for particular object exists. If you want to add objects of a class you created yourself you must implement hashCode() and equals().

Maps have key value pairs. Retrieving a value by key is fast. Iterating over map values is VERY slow. Maps are not optimized for iteration. Iterating over your own objects as keys you must implement hashCode() and equals().

Lists- ArrayList and LinkedList

ArrayList for only adding or removing at end of list

LinkedList for adding or removing anywhere else.

Sets- HashSet, TreeSet, LinkedHashSet

HashSet is unordered.

TreeSet is naturally ordered. Must implement comparable for custom types.

LinkedHashSet remains in order it was added.

Maps- HashMap, TreeMap, LinkedHashMap

HashMap has unordered keys.

TreeMap is naturally ordered. Either alphabetical or numerical. Must implement comparable for custom types.

LinkedHashMap remain in order added.

// SortedSet and SortedMap are also interfaces you can use.

Now for complex data structures.

We are working with a 1d and 2d array.

We choose Map because the order doesn’t need to be maintained. We use a Set for the Maps value to get rid of duplicates.

--

--