Tuesday, 2 September 2014

Java Collection Framework

Java Collection Framework

  1. Collection Framework
  2. Hierarchy of Collection Framework
  3. Methods of Collection interface
  4. Iterator interface
Collection Framework provides an architecture to store and manipulate the group of objects.
All the operations that you perform on a data such as searching, sorting, insertion, deletion etc. can be performed by Java Collection Framework.
Collection simply means a single unit of objects. Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).

What is Collection?

Collection represents a single unit of objects i.e. a group.

What is framework?

  • provides readymade architecture.
  • represents set of classes and interface.
  • is optional.

Collection framework

Collection framework represents a unified architecture for storing and manipulating group of object. It has:
  1. Interfaces and its implementations i.e. classes
  2. Algorithm


Do You Know ?
  • What are the two ways to iterate the elements of a collection ?
  • What is the difference between ArrayList and LinkedList classes in collection framework ?
  • What is the difference between ArrayList and Vector classes in collection framework ?
  • What is the difference between HashSet and HashMap classes in collection framework ?
  • What is the difference between HashMap and Hashtable class ?
  • What is the difference between Iterator and Enumeration interface in collection framework ?
  • How can we sort the elements of an object. What is the difference between Comparable and Comparator interfaces ?
  • What does the hashcode() method ?

Hierarchy of Collection Framework

Let us see the hierarchy of collection framework.The java.util package contains all the classes and interfaces for Collection framework.
hierarchy of collection framework

Methods of Collection interface

There are many methods declared in the Collection interface. They are as follows:
No.MethodDescription
1public boolean add(Object element)is used to insert an element in this collection.
2public boolean addAll(collection c)is used to insert the specified collection elements in the invoking collection.
3public boolean remove(Object element)is used to delete an element from this collection.
4public boolean removeAll(Collection c)is used to delete all the elements of specified collection from the invoking collection.
5public boolean retainAll(Collection c)is used to delete all the elements of invoking collection except the specified collection.
6public int size()return the total number of elements in the collection.
7public void clear()removes the total no of element from the collection.
8public boolean contains(object element)is used to search an element.
9public boolean containsAll(Collection c)is used to search the specified collection in this collection.
10public Iterator iterator()returns an iterator.
11public Object[] toArray()converts collection into array.
12public boolean isEmpty()checks if collection is empty.
13public boolean equals(Object element)matches two collection.
14public int hashCode()returns the hashcode number for collection.

Iterator interface

Iterator interface provides the facility of iterating the elements in forward direction only.

Methods of Iterator interface

There are only three methods in the Iterator interface. They are:
  1. public boolean hasNext() it returns true if iterator has more elements.
  2. public object next() it returns the element and moves the cursor pointer to the next element.
  3. public void remove() it removes the last elements returned by the iterator. It is rarely used.
What we will learn in Collection Framework ?
  • ArrayList class
  • LinkedList class
  • ListIterator interface
  • HashSet class
  • LinkedHashSet class
  • TreeSet class
  • PriorityQueue class
  • Map interface
  • HashMap class
  • LinkedHashMap class
  • TreeMap class
  • Hashtable class
  • Sorting
  • Comparable interface
  • Comparator interface
  • Properties class in Java

ArrayList class:

  • uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
  • can contain duplicate elements.
  • maintains insertion order.
  • not synchronized.
  • random access because array works at the index basis.
  • manipulation slow because a lot of shifting needs to be occurred.

Hierarchy of ArrayList class:

ArrayList class hierarchy

Example of ArrayList:

  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.    
  5.   ArrayList al=new ArrayList();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ravi");  
  9.   al.add("Ajay");  
  10.   
  11.   Iterator itr=al.iterator();  
  12.   while(itr.hasNext()){  
  13.    System.out.println(itr.next());  
  14.   }  
  15.  }  
  16. }  
Output:Ravi
       Vijay
       Ravi
       Ajay

Two ways to iterate the elements of collection:

  1. By Iterator interface.
  2. By for-each loop.

Iterating the elements of Collection by for-each loop:

  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.    
  5.   ArrayList al=new ArrayList();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ravi");  
  9.   al.add("Ajay");  
  10.   
  11.   for(Object obj:al)  
  12.     System.out.println(obj);  
  13.  }  
  14. }  
Output:Ravi
       Vijay
       Ravi
       Ajay

Storing user-defined class objects:

  1. class Student{  
  2.   int rollno;  
  3.   String name;  
  4.   int age;  
  5.   Student(int rollno,String name,int age){  
  6.    this.rollno=rollno;  
  7.    this.name=name;  
  8.    this.age=age;  
  9.   }  
  10. }  
  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.     
  5.   Student s1=new Student(101,"Sonoo",23);  
  6.   Student s2=new Student(102,"Ravi",21);  
  7.   Student s2=new Student(103,"Hanumat",25);  
  8.       
  9.   ArrayList al=new ArrayList();  
  10.   al.add(s1);  
  11.   al.add(s2);  
  12.   al.add(s3);  
  13.     
  14.   Iterator itr=al.iterator();  
  15.   while(itr.hasNext()){  
  16.     Student st=(Student)itr.next();  
  17.     System.out.println(st.rollno+" "+st.name+" "+st.age);  
  18.   }  
  19.  }  
  20. }  
Output:101 Sonoo 23
       102 Ravi 21
       103 Hanumat 25

Example of addAll(Collection c) method:

  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.    
  5.   ArrayList al=new ArrayList();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ajay");  
  9.     
  10.   ArrayList al2=new ArrayList();  
  11.   al2.add("Sonoo");  
  12.   al2.add("Hanumat");  
  13.     
  14.   al.addAll(al2);    
  15.   
  16.   Iterator itr=al.iterator();  
  17.   while(itr.hasNext()){  
  18.    System.out.println(itr.next());  
  19.   }  
  20.  }  
  21. }  
Output:Ravi
       Vijay
       Ajay
       Sonoo
       Hanumat

Example of removeAll() method:

  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.    
  5.   ArrayList al=new ArrayList();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ajay");  
  9.     
  10.   ArrayList al2=new ArrayList();  
  11.   al2.add("Ravi");  
  12.   al2.add("Hanumat");  
  13.     
  14.   al.removeAll(al2);  
  15.   
  16.   System.out.println("iterating the elements after removing the elements of al2...");  
  17.   Iterator itr=al.iterator();  
  18.   while(itr.hasNext()){  
  19.    System.out.println(itr.next());  
  20.   }  
  21.   
  22.   }  
  23. }  
Output:iterating the elements after removing the elements of al2...
       Vijay
       Ajay
       

Example of retainAll() method:

  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.    
  5.   ArrayList al=new ArrayList();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ajay");  
  9.     
  10.   ArrayList al2=new ArrayList();  
  11.   al2.add("Ravi");  
  12.   al2.add("Hanumat");  
  13.     
  14.   al.retainAll(al2);  
  15.   
  16.   System.out.println("iterating the elements after retaining the elements of al2...");  
  17.   Iterator itr=al.iterator();  
  18.   while(itr.hasNext()){  
  19.    System.out.println(itr.next());  
  20.   }  
  21.  }  
  22. }  
Output:iterating the elements after retaining the elements of al2...
       Ravi

LinkedList class:

  • uses doubly linked list to store the elements. It extends the AbstractList class and implements List and Deque interfaces.
  • can contain duplicate elements.
  • maintains insertion order.
  • not synchronized.
  • No random access.
  • manipulation fast because no shifting needs to be occurred.
  • can be used as list, stack or queue.
LinkedList class in collection framework

Example of LinkedList:

  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.    
  5.   LinkedList al=new LinkedList();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ravi");  
  9.   al.add("Ajay");  
  10.   
  11.   Iterator itr=al.iterator();  
  12.   while(itr.hasNext()){  
  13.    System.out.println(itr.next());  
  14.   }  
  15.  }  
  16. }  
Output:Ravi
       Vijay
       Ravi
       Ajay

List Interface:

List Interface is the subinterface of Collection.It contains methods to insert and delete elements in index basis.It is a factory of ListIterator interface.

Commonly used methods of List Interface:

  1. public void add(int index,Object element);
  2. public boolean addAll(int index,Collection c);
  3. public object get(int Index position);
  4. public object set(int index,Object element);
  5. public object remove(int index);
  6. public ListIterator listIterator();
  7. public ListIterator listIterator(int i);

ListIterator Interface:

ListIterator Interface is used to traverse the element in backward and forward direction.

Commonly used methods of ListIterator Interface:

  1. public boolean hasNext();
  2. public Object next();
  3. public boolean hasPrevious();
  4. public Object previous();

Example of ListIterator Interface:

  1. import java.util.*;  
  2. class Simple5{  
  3. public static void main(String args[]){  
  4.   
  5. ArrayList al=new ArrayList();  
  6. al.add("Amit");  
  7. al.add("Vijay");  
  8. al.add("Kumar");  
  9. al.add(1,"Sachin");  
  10.   
  11. System.out.println("element at 2nd position: "+al.get(2));  
  12.   
  13. ListIterator itr=al.listIterator();  
  14.   
  15. System.out.println("traversing elements in forward direction...");  
  16. while(itr.hasNext()){  
  17. System.out.println(itr.next());  
  18.  }  
  19.   
  20.   
  21. System.out.println("traversing elements in backward direction...");  
  22. while(itr.hasPrevious()){  
  23. System.out.println(itr.previous());  
  24.  }  
  25. }  
  26. }  
Output:element at 2nd position: Vijay
       traversing elements in forward direction...
       Amit
       Sachin
       Vijay
       Kumar
       traversing elements in backward direction...
       Kumar
       Vijay
       Sachin
       Amit

Difference between List and Set:

List can contain duplicate elements whereas Set contains unique elements only.

HashSet class:

  • uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
  • contains unique elements only.

Hierarchy of HashSet class:

HashSet class hierarchy

Example of HashSet class:

  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.    
  5.   HashSet al=new HashSet();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ravi");  
  9.   al.add("Ajay");  
  10.   
  11.   Iterator itr=al.iterator();  
  12.   while(itr.hasNext()){  
  13.    System.out.println(itr.next());  
  14.   }  
  15.  }  
  16. }  
Output:Ajay
       Vijay
       Ravi

LinkedHashSet class:

  • contains unique elements only like HashSet. It extends HashSet class and implements Set interface.
  • maintains insertion order.

Hierarchy of LinkedHashSet class:

LinkedHashSet class hierarchy

Example of LinkedHashSet class:

  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.    
  5.   LinkedHashSet al=new LinkedHashSet();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ravi");  
  9.   al.add("Ajay");  
  10.   
  11.   Iterator itr=al.iterator();  
  12.   while(itr.hasNext()){  
  13.    System.out.println(itr.next());  
  14.   }  
  15.  }  
  16. }  
Output:>Ravi
       Vijay
       Ajay

TreeSet class:

  • contains unique elements only like HashSet. The TreeSet class implements NavigableSet interface that extends the SortedSet interface.
  • maintains ascending order.

Hierarchy of TreeSet class:

TreeSet class hierarchy

Example of TreeSet class:

  1. import java.util.*;  
  2. class Simple{  
  3.  public static void main(String args[]){  
  4.    
  5.   TreeSet al=new TreeSet();  
  6.   al.add("Ravi");  
  7.   al.add("Vijay");  
  8.   al.add("Ravi");  
  9.   al.add("Ajay");  
  10.   
  11.   Iterator itr=al.iterator();  
  12.   while(itr.hasNext()){  
  13.    System.out.println(itr.next());  
  14.   }  
  15.  }  
  16. }  
Output:Ajay
       Ravi
       Vijay
       

Queue Interface:

The Queue interface basically orders the element in FIFO(First In First Out)manner.

Methods of Queue Interface :

  1. public boolean add(object);
  2. public boolean offer(object);
  3. public remove();
  4. public poll();
  5. public element();
  6. public peek();

PriorityQueue class:

The PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner.

Example of PriorityQueue:

  1. import java.util.*;  
  2. class Simple8{  
  3. public static void main(String args[]){  
  4.   
  5. PriorityQueue queue=new PriorityQueue();  
  6. queue.add("Amit");  
  7. queue.add("Vijay");  
  8. queue.add("Karan");  
  9. queue.add("Jai");  
  10. queue.add("Rahul");  
  11.   
  12. System.out.println("head:"+queue.element());  
  13. System.out.println("head:"+queue.peek());  
  14.   
  15. System.out.println("iterating the queue elements:");  
  16. Iterator itr=queue.iterator();  
  17. while(itr.hasNext()){  
  18. System.out.println(itr.next());  
  19. }  
  20.   
  21. queue.remove();  
  22. queue.poll();  
  23.   
  24. System.out.println("after removing two elements:");  
  25. Iterator itr2=queue.iterator();  
  26. while(itr2.hasNext()){  
  27. System.out.println(itr2.next());  
  28. }  
  29.   
  30. }  
  31. }  
Output:head:Amit
       head:Amit
       iterating the queue elements:
       Amit
       Jai
       Karan
       Vijay
       Rahul
       after removing two elements:
       Karan
       Rahul
       Vijay

Sorting

We can sort the elements of:
  1. String objects
  2. Wrapper class objects
  3. User-defined class objects
Collections class provides static methods for sorting the elements of collection.If collection elements are of Set type, we can use TreeSet.But We cannot sort the elements of List.Collections class provides methods for sorting the elements of List type elements.

Method of Collections class for sorting List elements

public void sort(List list): is used to sort the elements of List.List elements must be of Comparable type.

Note: String class and Wrapper classes implements the Comparable interface.So if you store the objects of string or wrapper classes, it will be Comparable.

Example of Sorting the elements of List that contains string objects

  1. import java.util.*;  
  2. class Simple12{  
  3. public static void main(String args[]){  
  4.   
  5. ArrayList al=new ArrayList();  
  6. al.add("Viru");  
  7. al.add("Saurav");  
  8. al.add("Mukesh");  
  9. al.add("Tahir");  
  10.   
  11. Collections.sort(al);  
  12. Iterator itr=al.iterator();  
  13. while(itr.hasNext()){  
  14. System.out.println(itr.next());  
  15.  }  
  16. }  
  17. }  
Output:Mukesh
       Saurav
       Tahir
       Viru
       

Example of Sorting the elements of List that contains Wrapper class objects

  1. import java.util.*;  
  2. class Simple12{  
  3. public static void main(String args[]){  
  4.   
  5. ArrayList al=new ArrayList();  
  6. al.add(Integer.valueOf(201));  
  7. al.add(Integer.valueOf(101));  
  8. al.add(230);//internally will be converted into objects as Integer.valueOf(230)  
  9.   
  10. Collections.sort(al);  
  11.   
  12. Iterator itr=al.iterator();  
  13. while(itr.hasNext()){  
  14. System.out.println(itr.next());  
  15.  }  
  16. }  
  17. }  
Output:101
       201
       230

Comparable interface

Comparable interface is used to order the objects of user-defined class.This interface is found in java.lang package and contains only one method named compareTo(Object).It provide only single sorting sequence i.e. you can sort the elements on based on single datamember only.For instance it may be either rollno,name,age or anything else.

Syntax:

public int compareTo(Object obj): is used to compare the current object with the specified object.

We can sort the elements of:
  1. String objects
  2. Wrapper class objects
  3. User-defined class objects
Collections class provides static methods for sorting the elements of collection.If collection elements are of Set type, we can use TreeSet.But We cannot sort the elements of List.Collections class provides methods for sorting the elements of List type elements.

Method of Collections class for sorting List elements

public void sort(List list): is used to sort the elements of List.List elements must be of Comparable type.

Note: String class and Wrapper classes implements the Comparable interface.So if you store the objects of string or wrapper classes, it will be Comparable.


Example of Sorting the elements of List that contains user-defined class objects on age basis

Student.java

  1. class Student implements Comparable{  
  2. int rollno;  
  3. String name;  
  4. int age;  
  5. Student(int rollno,String name,int age){  
  6. this.rollno=rollno;  
  7. this.name=name;  
  8. this.age=age;  
  9. }  
  10.   
  11. public int compareTo(Object obj){  
  12. Student st=(Student)obj;  
  13. if(age==st.age)  
  14. return 0;  
  15. else if(age>st.age)  
  16. return 1;  
  17. else  
  18. return -1;  
  19. }  
  20.   
  21. }  

Simple.java

  1. import java.util.*;  
  2. import java.io.*;  
  3.   
  4. class Simple{  
  5. public static void main(String args[]){  
  6.   
  7. ArrayList al=new ArrayList();  
  8. al.add(new Student(101,"Vijay",23));  
  9. al.add(new Student(106,"Ajay",27));  
  10. al.add(new Student(105,"Jai",21));  
  11.   
  12. Collections.sort(al);  
  13. Iterator itr=al.iterator();  
  14. while(itr.hasNext()){  
  15. Student st=(Student)itr.next();  
  16. System.out.println(st.rollno+""+st.name+""+st.age);  
  17.   }  
  18. }  
  19. }  
Output:105 Jai 21
       101 Vijay 23
       106 Ajay 27

Comparator interface

Comparator interface is used to order the objects of user-defined class. This interface is found in java.util package and contains 2 methods compare(Object obj1,Object obj2) and equals(Object element). It provides multiple sorting sequence i.e. you can sort the elements based on any data member. For instance it may be on rollno, name, age or anything else.

Syntax of compare method

public int compare(Object obj1,Object obj2): compares the first object with second object.
Collections class provides static methods for sorting the elements of collection. If collection elements are of Set type, we can use TreeSet. But We cannot sort the elements of List. Collections class provides methods for sorting the elements of List type elements.

Method of Collections class for sorting List elements

public void sort(List list,Comparator c): is used to sort the elements of List by the given comparator.

Example of sorting the elements of List that contains user-defined class objects on the basis of age and name

In this example, we have created 4 java classes:
  1. Student.java
  2. AgeComparator.java
  3. NameComparator.java
  4. Simple.java
Student.javaThis class contains three fields rollno, name and age and a parameterized constructor.
  1. class Student{  
  2. int rollno;  
  3. String name;  
  4. int age;  
  5. Student(int rollno,String name,int age){  
  6. this.rollno=rollno;  
  7. this.name=name;  
  8. this.age=age;  
  9. }  
  10. }  
AgeComparator.javaThis class defines comparison logic based on the age. If age of first object is greater than the second, we are returning positive value, it can be any one such as 1, 2 , 10 etc. If age of first object is less than the second object, we are returning negative value, it can be any negative value and if age of both objects are equal, we are returning 0.
  1. import java.util.*;  
  2. class AgeComparator implements Comparator{  
  3. public int Compare(Object o1,Object o2){  
  4. Student s1=(Student)o1;  
  5. Student s2=(Student)o2;  
  6.   
  7. if(s1.age==s2.age)  
  8. return 0;  
  9. else if(s1.age>s2.age)  
  10. return 1;  
  11. else  
  12. return -1;  
  13. }  
  14. }  
NameComparator.javaThis class provides comparison logic based on the name. In such case, we are using the compareTo() method of String class, which internally provides the comparison logic.
  1. import java.util.*;  
  2. class NameComparator implements Comparator{  
  3. public int Compare(Object o1,Object o2){  
  4. Student s1=(Student)o1;  
  5. Student s2=(Student)o2;  
  6.   
  7. return s1.name.compareTo(s2.name);  
  8. }  
  9. }  
Simple.javaIn this class, we are printing the objects values by sorting on the basis of name and age.
  1. import java.util.*;  
  2. import java.io.*;  
  3.   
  4. class Simple{  
  5. public static void main(String args[]){  
  6.   
  7. ArrayList al=new ArrayList();  
  8. al.add(new Student(101,"Vijay",23));  
  9. al.add(new Student(106,"Ajay",27));  
  10. al.add(new Student(105,"Jai",21));  
  11.   
  12. System.out.println("Sorting by Name...");  
  13.   
  14. Collections.sort(al,new NameComparator());  
  15. Iterator itr=al.iterator();  
  16. while(itr.hasNext()){  
  17. Student st=(Student)itr.next();  
  18. System.out.println(st.rollno+" "+st.name+" "+st.age);  
  19. }  
  20.   
  21. System.out.println("sorting by age...");  
  22.   
  23. Collections.sort(al,new AgeComparator());  
  24. Iterator itr2=al.iterator();  
  25. while(itr2.hasNext()){  
  26. Student st=(Student)itr2.next();  
  27. System.out.println(st.rollno+" "+st.name+" "+st.age);  
  28. }  
  29.   
  30.   
  31. }  
  32. }  
Output:Sorting by Name...
       106 Ajay 27
       105 Jai 21
       101 Vijay 23
       Sorting by age...       
       105 Jai 21
       101 Vijay 23
       106 Ajay 27

Properties class in Java

The properties object contains key and value pair both as a string. It is the subclass of Hashtable. It can be used to get property value based on the property key. The Properties class provides methods to get data from properties file and store data into properties file. Moreover, it can be used to get properties of system.

Advantage of properties file

Easy Maintenance: If any information is changed from the properties file, you don't need to recompile the java class. It is mainly used to contain variable information i.e. to be changed.

Methods of Properties class

The commonly used methods of Properties class are given below.
MethodDescription
public void load(Reader r)loads data from the Reader object.
public void load(InputStream is)loads data from the InputStream object
public String getProperty(String key)returns value based on the key.
public void setProperty(String key,String value)sets the property in the properties object.
public void store(Writer w, String comment)writers the properties in the writer object.
public void store(OutputStream os, String comment)writes the properties in the OutputStream object.
storeToXML(OutputStream os, String comment)writers the properties in the writer object for generating xml document.
public void storeToXML(Writer w, String comment, String encoding)writers the properties in the writer object for generating xml document with specified encoding.

Example of Properties class to get information from properties file

To get information from the properties file, create the properties file first. db.properties
  1. user=system  
  2. password=oracle  
Now, lets create the java class to read the data from the properties file. Test.java
  1. import java.util.*;  
  2. import java.io.*;  
  3. public class Test {  
  4. public static void main(String[] args)throws Exception{  
  5.     FileReader reader=new FileReader("db.properties");  
  6.       
  7.     Properties p=new Properties();  
  8.     p.load(reader);  
  9.       
  10.     System.out.println(p.getProperty("user"));  
  11.     System.out.println(p.getProperty("password"));  
  12. }  
  13. }  
Output:system
       oracle
Now if you change the value of the properties file, you don't need to compile the java class again. That means no maintenance problem.

Example of Properties class to get all the system properties

By System.getProperties() method we can get all the properties of system. Let's create the class that gets information from the system properties. Test.java
  1. import java.util.*;  
  2. import java.io.*;  
  3. public class Test {  
  4. public static void main(String[] args)throws Exception{  
  5.   
  6. Properties p=System.getProperties();  
  7. Set set=p.entrySet();  
  8.   
  9. Iterator itr=set.iterator();  
  10. while(itr.hasNext()){  
  11. Map.Entry entry=(Map.Entry)itr.next();  
  12. System.out.println(entry.getKey()+" = "+entry.getValue());  
  13. }  
  14.   
  15. }  
  16. }  
Output:
java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = C:\Program Files\Java\jdk1.7.0_01\jre\bin
java.vm.version = 21.1-b02
java.vm.vendor = Oracle Corporation
java.vendor.url = http://java.oracle.com/
path.separator = ;
java.vm.name = Java HotSpot(TM) Client VM
file.encoding.pkg = sun.io
user.country = US
user.script = 
sun.java.launcher = SUN_STANDARD
...........

Example of Properties class to create properties file

Now lets write the code to create the properties file. Test.java
  1. import java.util.*;  
  2. import java.io.*;  
  3. public class Test {  
  4. public static void main(String[] args)throws Exception{  
  5.   
  6. Properties p=new Properties();  
  7. p.setProperty("name","Sonoo Jaiswal");  
  8. p.setProperty("email","sonoojaiswal@javatpoint.com");  
  9.   
  10. p.store(new FileWriter("info.properties"),"Javatpoint Properties Example");  
  11.   
  12. }  
  13. }  
Let's see the generated properties file. info.properties
  1. #Javatpoint Properties Example  
  2. #Thu Oct 03 22:35:53 IST 2013  
  3. email=sonoojaiswal@javatpoint.com  
  4. name=Sonoo Jaiswal  

No comments:

Post a Comment