Category: Java

Java: Calculate all possible k-size subsets

Get all subsets of a certain size.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SetCombination {
   static void recursiveSubsets(List<int[]> sets, int s[], int k, int t[], int q, int r) {
      if (q == k) {
         int ss[] = new int[k];
         for (int i = 0; i < k; ++i) {
            ss[i] = t[i];
         }
         sets.add(ss);
      } else {
         for (int i = r; i < s.length; ++i) {
            t[q] = s[i];
            recursiveSubsets(sets, s, k, t, q + 1, i + 1);
         }
      }
   }

   public static List<int[]> subSets(int s[], int k) {
      List<int[]> list = new ArrayList<>();
      int t[] = new int[s.length];
      recursiveSubsets(list, s, k, t, 0, 0);
      return list;
   }

   public static void main(String arg[]) {
      int s[] = {1, 2, 3, 4, 5, 6};
      List<int[]> list = subSets(s, 3);
      for (int x[] : list) {
         System.out.println(Arrays.toString(x));
      }
   }
}

Prints out:
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 2, 6]
[1, 3, 4]
[1, 3, 5]
[1, 3, 6]
[1, 4, 5]
[1, 4, 6]
[1, 5, 6]
[2, 3, 4]
[2, 3, 5]
[2, 3, 6]
[2, 4, 5]
[2, 4, 6]
[2, 5, 6]
[3, 4, 5]
[3, 4, 6]
[3, 5, 6]
[4, 5, 6]

Advertisements

Running and controlling JavaFX applications from regular Java

The concept for JavaFX seems to be that your JavaFX Application is the control center for everything. But what if you would like to run an application as a new screen and to be able to run multiple applications and the same application multiple times? This turns out to be difficult. I spent most of a day trying to figure out how to do it and I think that I came up with a reasonable solution and so wanted to share.

Here is the main utility class and method. You create an application that you want to display i.e. x = new MyApplication(). Then you just call JavaFXUtil.runApplication(…). This will make sure JavaFX is initialized, start your application and block until it is ready to be accessed externally.

public class JavaFXUtil {
   public static void runApplication(final Application application)
           throws InterruptedException {
      InitializeFXApplication.initializeJavaFX();
      final WaitObject wait = new WaitObject();
      Platform.runLater(
              new Runnable() {
         @Override
         public void run() {
            try {
               application.init();
               Stage stage = new Stage();
               application.start(stage);
               wait.ready();
            } catch (Exception ex) {
               Logger.getLogger(JavaFXUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
         }
      });
      wait.waitUntilReady();
   }
   private JavaFXUtil() {
   }
}

This utility function depends on two other helper classes:

public class InitializeFXApplication extends Application {
   private static boolean started = false;
   private static WaitObject wait = new WaitObject();
   @Override
   public void start(Stage stage) throws Exception {
      started = true;
      wait.ready();
   }
   public static void initializeJavaFX() throws InterruptedException {
      if (started) {
         return;
      }
      new Thread() {
         @Override
         public void run() {
            Application.launch(InitializeFXApplication.class);
         }
      }.start();
      wait.waitUntilReady();
      Logger.getLogger(InitializeFXApplication.class.getName()).log(Level.INFO, "JavaFX initialized");
   }
}

This is the class that makes sure JavaFX environment is initialized.

public class WaitObject {
   private CountDownLatch latch = new CountDownLatch(1);
   public void ready() {
      latch.countDown();
   }
   public void waitUntilReady() throws InterruptedException {
      latch.await();
   }
   public boolean isReady() {
      return latch.getCount() == 0;
   }
}

This is just a helper object to manage waiting for resources to be available.