简体   繁体   English

如何为BitSet类型的元素创建SortedSet(例如TreeSet)

[英]How to create SortedSet (e.g. TreeSet) for elements of type BitSet

I have a number (power(2,k)) of BitSet objects and I want to store them in a SortedSet . 我有一个数字(power(2,k))的BitSet对象,我想将它们存储在SortedSet I use the code: 我用的代码是:

Set <BitSet> S= new TreeSet<>();

However, I am getting this error: java.lang.ClassCastException: java.util.BitSet cannot be cast to java.lang.Comparable 但是,我收到此错误: java.lang.ClassCastException: java.util.BitSet cannot be cast to java.lang.Comparable

How do I implement comparable interface? 如何实现类似的界面? Or is there any other way to sort these elements of type BitSet ? 或者有没有其他方法来对BitSet类型的这些元素进行排序?

There are two ways to use a TreeSet . 有两种方法可以使用TreeSet

  1. Have it contain objects that implement Comparable 它包含实现Comparable对象
  2. Have a custom Comparator object that compares the elements of your TreeSet . 有一个自定义Comparator对象,用于比较TreeSet的元素。

Since you want to have your TreeSet contain BitSet s, and BitSet does not implement Comparable , you need to give your TreeSet a custom Comparator . 由于您希望TreeSet包含BitSet ,并且BitSet不实现Comparable ,因此您需要为TreeSet提供自定义Comparator How you implement that Comparator is up to you. 你如何实现Comparator取决于你。

SortedSet<BitSet> s = new TreeSet<BitSet>(new CustomBitSetComparator());
s.add(bitSet1);
s.add(bitSet2);
//etc ...

The Comparator may look something like this 比较器可能看起来像这样

class CustomBitSetComparator implements Comparator<BitSet>{
    int compare(BitSet a, BitSet b) {
        if(a == b){
            return 0;
        } else if(a == null) {
            return -1;
        } else if(b == null) {
            return 1;
        } else if(a.equals(b)) {
            return 0;
        } else if(a.length() > b.length()) {
            return 1;
        } else if(b.lenght() > a.length()) {
            return -1;
        } else {
            for(int i = 0; i < a.length(); i++) {
               if(a.get(i) != b.get(i)) {
                   if(a.get(i)) {
                      return 1;
                   } else {
                      return -1;
                   }
                }
             }
             return 0;
         }
    }
}

I would convert them to BigIntegers (O(N)) and use a TreeSet. 我会将它们转换为BigIntegers(O(N))并使用TreeSet。 Otherwise you will have to write yourself a Comparator, which in the nature of things will run excruciatingly slowly, as you can see from other answers. 否则你将不得不自己写一个比较器,就其他答案而言,从性质的角度来说,这将会非常缓慢地运行。 I would also consider using PriorityQueue instead of a Set. 我还会考虑使用PriorityQueue而不是Set。

I'm not sure why you want to put BitSet in a treeSet, while a workaround is to create a wrapper class which implements Comparable interface. 我不确定为什么要将BitSet放在treeSet中,而解决方法是创建一个实现Comparable接口的包装类

Public class CustomComparableBitSet implements Comparable{
   private BitSet bs;

   public int compareTo(T o){
      //your comparison logic goes here.
   }
}

then in the client code, add instance of CustomComparableBitSet into treeset . 然后在客户端代码中,将CustomComparableBitSet的实例添加到treeset中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用Java中的值创建类似类型驱动选择的内容(例如,图片) - How to create something like a type driven selection with values in java (e.g. picture) 如何在SortedSet &lt;&gt;上定义比较器,如TreeSet &lt;&gt;? - How to define comparator on SortedSet<> like TreeSet<>? 如何检查数组的所有元素是否不等于某个值? (例如,不是空的) - How can I check if all elements of array doesn't equal to some value? (e.g. not empty) Java集合类型不匹配:无法从TreeSet转换为SortedSet - Java collection Type mismatch: cannot convert from TreeSet to SortedSet Java编译器如何处理类型之外的缺失类型参数 <E> ,例如 <E extends Foo> - How does the java compiler treat missing type arguments for types besides <E>, e.g., <E extends Foo> Jetty中的哪些元素是可插拔的? 例如会话cookie - What elements in Jetty are pluggable and removale? e.g. session cookies 不兼容的类型-SortedSet和TreeSet - Incompatible types - SortedSet and TreeSet 如何为类型参数的类调用.class? 例如清单 <String> 。类 - How to call .class for a class with type parameter? E.g. List<String>.class 如何获取“ Enum.CONSTANT”以返回某种类型(例如,颜色)? - How do I get “Enum.CONSTANT” to return some type (e.g. Color)? 如何为Android应用创建标题屏幕? 例如,当应用程序像Facebook一样加载时 - How do you create a title screen for an android app? E.g. When the app is loading like Facebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM