Posts

Overloading Cases

package com.sk.demo; class Overloading{ public static void main(String[] args){ A a = new A(); a.m1("String"); // string type method will call a.m1(new Object()); // Object type method will call a.m1(new A()); // Object type method will call a.m1(null); // string type method will call since it is applicable in child level also then no need to go for parent level System.out.println("------------ For B class ------------"); B b = new B(); b.m1(10); b.m1(new Object()); b.m1(null); // since null not applicable for int type then  System.out.println("-------------- For C class -------------"); C c = new C(); c.m1("Hello"); c.m1(new StringBuffer("Hi")); c.m1(new Object()); /* error: reference to m1 is ambiguous                 c.m1(null);                  ^ both method m1(String) in C and...

Mutable and Immutable

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Test2{ public static void main(String[] args) { List<Mutbl> imlist1 = new ArrayList<Mutbl>(); List<Mutbl> imlist2 = new ArrayList<Mutbl>(); // immuatble System.out.println("------------------IMMUTABLE EXAMPLE-----------------------"); Mutbl imm1 = new Mutbl("P1", "S1"); Mutbl imm2 = new Mutbl("P2", "S2"); imlist1.add(imm1); imlist1.add(imm2); System.out.println(Arrays.toString(imlist1.toArray())); imm1.setP1("PP1"); imm1.setP2("SS1"); imm2.setP1("PP2"); imm2.setP2("SS2"); imlist2.add(imm1); imlist2.add(imm2); System.out.println(Arrays.toString(imlist2.toArray())); System.out.println("value of list 1 changes indirectly due to immutabality"); System.out.println(Arrays.toString(imlist1.toArra...

Tree map sort by key and sort by value

package com.emiza.utility; import java.util.Comparator; import java.util.Map; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; public class Test { public static void main(String[] args) { Map<Integer, String> map = new TreeMap<Integer, String>(); map.put(1, "one"); map.put(3, "athree"); map.put(2, "two"); // prints one two three  for(Integer key : map.keySet()) {     System.out.println(map.get(key)); } System.out.println(entriesSortedByValues(map)); } static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {     SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(         new Comparator<Map.Entry<K,V>>() {             @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,...

DES Encryption and Decryption

import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import org.apache.commons.codec.binary.Base64; public class EncryptDES { private static final String UNICODE_FORMAT = "UTF8"; public static final String DESEDE_ENCRYPTION_SCHEME = "DESede"; private KeySpec ks; private SecretKeyFactory skf; private Cipher cipher; byte[] arrayBytes; SecretKey key; public EncryptDES() { try { String myEncryptionKey = "ThisIsAEncryptionPublicKey"; byte[] arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); ks = new DESedeKeySpec(arrayBytes); skf = SecretKeyFactory.getInstance(DESEDE_ENCRYPTION_SCHEME); cipher = Cipher.getInstance(DESEDE_ENCRYPTION_SCHEME); key = skf.generateSecret(ks); } catch (Exception e) { e.printStackTrace(); } } public String encrypt(String unencryptedString) { S...

Sub-List and Comma Separated String

Getting Sub-list from list and converting  List into comma separated String values List<String> l = new ArrayList<String>();  // Getting sublist from main list from-to index System.out.println(l.subList(2, l.size())); // including, excluding // Converting list into comma seperated values String d = String.join(",", l); System.out.println(d);