Is there something like a TStringList from Delphi in the Java (Android) language?
I am starting to develop in Android and decided to have a go at the native java language.
When sta开发者_开发技巧rting to use new languages, I tend to create a library of code creating routines with the same name which perform the same function as in the libraries I already have.
F.i. in my Classic Asp library I have a IntToStr() ... function which is an equivalent of the IntToStr() of Delphi, although I know that VBScript does not need such a function - it makes my code in the different languages much more readable and thus make it easier to switch between languages.
Coming from a Delphi background, I have a lot of library code in Delphi which uses TStringList classes.
Is there anything equivalent in Java ? Or how would you solve this ?
A delphi StringList looks to my like a list of key/value pairs. Java has structures for collections of key/value pairs (Map
), but the implementation TreeMap
and HashMap
do not keep the insertion order of the keys. A good start for a custum Java datatype could be:
public class TStringList { // unconventional Java name, but you like to keep the delphi names
private class Entry {
String key;
Object value; // to keep it flexible
}
private List<Entry> entries = new ArrayList<Entry>();
// add some constructors
// implement methods of delphis StringList
}
(Note: the stub does not implement a JCA interface (like List
) because the class is intended to mimic a delphi type and not to act as a collection)
Is it a simple List of Strings? If yes, use generics instead:
LinkedList<String> myStrings = new LinkedList<String>();
myStrings.add("hello");
String s = myStrings.get(0);
This is what you need:
org.apache.commons.collections.map.ListOrderedMap
I doubt there's a exact replacement for TStringList, because it is very flexible and does not correspond to one exact data structure class on java Containers - actually most TStringList's functionality have to be done using some other classes. Two years ago I implemented in Java a TStringList object. There's capable classes in java that can help you. I don't know if I still have that source, was only an experiment.
精彩评论