Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hashcode新手,需要帮助理解代码片段_Java - Fatal编程技术网

Java hashcode新手,需要帮助理解代码片段

Java hashcode新手,需要帮助理解代码片段,java,Java,我有下面的代码片段,需要一些帮助来理解其中的一些部分 String fileName = UUID.randomUUID().toString(); int hashcode = fileName.hashCode(); //I'm not sure why we are creating a mask with an int of 255 int mask = 255; //Wny are we adding 255 to the hashcode? int firstDir = has

我有下面的代码片段,需要一些帮助来理解其中的一些部分

String fileName = UUID.randomUUID().toString();

int hashcode = fileName.hashCode();

//I'm not sure why we are creating a mask with an int of 255
int mask = 255;

//Wny are we adding 255 to the hashcode?
int firstDir = hashcode & mask;

//What does it mean to have (hashcode >> 8) & mask? If I were to build a third
// tier would I use (hashcode >> 16) $ mask?
int secondDir = (hashcode >> 8) & mask;

StringBuilder sb = new StringBuilder(File.separator);

//I noticed when using this %02 it truncates the directory to 2 chars, does this 
//just convert 3 digits to alpha numeric representing the same three digits?
sb.append(String.format("%02x", firstDir));

sb.append(File.separator);
sb.append(String.format("%02x", secondDir));
sb.append(File.separator);

最后,如果我想从这两个目录生成一个文件名,我会只设置另一个没有file.separator的字符串生成器,还是先创建没有file.separator的字符串,然后拆分字符串更有效?

这段代码太傻了

如果您想创建两个随机分布的两位数十六进制代码,这些代码是从随机UUID派生的,那么您可以只使用UUID本身的前四位十六进制数字

String fileName = UUID.randomUUID().toString();
String firstDir = fileName.substring(0,2);
String secondDir = fileName.substring(2,4);

随机UUID是一个加密强随机二进制字符串(表示这是一个类型4 UUID的除外)。任何哈希、位移位或掩蔽都会降低随机性。

255是0FF十六进制,0 1111 1111二进制

与“and”运算符(“&”)一起使用的掩码用于隔离掩码与之and的值的位——与上述掩码and的整数将生成一个与原始整数具有相同最低8位的整数

通过>>8输入的整数向右移位8位;然后用相同的掩码进行anding,以隔离这8位,这8位作为原始整数中的下一个高阶8位开始


不要担心效率,除非你能证明几微秒会带来不同。担心使您的代码足够容易理解,而不必发布到stackoverflow来理解它。

首先去阅读
&
>
操作符。@SotiriosDelimanolis以及十六进制和格式字符串
&
(AND)是一个-在尝试使用它们之前,先阅读并准确理解它们的工作原理。我同意你们所有人的意见,我确实做了一次谷歌搜索,可以找到垃圾,这就是求助于此处寻求额外帮助的原因。使用您建议的逻辑,这是否仍然允许每个目录最多包含1000个项目?对于散列目录层,这两种情况下都是256个项目。00至FF。例如,
12/34/
下的文件数量可能是无限的(但不太可能达到非常高的数量)。与您的原始代码相同。我想我将使用您的解决方案,它似乎比我在网上找到的上述代码简单得多。我还有最后一个问题,我刚刚做了一个测试,测试了一百万个文件的文件分发,我从来没有在同一个目录中看到过两个文件使用这样的东西\9d\62\f35a.jpg。有没有可能因为目录太多而导致性能问题?两层中间目录可能有点过头了。也许一个就够了。但性能影响应该可以忽略不计。肯定比没有这些目录要好。但是有一百万个文件,你必须有一个至少有两个文件的目录:255*255不到一百万。谢谢Thilo,自从他回答了我最初的问题后,我不得不将rcook标记为最佳答案,但我使用你的解决方案,因为它似乎是解决我最初问题的更干净的解决方案。