问题
From the documentation: Thrift translates a SET container to HashSet by default. Can we change this behavior?
Containers Thrift containers are strongly typed containers that map to commonly used and commonly available container types in most programming languages.
https://thrift.apache.org/docs/types:
There are three container types: list: An ordered list of elements. Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc. set: An unordered set of unique elements. Translates to an STL set, Java HashSet, set in Python, etc. Note: PHP does not support sets, so it is treated similar to a List
回答1:
The sorted_containers
option switches to
TreeMap
formap<>
TreeSet
forset<>
The option is to be specified when calling the Thrift compiler, like so:
$ thrift -gen java:sorted_containers myfile.thrift
Here are all options available for Java (curent master branch, thrift -help
reveals more info)
java (Java):
beans: Members will be private, and setter methods will return void.
private-members: Members will be private, but setter methods will return 'this' like usual.
nocamel: Do not use CamelCase field accessors with beans.
fullcamel: Convert underscored_accessor_or_service_names to camelCase.
android: Generated structures are Parcelable.
android_legacy: Do not use java.io.IOException(throwable) (available for Android 2.3 and above).
option_type: Wrap optional fields in an Option type.
java5: Generate Java 1.5 compliant code (includes android_legacy flag).
reuse-objects: Data objects will not be allocated, but existing instances will be used (read and write).
sorted_containers:
Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of set/map.
generated_annotations=[undated|suppress]:
undated: suppress the date at @Generated annotations
suppress: suppress @Generated annotations entirely
Other options are not available. The C++ generator supports annotations, this seems not to be the case with Java. Did I mention that we accept patches?
来源:https://stackoverflow.com/questions/34341492/apache-thrift-can-we-instruct-thrift-to-translate-a-set-container-to-java-linke