
- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java BitSet clone() Method
Description
The Java BitSet clone() method produces a new BitSet that is equal to this one. The clone of the bit set is another bit set that has exactly the same bits set to true as this bit set.
Declaration
Following is the declaration for java.util.BitSet.clone() method
public Object clone()
Parameters
NA
Return Value
This method returns a clone of this bit set.
Exception
NA
Example 1
The following example shows the usage of Java BitSet clone() method. We're creating a BitSet. We're setting true values in the BitSet object using set() method call and using clone() method we're creating a clone of first bitset and then print the new bitset.
package com.tutorialspoint; import java.util.BitSet; public class BitSetDemo { public static void main(String[] args) { // create a bitset BitSet bitset = new BitSet(); // assign values to bitset1 bitset.set(0, 6, true); // print the set System.out.println("Bitset: " + bitset); // clone the bitset Object bitset1 = bitset.clone(); // print bitset1 System.out.println("Bitset1: " + bitset1); } }
Let us compile and run the above program, this will produce the following result −
Bitset: {0, 1, 2, 3, 4, 5} Bitset1: {0, 1, 2, 3, 4, 5}
Example 2
The following example shows the usage of Java BitSet clone() method. We're creating two BitSets using byte[] and using clone() method we're creating a clone of first bitset and then print the new bitset.
package com.tutorialspoint; import java.util.BitSet; public class BitSetDemo { public static void main(String[] args) { // create a bitset BitSet bitset = BitSet.valueOf(new byte[] { 0, 1, 2, 3, 4, 5 }); // print the set System.out.println("Bitset: " + bitset); // clone the bitset Object bitset1 = bitset.clone(); // print bitset1 System.out.println("Bitset1: " + bitset1); } }
Let us compile and run the above program, this will produce the following result −
Bitset: {8, 17, 24, 25, 34, 40, 42} Bitset1: {8, 17, 24, 25, 34, 40, 42}
Example 3
The following example shows the usage of Java BitSet clone() method. We're creating two BitSets using long[] and using clone() method we're creating a clone of first bitset and then print the new bitset.
package com.tutorialspoint; import java.util.BitSet; public class BitSetDemo { public static void main(String[] args) { // create a bitset BitSet bitset = BitSet.valueOf(new long[] { 0, 1, 2, 3, 4, 5 }); // print the set System.out.println("Bitset: " + bitset); // clone the bitset Object bitset1 = bitset.clone(); // print bitset1 System.out.println("Bitset1: " + bitset1); } }
Let us compile and run the above program, this will produce the following result −
Bitset: {64, 129, 192, 193, 258, 320, 322} Bitset1: {64, 129, 192, 193, 258, 320, 322}