HashSet clone

In this chapter you will learn:

  1. Clone a hash set

Clone a hash set

Object clone() Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.

import java.util.HashSet;
//from   j  av  a2 s  .co m
public class Main{
    public static void main(String args[]) {
        HashSet<String> hs = new HashSet<String>();

        hs.add("java2s.com");
        hs.add("A");
        hs.add("D");
        hs.add("E");
        hs.add("C");
        hs.add("j a v a2s.com");

        System.out.println(hs);
        
        Object obj = hs.clone();
        
        System.out.println(obj);
        
    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Get an iterator from a hash set
Home » Java Tutorial » Set
HashSet
HashSet element adding
HashSet element removing and clearing
HashSet clone
HashSet iterator
HashSet properties
TreeSet
TreeSet elements adding
TreeSet subSet
NavigableSet
LinkedHashSet