Monday, January 12, 2015

Java Using Array Program



Write a program to remove duplicate entries from an array

package com.java2novice.treeset;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
public class MyArrayDuplicates
{
public static void main(String a[])
{
        String[] strArr = {"one","two","three","four","four","five"};
        List tmpList = Arrays.asList(strArr);
        TreeSet unique = new TreeSet(tmpList);
        System.out.println(unique);
    }
}

Write a program to find duplicate value from an array

package com.java2novice.treeset;
import java.util.TreeSet;
public class MyDuplicateEntry
{
public static void main(String a[])
{
        String[] strArr = {"one","two","three","four","four","five"};
        TreeSet unique = new TreeSet();
        for(String str:strArr)
           {
            if(!unique.add(str))
            {
                System.out.println("Duplicate Entry is: "+str);
            }
        }
    }
}

How to create a TreeSet with comparator

package com.java2novice.treeset;
import java.util.Comparator;
import java.util.TreeSet;
public class MySetWithCompr
{
public static void main(String a[])
{
       TreeSet ts = new TreeSet(new MyComp());
        ts.add("Red");
        ts.add("Orange");
        ts.add("Blue");
        ts.add("Green");
        System.out.println(ts);
    }
}
class MyComp implements Comparator
{
           public int compare(String str1, String str2)
          {
        return str1.compareTo(str2);
    }
       }

Create TreeSet with comparator by user define objects

package com.java2novice.treeset;
import java.util.Comparator;
import java.util.TreeSet; 
public class MyCompUserDefine
{
 public static void main(String a[])
 {
        TreeSet nameComp = new TreeSet(new                           MyNameComp());
        nameComp.add(new Empl("Ram",3000));
        nameComp.add(new Empl("John",6000));
        nameComp.add(new Empl("Crish",2000));
        nameComp.add(new Empl("Tom",2400));
        for(Empl e:nameComp)
        {
            System.out.println(e);
        }
        System.out.println("===========================");
        TreeSet salComp = new TreeSet(new MySalaryComp());
        salComp.add(new Empl("Ram",3000));
        salComp.add(new Empl("John",6000));
        salComp.add(new Empl("Crish",2000));
        salComp.add(new Empl("Tom",2400));
        for(Empl e:salComp)
        {
            System.out.println(e);
        }
    }
}
class MyNameComp implements Comparator
{
 public int compare(Empl e1, Empl e2)
{
 return e1.getName().compareTo(e2.getName());
    }
}  
class MySalaryComp implements Comparator
{
           public int compare(Empl e1, Empl e2)
          {
        if(e1.getSalary() > e2.getSalary())
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}
class Empl
{
    private String name;
    private int salary;
public Empl(String n, int s)
{
        this.name = n;
        this.salary = s;
  }
public String getName()
 {
        return name;
    }
 public void setName(String name)
 {
        this.name = name;
    }
  public int getSalary()
  {
        return salary;
    }
  public void setSalary(int salary)
  {
        this.salary = salary;
    }
  public String toString()
  {
        return "Name: "+this.name+"-- Salary: "+this.salary;
    }
}

No comments:

Post a Comment