Use BitSet to track flags - Java Collection Framework

Java examples for Collection Framework:BitSet

Description

Use BitSet to track flags

Demo Code

import java.util.BitSet;

public class Main {
  BitSet sked = new BitSet(365);

  public Main() {
    /*  ww w . j  a  v  a  2 s. c om*/
    int[] holiday = { 1, 15, 50, 148, 185, 246, 281, 316, 326, 359 };
    for (int i = 0; i < holiday.length; i++) {
      addHoliday(holiday[i]);
    }
  }

  public void addHoliday(int dayToAdd) {
    sked.set(dayToAdd);
  }

  public boolean isHoliday(int dayToCheck) {
    boolean result = sked.get(dayToCheck);
    return result;
  }

  public static void main(String[] arguments) {
    Main cal = new Main();

    try {
      int whichDay = 123;
      if (cal.isHoliday(whichDay)) {
        System.out.println("Day number " + whichDay + " is a holiday.");
      } else {
        System.out.println("Day number " + whichDay + " is not a holiday.");
      }
    } catch (NumberFormatException nfe) {
      System.out.println("Error: " + nfe.getMessage());
    }

  }
}

Result


Related Tutorials