OCP

Collections: Did you know ..

Did you know:

  • that LinkedList implements both Queue and List
  • that this means you can remove and add items from the beginning and end of the list in constant  time
  • that you should use  ArrayDeque instead of Stack
  • that a Set does not allow duplicate entries
  • that a HashSet uses hashCode() to store its entries
  • that this is demonstrated by the set below printing out it’s content based on the number field of a Drink object
    package collections;
    
    import lombok.AllArgsConstructor;
    
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;
    
    public class UseHashSet {
    
        public static void main(String[] args) {
            Drink firstDrink = new Drink("Cola", true, 13);
            Drink secondDrink = new Drink("Beer", true, 4);
            Drink thirdDrink = new Drink(   "Wiskey", false, 1);
            Drink fourthDrink = new Drink("Water", false, 14);
    
            Set<Drink> setOfDrinks = new HashSet<>(Arrays.asList(firstDrink, secondDrink, thirdDrink, fourthDrink));
    
            // [Drink{name='Wiskey', number=1}, Drink{name='Beer', number=4}, Drink{name='Cola', number=13}, Drink{name='Water', number=14}]
            System.out.println(setOfDrinks); 
        }
    
        @AllArgsConstructor
        static class Drink{
            private String name;
            private boolean isSparkley;
            private int number;
    
            @Override
            public String toString() {
                return "Drink{" +
                        "name='" + name + '\'' +
                        ", number=" + number +
                        '}';
            }
    
            @Override
            public int hashCode() {
                return number;
            }
        }
    }
    
  • that Map is actually not a part of the Collection framework
  • that offer() will add to the back of the queue and return true or false
  • that add() will add to the back of the queue and return true or an exception
  • that poll() will remove from the front of the queue and return the next element if there is any (else null)
  • that remove() will remove from the front of the queue or throw an exception when queue is empty
  • that pop() will remove from the front of the queue or throw an exception when queue is empty
  • that peek() lets you spy on the front element of the queue without removing it
  • that push()will add to the front of the queue and return void
  • that you  cannot put null in ArrayDeque because it has methods that use null as a special return value
  • that TreeMap and TreeSet are sorted
  • and this means they call the compareTo() method
  • that Map cannot contain duplicate keys
  • but it can contain duplicate values
  • that HashTable cannot contain null key nor values
  • that Comparable  is often confused with Comparator
  • that Comparable uses compareTo()
  • and Comparator uses compare()
  • that both are functional interface
  • but only Comparator is commonly implemented with a lambda
  • that it would be silly to implement Comparable with a lambda  since the point is to implement it in the object being compared
  • that instead of using an anonymous class you can use a lambda expression
  • if it call only one  method you can use a method reference
  • that this is an example of a method reference:
  • Consumer<String> consumer = s -> System.out.println(s);
    
    // can be written as
    
    Consumer<String> consumer = System.out::prinln;
  • that Java8 introduced some nice methods to use on collections like removeIf(), replaceAll(), forEach()
  • that Java8 also did the same for map eg. putIfAbsent() and merge()
  • that this is an example of the merge() method and will print out Tom : Icecream, Nick : Fries and Loren : Strawberries on different lines:
  • import java.util.HashMap;
    import java.util.Map;
    import java.util.function.BiFunction;
    
    public class UseMergeOnMap {
    
        public static void main(String[] args) {
    
            Map<String, String> favoriteFood = new HashMap<>();
            favoriteFood.put("Nick", "Fries");
            favoriteFood.put("Tom", null);
    
            BiFunction<String, String, String> mapper = (s1, s2) -> s1.compareTo(s2) < 0 ? s1 : s2;
    
            favoriteFood.merge("Nick", "Pancakes", mapper);
            favoriteFood.merge("Loren", "Strawberries", mapper);
            favoriteFood.merge("Tom", "Icecream", mapper);
    
            favoriteFood.forEach((k,v) -> System.out.println(k + " : " + v));
        }
    }
  • that Vector implements List
  • that Map uses put() instead of add()
  • that you  just got a better understanding of  Java Collections
  • that this is the end of the blogpost

Leave a Reply

Your email address will not be published.