Category: Java


Page asset provides Category for categorization purpose. In this article i will explain “how to create new Category for page assets”.

  • Login to system with admin rights
  • Go to Advance environment
  • Goto Admin > Asset Types > Page > Categories

Here we will find all association with page asset (see Figure 1)

Category

Figure 1 – Page Category

To create a new association, click on “Add New Category button”

Category

And fill the form (see Figure – 2)

CategoryForm

Figure 2 – Page Category creation form

Description: Information about this page category. Make it small and informative.
Category Code: This code will be used get the value. This is a 4 char value. Please do not put blank space.

  • Click “Add New Category” Button

Other Related Posts:
Fatwire – How to create new association with page asset

In any kind of implementation, i (actually everyone) perform some empty string operation:

if(null != myString && !"".equals(myString))
    ...
}

.equals() method compare each character of strings to check the equality. In the example above we are:

  • Creating a String object (“”) with 0 length
  • using .equals() method to check if both strings are equal or not

Note: String class store values as char array

But when i had a look into the implementation of .equals() method, the idea comes in my mind why not use length() method in this scenario to save the .equals() overhead. length() method just return the length of string which String class store in count variable at the time we create String object. So its definition looks like:

public int length() {
    return count;
}

As simple as it can be. So i have simply replaced my code with length() metohd

if(null != myString && !0 == myString.length()) {
    ...
}

Is it safe to replace .equals() with .length() in my code?

It depends on your requirement actually, equals() method check for equality of two objects (string in our case), while .length() just return the number of characters in String object.

HashMap – How it works?

On very high level, Hash Map is used to store data in form of key value pair.

Hash Map is a Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.” … from Java API

After reading this definition, some question comes in my mind:

  1. How hash map store data internally?
  2. What happen when I try to store some new information in map?
  3. How hash map find my data?

And when I tried to explore it, I find it more and more interesting.

HashMap has a static class named Entry which implements Map.Entry interface. The Entry class looks like:

static class Entry implements Map.Entry {

final Object key;
Object value;
final int hash;
Entry next;
Entry(int i, Object obj, Object obj1, Entry entry) {
value = obj1;
next = entry;
key = obj;
hash = i;

}
// Other methods

}

Every time we insert a <key,value> into hashmap using .put() method, a new Entry object is created (not true is some cases. if key already exists, then it just replace the value).  Map internally used two data structures to manage/store data:

  1. Array
  2. Link List

This image shows how hashmap manage data. Here

  • Each index of array is a bucket
  • To identify the bucket for any <key,value>, Hash map use key.hashCode() and perform some operation:
    Bucket (index) =HashMap.indexFor (HashMap.hash(key.hashCode()), entryArray.length)
    It means, two keys with different hashCode can fall under same bucket.
  • If a bucket is empty (table[i] is null) then the Entry object is simply inserted at ith position
    table[i] = new Entry(hash, key, value, null)
  • If a bucket has some values, then the following algo is used:
    Entry entry = table[i]

    Table[i] = new Entry(hash,key,value,entry)

    It means, the latest entry resides on the top of the bucket.

  • If a key already exists in a map, then it just replace the value and return the old value
    If(entry.key == key || key.equals(entry.key)
    Then

    Object oldValue = entry.value;
    Entry.value = newValue;
    Return oldValue;

    End if

  • In case of new key,  map add the key, value (a new Entry object with key value) and return null
  • Entry.hash is not the hashCode of key, but HashMap use its own hash algo to create hash based on key.hashCode(). So it’s like:
    entry.hash = HashMap.hash(key.hashCode());
  • HashMap allow single null as key, in case of null key, it create a dummy object and use it as key.
    static final Object NULL_KEY = new Object();
  • hashMap.get(Obj) and hashMap.containsKey(obj), both method iterate through the map. Avoid to use extra call of containsKey().

    If(null != map.containsKey(obj)) {

    myObj = map.get(obj);
    //Perform other operations

    }

    This can be easily replaced by:

    myMap  = map.get(obj);
    if(null != myMap) {

    // Perform other operations

    }

Follow

Get every new post delivered to your Inbox.