Monday, June 10, 2013

getCurrentDate in Java

    public String getCurrentDate(String format) {
        String date = "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        date = sdf.format(new Date());
        return date;
    }

RemDupFromList.java

RemDupFromList.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package java4s;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
public class RemDupFromList {
    public static void main(String[] args)
    {
        List li = new ArrayList();
              li.add("one");
              li.add("two");
              li.add("three");
              li.add("one");//Duplicate
              li.add("one");//Duplicate
             // We have facility to pass a List into Set constructor and vice verse to cast     
                List li2 = new ArrayList(new HashSet(li)); //no order
             // List li2 = new ArrayList(new LinkedHashSet(li)); //If you need to preserve the order use 'LinkedHashSet'
             Iterator it= li2.iterator();
             while(it.hasNext())
             {
                 System.out.println(it.next());
             }
    }
}

Explanation

  • Take your normal List object
  • Pass that List li object to Set [Line number 22]  => So finally we have Set object in our hand, just pass this current Set object as argument to ArrayList, so we got new List object li2 without duplicate
  • But if you would like to preserve the order of data use LinkedHashSet rather HashSet

How to find duplicate values in ArrayList with out iterarting..?

  1. List list = new ArrayList();  
  2.     list.add("a");  
  3.     list.add("a");  
  4.     list.add("b");  
  5.     list.add("c");  
  6.     list.add("c");   
  7.     System.out.println(list);  
  8.     Set s = new HashSet(list);  
  9.     System.out.println("Set :- " +s); 

findDuplicates in a list and return them

public Set<Integer> findDuplicates(List<Integer> listContainingDuplicates)
{ 
  final Set<Integer> setToReturn = new HashSet(); 
  final Set<Integer> set1 = new HashSet();

  for (Integer yourInt : listContainingDuplicates)
  {
   if (!set1.add(yourInt))
   {
    setToReturn.add(yourInt);
   }
  }
  return setToReturn;
}
 
 have a List of type Integer eg:

[1, 1, 2, 3, 3, 3]

I would like a method to return all the duplicates eg:
[1, 3]
 
 ----------------
 
List<Item> list = ...;
list.removeAll(new HashSet<Item>(list));
return new HashSet<Item>(list); 
  ----------------
public static Set<Integer> findDuplicates(List<Integer> input) {
    List<Integer> copy = new ArrayList<Integer>(input);
    for (Integer value : new HashSet<Integer>(input)) {
        copy.remove(value);
    }
    return new HashSet<Integer>(copy );
} 
 ----------------
public static void main(String[] args) {
        List<Integer> list = new LinkedList<Integer>();
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(3);
        Map<Integer,Integer> map = new HashMap<Integer, Integer>();
        for (Integer x : list) { 
            Integer val = map.get(x);
            if (val == null) { 
                map.put(x,1);
            } else {
                map.remove(x);
                map.put(x,val+1);
            }
        }
        List<Integer> result = new LinkedList<Integer>();
        for (Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() > 1) {
                result.add(entry.getKey());
            }
        }
        for (Integer x : result) { 
            System.out.println(x);
        }

    } 
 ----------------
public void testFindDuplicates() {

    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(3);
    list.add(3);

    Set<Integer> result = new HashSet<Integer>();
    int currentIndex = 0;
    for (Integer i : list) {
        if (!result.contains(i) && list.subList(currentIndex + 1, list.size()).contains(i)) {
            result.add(i);
        }
        currentIndex++;
    }
    assertEquals(2, result.size());
    assertTrue(result.contains(1));
    assertTrue(result.contains(3));
}
-----------
ublic List<int> GetDuplicates(int max)
{   
    //allocate and clear memory to 0/false
    bit[] buckets=new bit[max]
    memcpy(buckets,0,max);
    //find duplicates
    List<int> result=new List<int>();
    foreach(int val in List)
    {
        if (buckets[val])
        {
            result.add(value);
        }
        else
        {
            buckets[val]=1;
        }
    }
    return  result
}  
----------
private <T> Set<T> findDuplicates(Collection<T> list) {

    Set<T> duplicates = new LinkedHashSet<T>();
    Set<T> uniques = new HashSet<T>();

    for(T t : list) {
        if(!uniques.add(t)) {
            duplicates.add(t);
        }
    }

    return duplicates;
}
-------
 
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
 
public class CrunchifyFindDuplicateInList {
 
    /**
     * @author Crunchify.com
     */
 
    public static void main(String[] args) {
        List<String> list = new LinkedList<String>();
        for (int i = 0; i < 10; i++) {
            list.add(String.valueOf(i));
        }
        for (int i = 0; i < 5; i++) {
            list.add(String.valueOf(i));
        }
 
        System.out.println("My List : " + list);
        System.out.println("\nHere are the duplicate elements from list : " + findDuplicates(list));
    }
 
    public static Set<String> findDuplicates(List<String> listContainingDuplicates) {
 
        final Set<String> setToReturn = new HashSet<String>();
        final Set<String> set1 = new HashSet<String>();
 
        for (String yourInt : listContainingDuplicates) {
            if (!set1.add(yourInt)) {
                setToReturn.add(yourInt);
            }
        }
        return setToReturn;
    }
}
Output
1
2
3
My List : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
 
Here are the duplicate elements from list : [3, 2, 1, 0, 4]
 
  
CREATE OR REPLACE procedure insert_auw_into_temp() is
cursor air_cur is select distinct temp.reg_no,air.max_allup_wt from tc_temp_log temp,tc_aircraft_mt air where temp.max_allup_wt is null and trim(temp.reg_no)=trim(air.reg_no);
   v_temp air_cur%rowtype;
begin
   open air_cur;
   loop
      fetch air_cur into v_temp;
      exit when air_cur%notfound;
      update tc_temp_log set max_allup_wt=v_temp.max_allup_wt where trim(reg_no)=trim(v_temp.reg_no);
   end loop;
   close air_cur;
end;
/

get_nextcode

/* Formatted on 2013/06/10 17:43 (Formatter Plus v4.8.8) */
CREATE OR REPLACE FUNCTION get_nextcode (
   p_table_name   VARCHAR,
   p_col_name     VARCHAR,
   p_length       NUMBER
)
   RETURN VARCHAR
IS
   v_query      VARCHAR (200);
   v_autocode   VARCHAR (50);
BEGIN
   v_query :=
         'select lpad(nvl(max(to_number('
      || p_col_name
      || '))+1,1),'
      || p_length
      || ',0) code from '
      || p_table_name;

   EXECUTE IMMEDIATE v_query
                INTO v_autocode;

   RETURN v_autocode;
END;
/

gettimeslot

/* Formatted on 2013/06/10 17:42 (Formatter Plus v4.8.8) */
CREATE OR REPLACE FUNCTION vabb.gettimeslot (vtime IN VARCHAR2)
   RETURN VARCHAR2
IS
   RESULT   VARCHAR2 (10);
BEGIN
   IF (vtime >= '0000' AND vtime <= '0100')
   THEN
      RESULT := '0531-0630';
   ELSIF (vtime >= '0101' AND vtime <= '0200')
   THEN
      RESULT := '0631-0730';
   ELSIF (vtime >= '0201' AND vtime <= '0300')
   THEN
      RESULT := '0731-0830';
   ELSIF (vtime >= '0301' AND vtime <= '0400')
   THEN
      RESULT := '0831-0930';
   ELSIF (vtime >= '0401' AND vtime <= '0500')
   THEN
      RESULT := '0931-1030';
   ELSIF (vtime >= '0501' AND vtime <= '0600')
   THEN
      RESULT := '1031-1130';
   ELSIF (vtime >= '0601' AND vtime <= '0700')
   THEN
      RESULT := '1131-1230';
   ELSIF (vtime >= '0701' AND vtime <= '0800')
   THEN
      RESULT := '1231-1330';
   ELSIF (vtime >= '0801' AND vtime <= '0900')
   THEN
      RESULT := '1331-1430';
   ELSIF (vtime >= '0901' AND vtime <= '1000')
   THEN
      RESULT := '1431-1530';
   ELSIF (vtime >= '1001' AND vtime <= '1100')
   THEN
      RESULT := '1531-1630';
   ELSIF (vtime >= '1101' AND vtime <= '1200')
   THEN
      RESULT := '1631-1730';
   ELSIF (vtime >= '1201' AND vtime <= '1300')
   THEN
      RESULT := '1731-1830';
   ELSIF (vtime >= '1301' AND vtime <= '1400')
   THEN
      RESULT := '1831-1930';
   ELSIF (vtime >= '1401' AND vtime <= '1500')
   THEN
      RESULT := '1931-2030';
   ELSIF (vtime >= '1501' AND vtime <= '1600')
   THEN
      RESULT := '2031-2130';
   ELSIF (vtime >= '1601' AND vtime <= '1700')
   THEN
      RESULT := '2131-2230';
   ELSIF (vtime >= '1701' AND vtime <= '1800')
   THEN
      RESULT := '2231-2330';
   ELSIF (vtime >= '1801' AND vtime <= '1900')
   THEN
      RESULT := '2331-0030';
   ELSIF (vtime >= '1901' AND vtime <= '2000')
   THEN
      RESULT := '0031-0130';
   ELSIF (vtime >= '2001' AND vtime <= '2100')
   THEN
      RESULT := '0131-0230';
   ELSIF (vtime >= '2101' AND vtime <= '2200')
   THEN
      RESULT := '0231-0330';
   ELSIF (vtime >= '2201' AND vtime <= '2300')
   THEN
      RESULT := '0331-0430';
   ELSE
      RESULT := '0431-0530';
   END IF;

   RETURN (RESULT);
END gettimeslot;
/