Iterate Over
Unmodifiable Collection
import
java.util.ArrayList;
import
java.util.Collection;
import
java.util.Collections;
import
java.util.Iterator;
import java.util.List;
public class
UnmodifiableCollection
{
public static void
main(String args[])
{
List
list = new ArrayList();
list.add("This");
list.add("is");
list.add("Unmodifiable
Collection");
System.out.println("Element
added to list: " + list.get(2));
Collection
immutableCol = Collections.unmodifiableCollection(list);
Iterator
iterator = immutableCol.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}
}
Iterate through the
values of Java Tree Map
import
java.util.Collection;
import
java.util.TreeMap;
import
java.util.Iterator;
public class
IterateValuesOfTreeMapExample
{
public static void
main(String[] args)
{
TreeMap treeMap = new
TreeMap();
treeMap.put("1","One");
treeMap.put("2","Two");
treeMap.put("3","Three");
Collection c =
treeMap.values(); Iterator itr = c.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
}
Iterate through the
values of Java HashMap
import
java.util.Collection;
import
java.util.HashMap;
import
java.util.Iterator;
public class
IterateValuesOfHashMapExample
{
public static void
main(String[] args)
{
HashMap hMap = new
HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
Collection c =
hMap.values(); Iterator itr = c.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
}
Checked and unchecked exceptions in java
import java.io.*;
class Example
{
public static void main(String args[])
{
FileInputStream fis = null;
fis = new FileInputStream("B:/myfile.txt");
int k;
while(( k = fis.read() ) != -1)
{
System.out.print((char)k);
}
fis.close();
}
}
No comments:
Post a Comment