Set: clone() : Set « java.util « Java by API






Set: clone()

 
/*
 Output:

[I, P, G, E]
[I, P, G, E]

 * */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MainClass {
  public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    Set set = new HashSet(Arrays.asList(elements));
    Set set2 = ((Set) ((HashSet) set).clone());
    System.out.println(set2);
    FileOutputStream fos = new FileOutputStream("set.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(set);
    oos.close();
    FileInputStream fis = new FileInputStream("set.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Set set3 = (Set) ois.readObject();
    ois.close();
    System.out.println(set3);
  }
}
           
         
  








Related examples in the same category

1.Set: add(String e)
2.Set: addAll(Collection c)
3.Set: clear()
4.Set: contains(Object o)
5.Set: containsAll(Collection c)
6.Set: equals(Object o)
7.Set: isEmpty()
8.Set: iterator()
9.Set: remove(Object o)
10.Set: removeAll(Collection c)
11.Set: retainAll(Collection c)
12.Set: size()
13.Set: toArray()
14.Set: toArray(String[] a)