Pacific-Design.com

    
Home Index

1. Algorithms

2. Java

+ 01 Hash Map

+ 02 Hash Map Sorted

+ 03 Hash Map Linked

+ 04 Hash Map Nested

+ 05 Hash Table

+ 05 Tree Map

+ 06 Pretty Map

+ 07 Hash Set

+ 08 Linked List

+ 09 ArrayList

+ Anagram

+ Binary Search

+ Bitwise

+ Find 1 Part Distance

+ Find Smallest Difference

+ Group By Key

+ Largest Number

+ Mean Median Variance

+ Most Frequent Number

+ Palindrome

+ Permutation

+ Permutation Hex

+ Regex

+ Search Sorted Array

+ Smallest Difference

+ Stack

+ String Builder

+ Subsequences

+ Sum Of Two

Algorithms / Java /

ArrayList - dynamicaly resized

        ArrayList<String> array = new ArrayList<>();
        array.add("one");
        array.add("two");
        System.out.println(array.get(0));

        // Java 1.8 using lambda
        array.forEach(string -> {
            System.out.println(string);
        });

        for (String str : array) {
            System.out.println(str);
        }

Vector - forEach / synchornized

        Vector<String> vector = new Vector<>();
        vector.add("one");   vector.add("two");
        vector.add("three"); vector.add("four");
        System.out.println(vector.get(0));

        // Java 1.8 using lambda
        vector.forEach(string -> {
            System.out.println(string);
        });

        for ( String str : vector) {
            System.out.println(str);
        }

HashSet - forEach / lambda

        HashSet<String> hashSet = new HashSet();
        hashSet.add("A"); hashSet.add("B");
        hashSet.add("D"); hashSet.add("E");
        hashSet.add("C"); hashSet.add("F");

        // Java 1.8 with lambda
        final long[] integers = {0};
        hashSet.forEach(str -> {
            System.out.print(str + " = ");
            System.out.println(integers[0]++);
        });

        for (String str : hashSet) {
            System.out.println(str);
        }

HashMap - increment value for a key

        HashMap<String, Integer> map = new HashMap<>();
        map.put("one", 1);
        map.put("two", 2);

        if(map.containsKey("two")) {
            int tmp = map.get("two");
            map.put("two", tmp + 10);
        }

        for(Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(" Key   = " + entry.getKey()
                             + " Value = " + entry.getValue());
        }

LinkList - forEach / lambda

        LinkedList<String> list = new LinkedList<>();
        list.add("one");
        list.add("two");

        //--- using for loop ---//
        for(String str : list) {
            System.out.println(str);
        }

        //--- using lambda ---//
        final Integer[] integers = {0};
        final String[] strings = {"Kevin","Thomas"};

        list.forEach(string -> {
            System.out.println(string);
            integers[0]++;
            strings[0] = "Agoura";
        });


        //--- using iterator ---//
        Iterator iterator = list.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }


Collection Framework

A collection is just a group of objects. Collection can be of different types. Collection of Strings, numbers etc.


                 +-----------------------+
                 |       Interfaces      |
                 +-----------------------+
                   /                   \                
        +------------+              +------------+    +-------------+
        |    List    |              |    Set     |    |    Queue    |
        | (Ordered)  |              | (No Order) |    |    Deque    |
        +------------+              +------------+    +-------------+
         /         \                      |
        /           \                     V
+-----------+  +------------+       +------------+       
| ArrayList |  | LinkedList |       | Sorted Set | 
+-----------+  +------------+       +------------+       


Map represent a dictionary or hash table kind of data structure where each record has key and value. Each key is unique in the map. We look for a record using a key, and this search is constant time, and search is very fast.
                +--------------+
                |     Map      |
                +--------------+
                | Key  | Value | 
                +--------------+
                       |
                       V
                +--------------+
                |  Sorted Map  |
                +--------------+

References: