1

When declaring a map, you can choose String or CharSequence;

Map<String, String> lexicalizationGraph = ChronicleMap
    .of(String.class, String.class)
    .name("lexicalizations-chronicle-map")
    .entries(1_000_000_000L)
    .constantKeySizeBySample("bn:14271053n")
    .createPersistedTo(file);

Is there any important difference between String and CharSequence?

  • If this is the same CharSequence that Chronicle Map uses, then yes. –  Jan 17 '20 at 11:19
  • 2
    @Tschallacka this question is not a duplicate of the generic question about the difference between String and CharSequence in Java. Chronicle Map [serializes data off-heap](https://github.com/OpenHFT/Chronicle-Map/blob/master/docs/CM_Tutorial.adoc#key-and-value-types) and tries to reuse objects upon access, so choosing key and value types has some implications specific to this framework. – leventov Jan 28 '20 at 17:26
  • 2
    When you define a ChronicleMap with CharSequence as a value and then use [`getUsing()`](https://github.com/OpenHFT/Chronicle-Map/blob/master/docs/CM_Tutorial.adoc#chroniclemapgetusing) method, `acquireUsing()`, or `get()` / `getUsing()` [within a context](https://github.com/OpenHFT/Chronicle-Map/blob/master/docs/CM_Tutorial.adoc#working-with-an-entry-within-a-context) and pass a `StringBuilder` into these methods then you can save an object allocation on heap. So using `CharSequence` value is beneficial if you are willing to write a little more boilerplate code. – leventov Jan 28 '20 at 17:33

2 Answers2

-1

CharSequence is an Interface that String implements, other classes that implement CharSequence are CharBuffer, Segment, StringBuffer, StringBuilder.

So if you want to map String specifically, use that, or if you want any type of CharSequence, use that.

mwarren
  • 759
  • 3
  • 6
-1

The difference between CharSequence and String in Java

CharSequence is an interface that represents a sequence of characters. Mutability is not enforced by this interface. Therefore, both mutable and immutable classes implement this interface.

String is a sequence of characters in Java. It is an immutable class and one of the most frequently used types in Java. This class implements the CharSequence, Serializable, and Comparable interfaces.

Since String is immutable every operation on it will create a new String for example

String test = "a";
test = test + "B";

in this scenario you've created two different string

if you use StringBuffer StringBuilder because they 're both mutable, in this case you're creating only one instance

StringBuilder test = new StringBuilder("a");
test.append("B");

you can choose CharSequence just means you can choose CharBuffer, Segment, StringBuffer, StringBuilder.

Karim
  • 1,004
  • 8
  • 18