Objective:
In the previous article Collection in Java we learned about the collection method used in Java. In this article we will learn about the Set in Collection and File Handling in Java.
Set:
The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.
Set have its implementation in various classes like
-
HashSet
-
LinkedHashSet.
-
TreeSet
Methods with description:
add( )
Adds an object to the collection
clear( )
Removes all objects from the collection
contains( )
Returns true if a specified object is an element within the collection
isEmpty( )
Returns true if the collection has no elements
iterator( )
Returns an Iterator object for the collection which may be used to retrieve an object
size( )
Returns the number of elements in the collection
Example 1:
Following is the example to explain Set functionality:
import java.util.*;
public class SetDemo {
public static void main(String args[ ]) {
int count[ ] = {34, 22,10,60,30,22};
Set<Integer> set = new HashSet<Integer>();
try{
for(int i = 0; i<5; i++){
set.add(count[i]);
}
System.out.println(set);
TreeSet sortedSet = new TreeSet<Integer>(set);
System.out.println("The sorted list is:");
System.out.println(sortedSet);
System.out.println("The First element of the set is: "+
(Integer)sortedSet.first());
System.out.println("The last element of the set is: "+
(Integer)sortedSet.last());
}
catch(Exception e){}
}
}
//Output:
[amrood]$ java SetDemo
[34, 30, 60, 10, 22]
The sorted list is:
[10, 22, 30, 34, 60]
The First element of the set is: 10
The last element of the set is: 60
File Handling in java:
Java FileInputStream and OutputStream
In Java, FileInputStream and FileOutputStream classes are used to read and write data in file. In another words, they are used for file handling in java.
Java FileOutputStream:
Java FileOutputStream is an output stream for writing data to a file.
If you have to write primitive values then use FileOutputStream.Instead, for character-oriented data, prefer FileWriter.But you can write byte-oriented as well as character-oriented data.
Example:
import java.io.*;
class Test{
public static void main(String args[]){
try{
FileOutputstream fout=new FileOutputStream("abc.txt");
String s="Sachin Tendulkar is my favourite player";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
}catch(Exception e){system.out.println(e);}
}
}
//Output:
success...
Java FileInputStream:
Java FileInputStream class obtains input bytes from a file.It is used for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
It should be used to read byte-oriented data for example to read image, audio, video etc.
Example:
import java.io.*;
class SimpleRead{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("abc.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
}catch(Exception e){system.out.println(e);}
}
}
//Output
Sachin is my favourite player.