Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Thursday, November 27, 2014

TreeSet in java : A detailed overview

TreeSet in java under Collection Framework have below property.

1. Underlying data structure is balanced  tree.
2. Doesn't allow duplicate.
3. Elements are inserted as per natural sorting order by default.
4. All elements which are inserted as per default natural sorting order must be comparable in nature.
5. An object is said to be comparable only if corresponding class implements java.lang.Comparable interface.
6. If we use default natural sorting order than objects are compared using compareTo(Object obj) method of java.lang.Comparable interface.
7. Null insertion is not allowed.


Constructor in TreeSet

TreeSet ts=new TreeSet(); //creates an empty TreeSet with default natural sorting order
TreeSet ts=new TreeSet(Comparator c); //creates an empty TreeSet with passed Customoized sorting order

TreeSet ts=new TreeSet(SortedSet s); //Creates a TreeSet view of a Passed SortedSet argument object.


//TreeSetDemo.java


import java.util.*;
class TreeSetDemo 
{
 public static void main(String[] args) 
 {
  TreeSet ts=new TreeSet();
  ts.add("10");
  ts.add("20");
  ts.add("30");
  System.out.println(ts);
 }
}



TreeSet by default stores objects which are homogeneous and comparable, an object is said to be comparable if and only if it implements Comparable interface.

TreeSet stores elements based upon the default natural sorting order.Elements in the TreeSet are comparaed using compareTo(Object obj) method of java.lang.Comparable interface.

But if we want to store objects based upon our own customized sorting order then we need to have a class which implements java.util.Comparator interface.


Comparator


  • It is meant for customized sorting order.
  • Present in java.util package.
  • Contains below 2 method.

1. public int compare(Object obj1,Object obj2)
2. equals()


any class which implements java.util.Comparator Interface should compulsory implement compare() method. Whereas implementing equal() method is optional.

This is because every java class is a child class of Java.lang.Object class. Object class provide implementation for equals() method, so equals () is by default present in every java class.


Write a java program to insert Integer elements in TreeSet with desecending sorting order.



import java.util.*;
class ReverseTreeSet 
{
 public static void main(String[] args) 
 {
  TreeSet ts=new TreeSet(new Com());
  ts.add(10);
  ts.add(20);
  ts.add(30);
  ts.add(40);
  ts.add(50);
  System.out.println(ts);
 }
}

//Java code for customized sorting order


import java.util.*;
class Com implements Comparator
{
 public int compare(Object obj1, Object obj2)
 {
  Integer i1=(Integer)obj1;
  Integer i2=(Integer)obj2;
  return i2.compareTo(i1);
 }
}

Saturday, November 22, 2014

India Insurance Guide : A complete resource for your insurance need



Insurance is the big thing in the dynamically growing world, insurance for different purpose comes in different flavors and different rules based upon the kind of insurance policy you have taken.

India insurance guide is the complete reference for insurance policies, how to claim them and what rules are involved. 

Buy it here


As per the description from the FlipKart 

India Insurance Guide-Handbook of Insurance Policies, Claims and Law is a fully updated and revised 2nd Edition. It is a Reference book on Insurance and Insurance sector in India and covers both Life and Non-Life Insurance with important statistics. Review of India Insurance Guide has been published by Times of India & Asia Insurance Review (Professional magazine published from Singapore).

Foreword of book has been written by honorable Justice (Retd.) N. K. Jain Former Judge of M. P. High Court, Ex-President, M. P. State Consumer Disputes Redressal Commission Bhopal. This updated Reference Book on Insurance explains theoretical & practical aspects of Insurance Policies, Claims & Law related to life & non-life insurance in India & would be found very useful for insurance consumers, students of insurance, Lawyers, insurance professionals, financial consultants, Insurance claims consultants and must for every library in India. This book would prove to be a very important supplementary training/study material for Training Programs conducted for Insurance and bank employees, Insurance agents, Insurance brokers, Insurance surveyors, Third Party Administrators, Courses conducted by Insurance Institute of India, College of Insurance, PG Diploma in Life/General Insurance, MBA in Insurance/Finance, etc.




Apart from india insurance guide , Mr. L. P. gupta has written couple of other handbooks for insurance named as "General insurance guide" and "Insurance claims solutions"

Mr. L. P. Gupta can be reached here : lpgupta1950@gmail.com

L. P. Gupta personal blog can be read here : Blog

Sunday, November 9, 2014

Mail System User : JAVA PROJECT for freshers and final year students

Mail System

Mail system is a JSP-Servlet based advance java project for all the freshers willing to build their career in java software /application develpoment workspace.

This project can be used to showcase in your resume as well as to grow your knowledge on the subjects like JSP and Servlets. 

Its a mini web application which can can be used for best practices.

Technology used

              JSP
·         SERVLET
·         CORE JAVA
·         HTML
·         CSS
·         SQL

To use the email services project below are the prerequisite.
1.       Java
2.       MySql
3.       Apache Tomcat 7.0 or +

To customize the project below are the prerequisite.

1.       Eclipse

Database configuration

·     Create a MySql database and name it as mailsystem.

·         Run the below sql query to create table.
o   CREATE TABLE MAILSYSTEMUSER(EMAIL VARCHAR(100),PASSWORD VARCHAR(50),NAME VARCHAR(100),GENDER VARCHAR(25),MNUM VARCHAR(100), COUNTRY VARCHAR(100))

o   CREATE TABLE INBOX(ID INT(20),RECIEVER VARCHAR(100),SENDER VARCHAR(100),MESSAGE VARCHAR(1000),DATE_OF_RECEIVING VARCHAR(100))

How to use Project

·         Unzip the project to your current workspace of eclipse.
·         Configure the Tomcat 7.0 or higher.
·         Run the project.


Home page of application

Home page of project

Add caption

Compose mail page

Registration page
Download here:

Download here:

Please let us know in comment section if this project helped you that way we will be encouraged to develop more and more projects for free .

If you run into any problem related to this project , feel free to ping us at diehardtechy@gmail.com or you can comment in the comment section below.

Like us on facebook for more !!

Wednesday, November 5, 2014

How to read the value of a attribute of a XML tag in Java

How to read the value of a attribute of a XML tag using Java core library. Attributes reside inside the tag and can be read using the org.w3c.dom and javax.xml.Xpath package. There are several others ways also to achieve the same behavior.

For example you have a below XML file .


<?xml version="1.0" encoding="UTF-8"?>
<details name="person-detail" >
    <property name="personal">
        <value text="Mr.XYZ"/>
        <value text="Teacher" />
        <value text="Single" />
    </property>   
</details>


and you want to read the value of text attribute of a value tag of above XML using java and print it on console.

Expected Output:
Mr.XYZ
Teacher
Single


Given below is the code to achieve expected behavior using java.
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
import javax.xml.xpath.*;

public class XMLRead
{

    public static void main(String args[]) 
    {
        try 
        {
   DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
   Document dDoc = builder.parse("C:\\MyXMLFile.xml");
   XPath xPath = XPathFactory.newInstance().newXPath();
   NodeList nodes1 = (NodeList) xPath.evaluate("/details/property[@name='personal']/value/@text", dDoc, XPathConstants.NODESET);
   for (int j = 0; j < nodes1.getLength(); j++) {
   Node node1 = nodes1.item(j);
   System.out.println(node1.getTextContent()); 
   }

        } 
  catch (Exception e) 
        {
            e.printStackTrace();
        }

    }
}

Above code produces the below output.


In Ruby how to use the function of a module defined in a different directory



To use the function of a module defined in a different directory below steps need to be followed.

1. Define a module in some directory other then which have your ruby class.

Example

Say some module Week is defined in trig.rb file inside io/test package.


module Week
  FIRST_DAY = "Sunday"
  def Week.weeks_in_month
    puts "You have four weeks in a month"
  end
  def Week.weeks_in_year
    puts "You have 52 weeks in a year"
  end
end


2. To use this module related function in your class 'test.rb' use the below syntax.


#!/usr/bin/ruby

require_relative 'io/test/trig'

class Decade

end
d1=Decade.new
puts Week::FIRST_DAY
Week.weeks_in_month
Week.weeks_in_year

To make it more clear check the below the hierarchy diagram of directory structure.

Hierarchy diagram of above explanation


To download the above example code: