Posts

Showing posts from August, 2019

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);