小编jan*_*jan的帖子

Java:使用Collat​​orKey对集合进行排序

我想要实现的是通过字符串值对对象的集合进行排序.但是,使用collat​​or以依赖于语言环境的方式.由于性能原因,我不想使用Collat​​or compare()方法(如下面的代码中)而不是Collat​​ionKey类,因为java API声明使用Collat​​ionKey要快得多.

但是如何使用Collat​​ionKey实现compareTo()方法?据我所知,如果我将使用Collat​​ionKey,我必须自己完全编写所有的比较方法.所以我甚至不再能够使用Collections.sort()方法......我非常感谢一个易于理解的示例,以及使用Collat​​ionKey对Person对象的Collection进行排序的最有效实现.

谢谢!

public class Person implements Comparable<Person> {

String lastname;

public int compareTo(Person person) {
     //This works but it is not the best implementation for a good performance
     Collator instance = Collator.getInstance(Locale.ITALY);
     return instance.compare(lastname, person.lastname);
}
}

...
ArrayList list = new ArrayList();
Person person1 = new Person("foo");
list.add(person1);
Person person2 = new Person("bar");
list.add(person2);
Collections.sort(list);
...
Run Code Online (Sandbox Code Playgroud)

java comparable

7
推荐指数
1
解决办法
5726
查看次数

如何最好地表示数据库中的常量(枚举)(INT与VARCHAR)?

什么是性能方面的最佳解决方案和"可读性/良好的编码风格"来表示DB层上的(Java)枚举(固定的常量集)关于整数(或一般的任何数字数据类型)与字符串的关系表示.

警告:有些数据库系统直接支持"枚举",但这需要保持数据库枚举定义与业务层实现同步.此外,这种数据类型可能并非在所有数据库系统上都可用,并且语法可能不同=>我正在寻找一种易于管理且易于在所有数据库系统上使用的简单解决方案.(所以我的问题只是解决了Number vs String表示.)

在我看来,常量的Number表示非常有效(例如,仅消耗两个字节作为整数),并且在索引方面很可能非常快,但难以阅读("0"与"1"等). .

字符串表示更具可读性(与"0"和"1"相比,存储"已启用"和"已禁用"),但消耗的存储空间很多,并且在索引方面很可能也较慢.

我的问题是,我是否错过了一些重要方面?您建议在数据库层上使用枚举表示法.

非常感谢你!

java database constants

5
推荐指数
1
解决办法
2263
查看次数

在Java构造函数中添加"this"对Array的引用失败

我绝对无能为力,为什么以下代码不断抛出NullpointerExceptions.我无法理解或调试它(从较大的类中删除它的代码)...

代码基于"枚举模式",我想保留类中包含的所有"常量"的列表/映射(我可能正在使用Reflection,但使用List/Map更容易... )

   public class Country {

        public static final Country SWITZERLAND = new Country("SWITZERLAND");

        private static ArrayList<Country> countries = new ArrayList<Country>();

        private  Country(String constname) {     
             //constname is currently not used (I will use it for a Key in a Map)
            System.out.println(constname);
            System.out.println("Ref debug:"+this);

            //Ad this to the Countries
            Country.countries.add(this);
        }
    }
Run Code Online (Sandbox Code Playgroud)

非常感谢帮助.我在这里错过了什么?

java constructor

0
推荐指数
1
解决办法
387
查看次数

标签 统计

java ×3

comparable ×1

constants ×1

constructor ×1

database ×1