use*_*349 5 java algorithm stringbuilder utf-8 data-structures
我有一个tasks列表对象,我正在迭代并附加每个任务对象,StringBuilder然后是新行,如下所示.现在我将继续在同一个字符串生成器中追加任务对象,直到它达到60000字节的大小限制.一旦达到限制,我将填充此字符串作为映射中的值,键将是带增量索引的文件名.然后我将重置字符串构建器和其他东西并再次重复此过程.
因此,如果我有一个大tasks对象,那么我将拆分成多个字符串对象,其大小应始终小于60000字节.
我得到了下面的代码,但我总是看到地图中的值大小超过60000字节.我做错了什么?此外,我正在填写HashMap两个不同的地方 - 一个达到限制,另一个是如果没有达到限制.
public void populate(final List<Task> tasks) {
Map<String, String> holder = new HashMap<>();
int size = 0;
int index = 0;
StringBuilder sb = new StringBuilder();
for (Task task : tasks) {
sb.append(task).append(System.getProperty("line.separator"));
size = sb.toString().getBytes(StandardCharsets.UTF_8).length;
if (size > 60000) {
String fileName = "tasks_info_" + index + ".txt";
holder.put(fileName, sb.toString());
index++;
sb = new StringBuilder();
size = 0;
}
}
// for cases where we don't reach the limit
if(sb.toString().length > 0) {
String fileName = "tasks_info_" + index + ".txt";
holder.put(fileName, sb.toString());
}
System.out.println(holder);
}
Run Code Online (Sandbox Code Playgroud)
注意:如果每个Task对象都超过60000 bytes,那么我将立即删除该对象并移动到下一个条目.但实际上,它不会发生.
更新:
public void populate(final List<Task> tasks, final long timestamp) {
Map<String, String> holder = new HashMap<>();
int size = 0;
int index = 0;
int nl = System.getProperty("line.separator").getBytes(StandardCharsets.UTF_8).length;
StringBuilder sb = new StringBuilder();
// new change
sb.append(timestamp).append(System.getProperty("line.separator"));
for (Task task : tasks) {
int ts = String.valueOf(task).getBytes(StandardCharsets.UTF_8).length;
if (size + ts + nl > 60000) {
String fileName = "tasks_info_" + index + ".txt";
holder.put(fileName, sb.toString());
index++;
sb = new StringBuilder();
// new change
sb.append(timestamp).append(System.getProperty("line.separator"));
size = 0;
}
sb.append(task).append(System.getProperty("line.separator"));
size += ts + nl;
}
// for cases where we don't reach the limit
if (size > 0) { // size can only be 0 if you have 0 tasks
String fileName = "tasks_info_" + index + ".txt";
holder.put(fileName, sb.toString());
}
System.out.println(holder);
}
Run Code Online (Sandbox Code Playgroud)
其他答案已经提到了它不起作用的原因(您在超过限制后添加)。但我认为到目前为止,没有一个实现是正确的,不仅仅是因为省略了换行符的大小。
public Map<String, String> populate(final List<Task> tasks) {
Map<String, String> holder = new HashMap<>();
if (tasks.size() == 0)
return holder;
int index = 0;
int nl = System.getProperty("line.separator").getBytes(StandardCharsets.UTF_8).length;
StringBuilder sb = new StringBuilder();
sb.append(System.currentTimeMillis()).append(System.getProperty("line.separator"));
int size = sb.toString().getBytes(StandardCharsets.UTF_8).length;
for (Task task : tasks) {
int ts = String.valueOf(task).getBytes(StandardCharsets.UTF_8).length;
if (size + ts + nl > 60000) {
String fileName = "tasks_info_" + index + ".txt";
holder.put(fileName, sb.toString());
index++;
sb = new StringBuilder();
sb.append(System.currentTimeMillis()).append(System.getProperty("line.separator"));
size = sb.toString().getBytes(StandardCharsets.UTF_8).length;
}
sb.append(task).append(System.getProperty("line.separator"));
size += ts + nl;
}
String fileName = "tasks_info_" + index + ".txt";
holder.put(fileName, sb.toString());
return holder;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
220 次 |
| 最近记录: |