Wednesday, March 18, 2015

Get Size of Java TreeSet Example

  1. /*
  2.   Get Size of Java TreeSet Example
  3.   This Java Example shows how to get the size or nubmer of elements stored in
  4.   Java TreeSet object using size method.
  5. */
  6.  
  7. import java.util.TreeSet;
  8.  
  9. public class GetSizeOfJavaTreeSetExample{
  10.  
  11.   public static void main(String[] args) {
  12.    
  13.     //create TreeSet object
  14.     TreeSet tSet = new TreeSet();
  15.    
  16.     /*
  17.       To get the size of TreeSet use
  18.       int size() method of TreeSet class. It returns the number of elements
  19.       stored in TreeSet object.    
  20.     */
  21.     System.out.println("Size of TreeSet : " + tSet.size());
  22.    
  23.     //add elements to TreeSet object
  24.     tSet.add(new Integer("1"));
  25.     tSet.add(new Integer("2"));
  26.     tSet.add(new Integer("3"));
  27.  
  28.     System.out.println("Size of TreeSet after addition : " + tSet.size());
  29.    
  30.     //remove one element from TreeSet using remove method
  31.     tSet.remove(new Integer("1"));
  32.     System.out.println("Size of TreeSet after removal : " + tSet.size());
  33.   }
  34. }
  35.  
  36. /*
  37. Output would be
  38. Size of TreeSet : 0
  39. Size of TreeSet after addition : 3
  40. Size of TreeSet after removal : 2
  41. */

Get Size of Java HashSet Example

  1. /*
  2.   Get Size of Java HashSet Example
  3.   This Java Example shows how to get the size or nubmer of elements stored in
  4.   Java HashSet object using size method.
  5. */
  6.  
  7. import java.util.HashSet;
  8.  
  9. public class GetSizeOfJavaHashSetExample{
  10.  
  11.   public static void main(String[] args) {
  12.    
  13.     //create HashSet object
  14.     HashSet hSet = new HashSet();
  15.    
  16.     /*
  17.       To get the size of HashSet use
  18.       int size() method of HashSet class. It returns the number of elements
  19.       stored in HashSet object.    
  20.     */
  21.     System.out.println("Size of HashSet : " + hSet.size());
  22.    
  23.     //add elements to HashSet object
  24.     hSet.add(new Integer("1"));
  25.     hSet.add(new Integer("2"));
  26.     hSet.add(new Integer("3"));
  27.  
  28.     System.out.println("Size of HashSet after addition : " + hSet.size());
  29.    
  30.     //remove one element from HashSet using remove method
  31.     hSet.remove(new Integer("1"));
  32.     System.out.println("Size of HashSet after removal : " + hSet.size());
  33.   }
  34. }
  35.  
  36. /*
  37. Output would be
  38. Size of HashSet : 0
  39. Size of HashSet after addition : 3
  40. Size of HashSet after removal : 2
  41. */

Get Size of Java LinkedHashSet Example

  1. /*
  2.   Get Size of Java LinkedHashSet Example
  3.   This Java Example shows how to get the size or nubmer of elements stored in
  4.   Java LinkedHashSet object using size method.
  5. */
  6.  
  7. import java.util.LinkedHashSet;
  8.  
  9. public class GetSizeOfJavaLinkedHashSetExample{
  10.  
  11.   public static void main(String[] args) {
  12.  
  13.     //create LinkedHashSet object
  14.     LinkedHashSet lhashSet = new LinkedHashSet();
  15.  
  16.     /*
  17.       To get the size of LinkedHashSet use
  18.       int size() method of LinkedHashSet class. It returns the number of elements
  19.       stored in LinkedHashSet object.    
  20.     */
  21.     System.out.println("Size of LinkedHashSet : " + lhashSet.size());
  22.  
  23.     //add elements to LinkedHashSet object
  24.     lhashSet.add(new Integer("1"));
  25.     lhashSet.add(new Integer("2"));
  26.     lhashSet.add(new Integer("3"));
  27.  
  28.     System.out.println("Size of LinkedHashSet after addition : " + lhashSet.size());
  29.  
  30.     //remove one element from LinkedHashSet using remove method
  31.     lhashSet.remove(new Integer("1"));
  32.     System.out.println("Size of LinkedHashSet after removal : " + lhashSet.size());
  33.   }
  34. }
  35.  
  36. /*
  37. Output would be
  38. Size of LinkedHashSet : 0
  39. Size of LinkedHashSet after addition : 3
  40. Size of LinkedHashSet after removal : 2
  41. */

Get Week of month and year using Java Calendar

  1. /*
  2.   Get Week of month and year using Java Calendar
  3.   This example shows how to get current week of month and curent week of year  
  4.   using Java Calendar class.
  5. */
  6.  
  7. import java.util.Calendar;
  8.  
  9. public class GetWeekOfMonthAndYear {
  10.  
  11.   public static void main(String[] args) {
  12.  
  13.     //create Calendar instance
  14.     Calendar now = Calendar.getInstance();
  15.    
  16.     System.out.println("Current week of month is : " +
  17.                 now.get(Calendar.WEEK_OF_MONTH));
  18.                
  19.     System.out.println("Current week of year is : " +
  20.                 now.get(Calendar.WEEK_OF_YEAR));
  21.  
  22.     now.add(Calendar.WEEK_OF_MONTH1);
  23.     System.out.println("date after one year : " + (now.get(Calendar.MONTH) + 1)
  24.                         + "-"
  25.                         + now.get(Calendar.DATE)
  26.                         + "-"
  27.                         + now.get(Calendar.YEAR));
  28.  
  29.    
  30.   }
  31. }
  32.  
  33. /*
  34. Typical output would be
  35. Current week of month is : 5
  36. Current week of year is : 52
  37. */

Swap elements of Java ArrayList example

  1. /*
  2.   Swap elements of Java ArrayList example
  3.   This java example shows how to swap elements of Java ArrayList object using
  4.   swap method of Collections class.
  5. */
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9.  
  10. public class SwapElementsOfArrayListExample {
  11.  
  12.   public static void main(String[] args) {
  13.    
  14.     //create an ArrayList object
  15.     ArrayList arrayList = new ArrayList();
  16.    
  17.     //Add elements to Arraylist
  18.     arrayList.add("A");
  19.     arrayList.add("B");
  20.     arrayList.add("C");
  21.     arrayList.add("D");
  22.     arrayList.add("E");
  23.    
  24.     System.out.println("Before swaping, ArrayList contains : " + arrayList);
  25.    
  26.     /*
  27.       To swap elements of Java ArrayList use,
  28.       static void swap(List list, int firstElement, int secondElement)
  29.       method of Collections class. Where firstElement is the index of first
  30.       element to be swapped and secondElement is the index of the second element
  31.       to be swapped.
  32.      
  33.       If the specified positions are equal, list remains unchanged.
  34.      
  35.       Please note that, this method can throw IndexOutOfBoundsException if
  36.       any of the index values is not in range.
  37.     */
  38.    
  39.     Collections.swap(arrayList,0,4);
  40.    
  41.     System.out.println("After swaping, ArrayList contains : " + arrayList);
  42.    
  43.   }
  44. }
  45.  
  46. /*
  47. Output would be
  48. Before swaping, ArrayList contains : [A, B, C, D, E]
  49. After swaping, ArrayList contains : [E, B, C, D, A]
  50. */