Saturday, 14 November 2015

How to remove duplicates from ArrayList


In Java, List permits ordered access of their elements. They can have duplicates because their lookup key is the position not some hash code, every element can be modified while they remain in the list where as Set represents a collection of unique elements and while elements are in set, they must not be modified.While there is no restriction preventing you from modifying elements in a set, if an element is modified, then it could become forever lost in the set.

In this problem, to remove duplicates from an ArrayList, you can store the element of list in the Hashset . You can iterate over Hashset or you can convert the Hashset into new ArrayList.

Sample Program:-

 import java.util.ArrayList;  
 import java.util.HashSet;  
 import java.util.List;  
 import java.util.Set;  
 public class RemoveDuplicates {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           List<String> l = new ArrayList<String>();  
           l.add("A");  
           l.add("B");  
           l.add("C");  
           l.add("A");  
           System.out.println("Before removing duplicates: ");  
           for (String s : l) {  
                System.out.println(s);  
           }  
           Set<String> set = new HashSet<String>(l);  
           List<String> newlist = new ArrayList<String>(set);  
           System.out.println("after removing duplicates: ");  
           for (String s : newlist) {  
                System.out.println(s);  
           }  
      }  
 }  


Output:-

 Before removing duplicates:   
 A  
 B  
 C  
 A  
 after removing duplicates:   
 A  
 B  
 C  


Enjoy Programming.

Friday, 13 November 2015

Write a program to find average of consecutive N Odd numbers and Even numbers



Sample Program:-
 public class AvgEvenOddNumbers {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int n = 100;  
           int evenCount = 0, oddCount = 0, evenSum = 0, oddSum = 0;  
           while (n > 0) {  
                if (n % 2 == 0) {  
                     evenCount++;  
                     evenSum = evenSum + n;  
                } else {  
                     oddCount++;  
                     oddSum = oddSum + n;  
                }  
                n--;  
           }  
           int avgEven, avgOdd;  
           avgEven = evenSum / evenCount;  
           avgOdd = oddSum / oddCount;  
           System.out.println("Average of Even no till 100 is :- " + avgEven);  
           System.out.println("Average of Odd no till 100 is :- " + avgOdd);  
      }  
 }  

Output:-
 Average of Even no till 100 is :- 51  
 Average of Odd no till 100 is :- 50  

Enjoy Programming.

How to obtain Array From an ArrayList ?


The Collection interface includes the toArray() method to convert a new collection into an array. There are two forms of this method. The no argument version will return the elements of the collection in an Object array: public Object[ ] toArray(). The returned array cannot cast to any other data type. This is the simplest version. The second version requires you to pass in the data type of the array you’d like to return: public Object [ ] toArray(Object type[ ]).

For example:- assume col represents a collection of Date objects,

Date d[ ] = (Date []) col.toArray(new Date[0]);

To convert ArrayList into an Array, first method is sufficient.


Sample Program:-


 import java.util.ArrayList;  
 import java.util.List;  
 public class ConvertArrayListIntoArray {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           List<String> l=new ArrayList<String>();  
           l.add("A");  
           l.add("B");  
           l.add("C");  
           Object arr[]=l.toArray();  
           for(Object a:arr)  
           {  
                String str=(String)a;  
                System.out.println(str);  
           }  
      }  
 }  

Output:-

 A  
 B  
 C  



Enjoy Reading