Tuesday, January 26, 2016

Java Function Programming - Predicate

As java 8 introduces the predicate functions, but we may wonder where to use those true/false returning function in our daily programming life. The answer is we can use this in the following scenario:
1. when we have to evaluate the objects from the same class which will result in the true or false.
2. to filter the objects by using some of its variables etc.

lets hack Predicate with some examples:

A. Examining if the object is the intance of the particular class or not: 

/** * Created by yubraj on 1/26/16. */

public class MyClass {
    Predicate<MyClass> predicate1 = (MyClass myclass) -> this.equals(myclass);
    Predicate<MyClass> predicate2 =  this::equals;
    String text1 = "yubraj", text2 = "pokharel";
    public MyClass(String text1, String text2) {
        this.text1 = text1;        this.text2 = text2;    }

    @Override    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyClass myClass = (MyClass) o;
        if (!text1.equals(myClass.text1)) return false;
        return text2.equals(myClass.text2);
    }

    @Override    public int hashCode() {
        int result = text1.hashCode();      
        result = 31 * result + text2.hashCode();
        return result;    }

    public void myMethod(MyClass cl){
        System.out.println("Intance equals : " + predicate2.test(cl));    }

    public static void main(String[] args) {
        MyClass mc = new MyClass("sudhan", "pokharel");
        MyClass mc2 = new MyClass("yubraj", "pokharel");
        mc.myMethod(mc);
        System.out.println("-----------------------------------");
        mc2.myMethod(mc2);
    }

}

Output:
true
true
//because both are the intance of the similar object.



B. Filtering the objects

for male employee who are older then 21 years

lets add this in the Employee Class

public static Predicate<Employee> isAdultMale() {
    return p -> p.getAge() > 21 && p.getGender().equalsIgnoreCase("M");
}

then we can access all the employee who are Male and also more then 21 years by passing method as the variable eg:
public static void main(String[] args){
        Employee e1 = new Employee(1,23,"M","Rick","Beethovan");
        Employee e2 = new Employee(2,13,"F","Martina","Hengis");
        Employee e3 = new Employee(3,43,"M","Ricky","Martin");
        Employee e4 = new Employee(4,26,"M","Jon","Lowman");
        Employee e5 = new Employee(5,19,"F","Cristine","Maria");
        Employee e6 = new Employee(6,15,"M","David","Feezor");
        Employee e7 = new Employee(7,68,"F","Melissa","Roy");
        Employee e8 = new Employee(8,79,"M","Alex","Gussin");
        Employee e9 = new Employee(9,15,"F","Neetu","Singh");
        Employee e10 = new Employee(10,45,"M","Naveen","Jain");
         
        List<Employee> employees = new ArrayList<Employee>();
        employees.addAll(Arrays.asList(new Employee[]{e1,e2,e3,e4,e5,e6,e7,e8,e9,e10}));
                
        System.out.println(filterEmployees(employees, isAdultMale()));
}

Happy Coding :)

References: howtodoinjava, oracle

Wednesday, October 28, 2015

LinkedList In java Example

import java.util.LinkedList;

public class MyFirstLinkedList{

public static void main(String args[]){
System.out.println("I am at the starting point Yuppie");
System.out.println("---------------------------------");

LinkedList<String> ll = new LinkedList<String>();
ll.add("Nepal");
ll.add("Canada");

System.out.println("New LinkedList : " + ll);
ll.addFirst("Usa");
ll.add(1, "Iran");
ll.addLast("Japan");

System.out.println("New LinkedList afer adding items : " + ll);

ll.addFirst("Nepal");
ll.set(4, "Pakistan");
ll.addLast("Turkey");
ll.addLast("Turkey");
ll.addLast("Egypt");
ll.addLast("Egypt");
ll.addLast("Qatar");

System.out.println("New LinkedList updated [0] : " + ll);

if(ll.contains("Nepal")){
System.out.print("Yuppie there is Nepal");
}

Object name = ll.get(0);
System.out.println("Taken Object Name is : " + name);
System.out.println("Directly taken name is : " + ll.get(3));

System.out.println("Index of Egypt: " + ll.indexOf("moti"));
System.out.println("LastIndex of Egypt : " + ll.lastIndexOf("moti"));

ll.removeFirstOccurrence("Turkey");
System.out.println("List updated [1] : " + ll);

ll.poll();
System.out.println("List updated [2] : " + ll);

ll.pollFirst();
System.out.println("List updated [3] : " + ll);

ll.removeLastOccurrence("Turkey");
System.out.println("List updated [4]: " + ll);

}

}

Output ::

I am at the starting point Yuppie
---------------------------------
New LinkedList : [Nepal, Canada]
New LinkedList afer adding items : [Usa, Iran, Nepal, Canada, Japan]
New LinkedList updated [0] : [Nepal, Usa, Iran, Nepal, Pakistan, Japan, Turkey, Turkey, Egypt, Egypt, Qatar]
Yuppie there is NepalTaken Object Name is : Nepal
Directly taken name is : Nepal
Index of Egypt: 8
LastIndex of Egypt : 9
List updated [1] : [Nepal, Usa, Iran, Nepal, Pakistan, Japan, Turkey, Egypt, Egypt, Qatar]
List updated [2] : [Usa, Iran, Nepal, Pakistan, Japan, Turkey, Egypt, Egypt, Qatar]
List updated [3] : [Iran, Nepal, Pakistan, Japan, Turkey, Egypt, Egypt, Qatar]
List updated [4]: [Iran, Nepal, Pakistan, Japan, Egypt, Egypt, Qatar]


Tuesday, May 26, 2015

Stanton measure

1. The Stanton measure of an array is computed as follows. Count the number of 1s in the array. Let this count be n. The Stanton measure is the number of times that n appears in the array. For example, the Stanton measure of {1, 4, 3, 2, 1, 2, 3, 2} is 3 because 1 occurs 2 times in the array and 2 occurs 3 times.

Write a function named stantonMeasure that returns the Stanton measure of its array argument.

If you are  programming in Java or C#, the function prototype is
int stantonMeasure(int[ ] a)

If you are programming in C++ or C, the function prototype is
int stantonMeasure(int a[ ], int len) where len is the number of elements in the array.

Examples

if a is
return
reason
{1}
1
1 occurs 1 time, 1 occurs 1 time
{0}
1
1 occurs 0 times, 0 occurs 1 time
{3, 1, 1, 4}
0
1 occurs 2 times, 2 occurs 0 times
{1, 3, 1, 1, 3, 3, 2, 3, 3, 3, 4}
6
1 occurs 3 times, 3 occurs 6 times
{}
0
1 occurs 0 times, 0 occurs 0 times
Solution:

public static int stantonMeasure(int[] a){
int one_counter = 0, stat_counter = 0;

for(int i=0; i<a.length;i++){
if(a[i] == 1) one_counter++;
}

for(int j=0; j<a.length; j++){
if(a[j] == one_counter) stat_counter++;
}

return stat_counter;

}