什么时候有益于Java中的flyweight Strings? [英] When is it beneficial to flyweight Strings in Java?

查看:149
本文介绍了什么时候有益于Java中的flyweight Strings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解了java的String实习的基本概念,但我正在设计出发生的情况,



有点相关:





他们一起告诉我, String s =foo是好的, String s = new String(foo)是坏的,但没有提到任何其他情况。 p>

特别是,如果我解析一个具有很多重复值的文件(比如csv),Java的字符串将在掩护我,还是需要自己去做?关于String实习是否适用于我的其他问题,我已经得到了相互矛盾的建议






完整答案有几个片段,所以我来总结一下:



默认情况下,java只有实习生在编译时已知的字符串。 String.intern(String)可以在运行时使用,但是效果不好,所以只适用于较小数量的 String s,你肯定会重复很多。对于较大的字符串,它是Guava的救援(见ColinD的答案)。

解决方案

不要使用String.intern()在你的代码至少不能,如果你可能得到20个或更多不同的字符串。根据我使用 String.intern 的经验,当您有几百万个字符串时,会减缓整个应用程序。



为避免重复的 String 对象,只需使用 HashMap

  private final Map< String,String> pool = new HashMap< String,String>(); 

private void interned(String s){
String interned = pool.get(s);
if(interned!= null){
return interned;
pool.put(s,s);
return s;


$ b private void readFile(CsvFile csvFile){
for(List< String> row:csvFile){
for(int i = 0; i& row.size(); i ++){
row.set(i,interned(row.get(i)));
//进一步处理行
}
}
pool.clear(); //允许垃圾收集器清理
}

使用该代码可以避免重复一个CSV文件的字符串。如果您需要更大规模地避免这些问题,请在另一个地方调用 pool.clear()


I understand the basic idea of java's String interning, but I'm trying to figure out which situations it happens in, and which I would need to do my own flyweighting.

Somewhat related:

Together they tell me that String s = "foo" is good and String s = new String("foo") is bad but there's no mention of any other situations.

In particular, if I parse a file (say a csv) that has a lot of repeated values, will Java's string interning cover me or do I need to do something myself? I've gotten conflicting advice about whether or not String interning applies here in my other question


The full answer came in several fragments, so I'll sum up here:

By default, java only interns strings that are known at compile-time. String.intern(String) can be used at runtime, but it doesn't perform very well, so it's only appropriate for smaller numbers of Strings that you're sure will be repeated a lot. For larger sets of Strings it's Guava to the rescue (see ColinD's answer).

解决方案

Don't use String.intern() in your code. At least not if you might get 20 or more different strings. In my experience using String.intern slows down the whole application when you have a few millions strings.

To avoid duplicated String objects, just use a HashMap.

private final Map<String, String> pool = new HashMap<String, String>();

private void interned(String s) {
  String interned = pool.get(s);
  if (interned != null) {
    return interned;
  pool.put(s, s);
  return s;
}

private void readFile(CsvFile csvFile) {
  for (List<String> row : csvFile) {
    for (int i = 0; i < row.size(); i++) {
      row.set(i, interned(row.get(i)));
      // further process the row
    }
  }
  pool.clear(); // allow the garbage collector to clean up
}

With that code you can avoid duplicate strings for one CSV file. If you need to avoid them on a larger scale, call pool.clear() in another place.

这篇关于什么时候有益于Java中的flyweight Strings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆