Saturday, 8 October 2016

Implement Bijective Map

Implement map, where following operations are performed in constant time.
a.   Get the value by Key
b.   Get the key be value
c.    Remove <key, value> pair by key
d.   Remove <key, value> pair by value.
e.   Check for the existence of key
f.    Check for the existence of value
g.   Get the size of the map

Assume there is one-to-one mapping between <key, value> pairs. By using two maps (one to keep track of <key, value> mappings and other to keep track of <value, key> mappings), we can implements Bijective map easily. Following is the complete working application.
import java.util.HashMap;
import java.util.Map;

public class BijectiveMap<K, V> {

 private Map<K, V> map = new HashMap<K, V>();
 private Map<V, K> inverseMap = new HashMap<V, K>();

 public void put(K key, V value) {
  map.put(key, value);
  inverseMap.put(value, key);
 }

 public V getValue(K key) {
  return map.get(key);
 }

 public K getKey(V value) {
  return inverseMap.get(value);
 }

 @SuppressWarnings("unchecked")
 public RemovedKey<K, V> removeKeyValuePairByKey(K key) {
  if (key == null || !map.containsKey(key)) {
   return RemovedKey.EMPTY_ENTRY;
  }

  V value = map.remove(key);
  inverseMap.remove(value);

  RemovedKey<K, V> removedKey = new RemovedKey<>(key, value);
  return removedKey;

 }

 @SuppressWarnings("unchecked")
 public RemovedKey<K, V> removeKeyValuePairByValue(V value) {
  if (value == null || !inverseMap.containsKey(value)) {
   return RemovedKey.EMPTY_ENTRY;
  }

  K key = inverseMap.remove(value);
  map.remove(key);
  RemovedKey<K, V> removedKey = new RemovedKey<>(key, value);
  return removedKey;

 }

 public boolean containsKey(Object key) {
  return map.containsKey(key);
 }

 public boolean containsValue(Object value) {
  return inverseMap.containsKey(value);
 }

 public boolean isEmpty() {
  return map.isEmpty();
 }

 public int size() {
  return map.size();
 }

 public static class RemovedKey<K, V> {
  K key;
  V value;

  @SuppressWarnings("rawtypes")
  public static final RemovedKey EMPTY_ENTRY = new RemovedKey();

  private RemovedKey() {
   key = null;
   value = null;
  }

  public RemovedKey(K key, V value) {
   this.key = key;
   this.value = value;
  }

  @SuppressWarnings("rawtypes")
  public static RemovedKey getEmptyEntry() {
   return EMPTY_ENTRY;
  }
 }

}

public class BijectiveMapTest {
 public static void main(String args[]) {
  BijectiveMap<Integer, String> bijMap = new BijectiveMap<>();

  bijMap.put(1, "Hari krishna");
  bijMap.put(2, "Sudhir");

  System.out.println("Value associated with key 1 is : " + bijMap.getValue(1));
  System.out.println("Value associated with key 2 is : " + bijMap.getValue(2));

  System.out.println("Key associated with value \"Hari krishna\" is : " + bijMap.getKey("Hari krishna"));
  System.out.println("Key associated with value \"Sudhir\" is : " + bijMap.getKey("Sudhir"));
 }

}


Output
Value associated with key 1 is : Hari krishna
Value associated with key 2 is : Sudhir
Key associated with value "Hari krishna" is : 1
Key associated with value "Sudhir" is : 2


You may like


Friday, 7 October 2016

Itext: ZapfDingbatsNumberList

ZapfDingbatsNumberList is a special-version of list which use zapfdingbats-numbers (1..10).

Go through following wiki link to know about Zapf_Dingbat numbers.

ZapfDingbatsNumberList provides following constructors to define ZapfDingbatsNumberList instance.

public ZapfDingbatsNumberList(int type)

public ZapfDingbatsNumberList(int type, int symbolIndent)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.ZapfDingbatsNumberList;
import com.itextpdf.text.pdf.PdfWriter;

public class ListEx {
 private static java.util.List<String> countries = Arrays.asList("Afghanistan", "Armenia", "Azerbaijan", "Bahrain",
   "Bangladesh", "Bhutan", "Brunei", "Cambodia", "China", "Cyprus", "Georgia", "India", "Indonesia", "Iran",
   "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", "Malaysia",
   "Maldives", "Mongolia", "Myanmar (Burma)", "Nepal", "North Korea", "Oman", "Pakistan", "Palestine",
   "Philippines", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Korea", "Sri Lanka", "Syria",
   "Taiwan", "Tajikistan", "Thailand", "Timor-Leste", "Turkey", "Turkmenistan", "United Arab Emirates (UAE)",
   "Uzbekistan", "Vietnam", "Yemen");

 public static void main(String args[]) throws FileNotFoundException, DocumentException {
  Document document = new Document();

  PdfWriter.getInstance(document, new FileOutputStream("countries.pdf"));

  List list = new ZapfDingbatsNumberList(2);

  Paragraph paragraph = new Paragraph("List Of Countries in Asia");

  for (String country : countries) {
   ListItem item = new ListItem(country);
   list.add(item);
  }

  document.open();
  document.add(paragraph);
  document.add(list);
  document.close();

 }
}



Previous                                                 Next                                                 Home

Tuesday, 4 October 2016

Itext: GreekList

GreekList is a special-version of list, which use greek-letters. . It extends List class.

GreekList class provides following constructors to define GreekList.

public GreekList()
public GreekList(int symbolIndent)
public GreekList(boolean greeklower, int symbolIndent)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.GreekList;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class ListEx {
 private static java.util.List<String> countries = Arrays.asList("Afghanistan", "Armenia", "Azerbaijan", "Bahrain",
   "Bangladesh", "Bhutan", "Brunei", "Cambodia", "China", "Cyprus", "Georgia", "India", "Indonesia", "Iran",
   "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", "Malaysia",
   "Maldives", "Mongolia", "Myanmar (Burma)", "Nepal", "North Korea", "Oman", "Pakistan", "Palestine",
   "Philippines", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Korea", "Sri Lanka", "Syria",
   "Taiwan", "Tajikistan", "Thailand", "Timor-Leste", "Turkey", "Turkmenistan", "United Arab Emirates (UAE)",
   "Uzbekistan", "Vietnam", "Yemen");

 public static void main(String args[]) throws FileNotFoundException, DocumentException {
  Document document = new Document();

  PdfWriter.getInstance(document, new FileOutputStream("countries.pdf"));

  List list = new GreekList();

  Paragraph paragraph = new Paragraph("List Of Countries in Asia");

  for (String country : countries) {
   ListItem item = new ListItem(country);
   list.add(item);
  }

  document.open();
  document.add(paragraph);
  document.add(list);
  document.close();

 }
}



Previous                                                 Next                                                 Home

Monday, 3 October 2016

Itext: RomanList

RomanList is a special-version of List, which use roman-letters. It extends List class.

RomanList class provides following constructors to define RomanList.

public RomanList()
public RomanList(int symbolIndent)
public RomanList(boolean lowercase, int symbolIndent)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.RomanList;
import com.itextpdf.text.pdf.PdfWriter;

public class ListEx {
 private static java.util.List<String> countries = Arrays.asList("Afghanistan", "Armenia", "Azerbaijan", "Bahrain",
   "Bangladesh", "Bhutan", "Brunei", "Cambodia", "China", "Cyprus", "Georgia", "India", "Indonesia", "Iran",
   "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", "Malaysia",
   "Maldives", "Mongolia", "Myanmar (Burma)", "Nepal", "North Korea", "Oman", "Pakistan", "Palestine",
   "Philippines", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Korea", "Sri Lanka", "Syria",
   "Taiwan", "Tajikistan", "Thailand", "Timor-Leste", "Turkey", "Turkmenistan", "United Arab Emirates (UAE)",
   "Uzbekistan", "Vietnam", "Yemen");

 public static void main(String args[]) throws FileNotFoundException, DocumentException {
  Document document = new Document();

  PdfWriter.getInstance(document, new FileOutputStream("countries.pdf"));

  List list = new RomanList();

  Paragraph paragraph = new Paragraph("List Of Countries in Asia");

  for (String country : countries) {
   ListItem item = new ListItem(country);
   list.add(item);
  }

  document.open();
  document.add(paragraph);
  document.add(list);
  document.close();

 }
}


Previous                                                 Next                                                 Home

Saturday, 1 October 2016

Itext: Set prepend and append data to symbol of list items

List class provides following methods to prepend and append some data to symbols of list items.

public void setPreSymbol(final String preSymbol)
Sets the String that has to be added before a number or letter in the list symbol.

public void setPostSymbol(final String postSymbol)
Sets the String that has to be added after a number or letter in the list symbol.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class ListEx {
  private static java.util.List<String> countries = Arrays.asList("Afghanistan", "Armenia", "Azerbaijan", "Bahrain",
      "Bangladesh", "Bhutan", "Brunei", "Cambodia", "China", "Cyprus", "Georgia", "India", "Indonesia", "Iran",
      "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", "Malaysia",
      "Maldives", "Mongolia", "Myanmar (Burma)", "Nepal", "North Korea", "Oman", "Pakistan", "Palestine",
      "Philippines", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Korea", "Sri Lanka", "Syria",
      "Taiwan", "Tajikistan", "Thailand", "Timor-Leste", "Turkey", "Turkmenistan", "United Arab Emirates (UAE)",
      "Uzbekistan", "Vietnam", "Yemen");

  public static void main(String args[]) throws FileNotFoundException, DocumentException {
    Document document = new Document();

    PdfWriter.getInstance(document, new FileOutputStream("countries.pdf"));

    List list = new List(List.ORDERED);

    Paragraph paragraph = new Paragraph("List Of Countries in Asia");

    list.setPreSymbol("Country ");
    list.setPostSymbol(":");

    for (String country : countries) {
      ListItem item = new ListItem(country);
      list.add(item);
    }

    document.open();
    document.add(paragraph);
    document.add(list);
    document.close();

  }
}




Previous                                                 Next                                                 Home

Itext: Set indentation of list item

By using 'setSymbolIndent' method, you can set the indentation between the symbol and the ListItem.

list.setSymbolIndent(36); // Half inch, 1 inch = 72
list.setAutoindent(false);

Above two statements sets the indentation of list item to half inch.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class ListEx {
 private static java.util.List<String> countries = Arrays.asList("Afghanistan", "Armenia", "Azerbaijan", "Bahrain",
   "Bangladesh", "Bhutan", "Brunei", "Cambodia", "China", "Cyprus", "Georgia", "India", "Indonesia", "Iran",
   "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", "Malaysia",
   "Maldives", "Mongolia", "Myanmar (Burma)", "Nepal", "North Korea", "Oman", "Pakistan", "Palestine",
   "Philippines", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Korea", "Sri Lanka", "Syria",
   "Taiwan", "Tajikistan", "Thailand", "Timor-Leste", "Turkey", "Turkmenistan", "United Arab Emirates (UAE)",
   "Uzbekistan", "Vietnam", "Yemen");

 private static java.util.List<Integer> countriesNumber = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 2, 3, 5,
   7, 11, 13, 17, 19, 23, 29, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 2, 3, 5,
   7, 11, 13, 17, 19, 23, 29);

 public static void main(String args[]) throws FileNotFoundException, DocumentException {
  Document document = new Document();

  PdfWriter.getInstance(document, new FileOutputStream("countries.pdf"));

  List list = new List(List.UNORDERED, List.ALPHABETICAL);

  Paragraph paragraph = new Paragraph("List Of Countries in Asia");

  list.setSymbolIndent(36); // Half inch, 1 inch = 72
  list.setAutoindent(false);

  int counter = 0;

  for (String country : countries) {
   ListItem item = new ListItem(country);
   item.setListSymbol(new Chunk(countriesNumber.get(counter++) + ""));
   list.add(item);
  }

  document.open();
  document.add(paragraph);
  document.add(list);
  document.close();

 }
}



Previous                                                 Next                                                 Home

Friday, 30 September 2016

Itext: Changing list symbol

Some times you may want to change the list item symbol. You can do this by using setListSymbol method of ListItem class.

public void setListSymbol(final Chunk symbol)

Following snippet adds custom symbol to the list item.

ListItem item = new ListItem(country);
item.setListSymbol(new Chunk(countriesNumber.get(counter++) +" . "));
list.add(item);

Following is the complete working application.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class ListEx {
 private static java.util.List<String> countries = Arrays.asList("Afghanistan", "Armenia", "Azerbaijan", "Bahrain",
   "Bangladesh", "Bhutan", "Brunei", "Cambodia", "China", "Cyprus", "Georgia", "India", "Indonesia", "Iran",
   "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", "Malaysia",
   "Maldives", "Mongolia", "Myanmar (Burma)", "Nepal", "North Korea", "Oman", "Pakistan", "Palestine",
   "Philippines", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Korea", "Sri Lanka", "Syria",
   "Taiwan", "Tajikistan", "Thailand", "Timor-Leste", "Turkey", "Turkmenistan", "United Arab Emirates (UAE)",
   "Uzbekistan", "Vietnam", "Yemen");

 private static java.util.List<Integer> countriesNumber = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 2, 3, 5,
   7, 11, 13, 17, 19, 23, 29, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 2, 3, 5,
   7, 11, 13, 17, 19, 23, 29);

 public static void main(String args[]) throws FileNotFoundException, DocumentException {
  Document document = new Document();

  PdfWriter.getInstance(document, new FileOutputStream("countries.pdf"));

  List list = new List(List.UNORDERED, List.ALPHABETICAL);

  Paragraph paragraph = new Paragraph("List Of Countries in Asia");

  int counter = 0;

  for (String country : countries) {
   ListItem item = new ListItem(country);
   item.setListSymbol(new Chunk(countriesNumber.get(counter++) + " . "));
   list.add(item);
  }

  document.open();
  document.add(paragraph);
  document.add(list);
  document.close();

 }
}




Previous                                                 Next                                                 Home