Stores a BitSet object as a string in a map. - Java java.util

Java examples for java.util:BitSet

Description

Stores a BitSet object as a string in a map.

Demo Code


//package com.java2s;

import java.util.BitSet;
import java.util.Map;

public class Main {
    /**/* w ww  .  j  a  va 2  s.c o m*/
     * Stores a BitSet object as a string in a map. Used for state persistence.
     * Basically converts the BitSet into a string of "1" and "0" according to its bit values.
     *
     * @param map A Map object to store the bit set in.
     * @param key The map key in which to store the bit set.
     * @param bits The bit set that will be stored in the map.
     * @param length Number of bits to store.
     * @return The same map object as the parameter.
     */
    public static Map<String, String> bitSetToMap(Map<String, String> map,
            String key, BitSet bits, int length) {
        map.put(key, bitSetToString(bits, length));
        return map;
    }

    /**
     * Converts a BitSet to String
     * @param bits
     * @param length
     * @return 
     */
    public static String bitSetToString(BitSet bits, int length) {
        String sbits = "";
        for (int i = length - 1; i >= 0; i--)
            sbits += (bits.get(i) ? "1" : "0");

        return sbits;
    }
}

Related Tutorials