Java BitSet to bitSetToMap(Map map, String key, BitSet bits, int length)

Here you can find the source of bitSetToMap(Map map, String key, BitSet bits, int length)

Description

Stores a BitSet object as a string in a map.

License

Open Source License

Parameter

Parameter Description
map A Map object to store the bit set in.
key The map key in which to store the bit set.
bits The bit set that will be stored in the map.
length Number of bits to store.

Return

The same map object as the parameter.

Declaration

public static Map<String, String> bitSetToMap(Map<String, String> map, String key, BitSet bits, int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//from w ww. j  a  v a  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

  1. bitSet2Int(BitSet bs)
  2. bitsetToDoubleArray(BitSet bs, int n)
  3. bitsetToIndices(BitSet bits)
  4. bitSetToInt(final BitSet bitSet)
  5. bitSetToLong(BitSet set)
  6. bitSetToUnsignedInt(BitSet b, int startBit, int length)
  7. bitsToBytes(BitSet ba, int size)
  8. BitsToBytes(BitSet bits)
  9. bitsToHexString(BitSet ba, int size)