Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 18 September 2016

C# Vs Java

My experience with C# after working with Java for almost 5 years is :

1. Until Java 1.8, most of advanced features available in other languages such as C# were missing.

C# features that stand out because of coding experience -
1. Lambda/LINQ (Available in Java 1.8)
2. Extension Methods ( Its awesome feature where existing classes which can not be accessed because they may belong to .Net library/third party library, can be introduced with additional new methods, without touching its code. Surprised, why java did not have this feature. Coming from Java background, makes me little strange, even sealed classes are allowed to be extended in some way through Extension Methods)

Example :
static class Extension
{

     public static String substr(this String str, int startIndex, int lastIndex)
    {
          return str.substr(startIndex,lastIndex)
     }
}

3. Detecting DataType during compilation time using var in C#
4. optional parameter by setting default value to certain parameter in C#
5. Properties for getter/setter ; Example = public string Name {get;set;}. Here get/set can be controlled. If set is not defined, then Name becomes readonly. No need for methods like GetName(), SetName(String name);
6. out parameter
7. By default methods can not be overridden (Non Virtual). Explicit use of virtual/overridden to make method overridden. [I don't feel this is good feature. By default methods should be overridden, if a class is inherited. Java does not have concept of keywords virtual which will then allow method to be overridden, though annotation @override is possible for sake of clarity ]
8. nullable datatype ;
example - int ? x = null; provides property Value to access value. Also, HasValue will let one know, if x is assigned to some value or not assigned.


I really find coding experience in C# much better than Java. Having said that, I also like to make a point, languages that come later always know the shortcomings and come with its handling to overcome known limitation/shortcomings.
That is the case with C#, which is inspired by Java and took birth to compete with Java.
On other side, Java is opensource, very much popular, has large community, technology stack is hard to beat. Most of applications are built upon Java. Next thing is unbeatable - It's platform support/cross platform. Write once, run on many different Operating Systems.

Saturday, 6 December 2014

Traversing Linked List In Reverse Order

Traversing Linked List In Reverse Order:

package linkedList;

import java.util.List;

public interface LinkedListInterface<T>
{
void add(T t);
List<T> readNodeValues();
List<T> readNodeValuesInReverseOrder();
}


/********************************************/

package linkedList;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractLinkedListImpl<T> implements LinkedListInterface<T>
{
protected Node start = null;
static class Node
{
private Node next = null;
private Object value;

}

@Override
public void add(T t) {
Node n = new Node();
n.value = t;
if(this.start == null)
{
this.start = n;
}
else
{
//create temp node to point to start
Node temp = this.start;
//traverse to end of linked list whre next = null
while(temp.next !=  null)
{
temp = temp.next;
}
temp.next = n;
}
}

@Override
public List<T> readNodeValues() {
List<T> list = null;
while(this.start!=null)
{
list = new ArrayList<T>();
list.add((T)start.value);
this.start=this.start.next;
}

return list;
}

@Override
public List<T> readNodeValuesInReverseOrder() {

List<T> list = null;
traverse(this.start);
return list;
}

private void traverse(Node start)
{

if(start == null)
{
return;
}
traverse(start.next);
System.out.println(start.value);


}

}

/********************************/
package linkedList;

public class MyLinkedList<T> extends AbstractLinkedListImpl<T>
{

public MyLinkedList()
{
this.start = null;
}

public static void main(String [] str)
{
MyLinkedList<String> linkedList = new MyLinkedList<String>();
linkedList.add(new String("Debasish"));
linkedList.add(new String ("Subhasish"));
linkedList.add(new String ("Krishna"));
linkedList.add(new String ("Kanha"));
linkedList.readNodeValuesInReverseOrder();
}
}

Thursday, 29 May 2014

How to extract text from PDF Using Apache Tika Library In Java

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;

public class ParsePDFWithTika {
  public static void main(String args[]) throws Exception {

    InputStream is = null;
    try {
      is = new FileInputStream("C:/Temp/realhowto-vbs-20121221.pdf");
      ContentHandler contenthandler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      PDFParser pdfparser = new PDFParser();
      pdfparser.parse(is, contenthandler, metadata, new ParseContext());
      System.out.println(contenthandler.toString());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
        if (is != null) is.close();
    }
  }
}