我有一个排序ArrayList
的联系人。我想将列表存储在SharedPreference
中。我试过以下代码:
SharedPreferences.Editor editor = app_preference.edit();
Set<String> set = new HashSet<String>();
set.addAll(contact_names_list);
editor.putStringSet("CONTACT_LIST", set);
editor.apply();
问题是,当我从HashSet
检索这个时,我得到了一个未排序的列表。除ArrayList
以外还有其他方式存储Hashset
吗?
答案 0 :(得分:0)
检索值
Set<String> set = myScores.getStringSet("CONTACT_LIST", null);
答案 1 :(得分:0)
static Prefs singleton = null;
public static Prefs with(Context context) {
if (singleton == null) {
singleton = new Builder(context).build();
}
return singleton;
}
这样做 - :
SharedPreferences.Editor editor = app_preference.edit();
Set<String> set = new HashSet<String>();
set.addAll(contact_names_list);
editor.putStringSet("CONTACT_LIST", set);
editor.apply();
or
Prefs.with(getAppContext()).putStringSet("CONTACT_LIST", set);
使用此代码重写值 - :
Set<String> existindData = Prefs.with(getAppContext()).getStringSet("CONTACT_LIST", null);
答案 2 :(得分:0)
由于putStringSet
接受任何实现Set
接口的类
您可以使用保留插入顺序的LinkedHashSet
。
LinkedHashSet的文档:
这个实现与HashSet的不同之处在于它维护了一个 贯穿其所有条目的双向链表。这有联系 list定义迭代排序,即顺序 元素被插入到集合中(插入顺序)。
HashSet
不会保留插入顺序,这就是问题所在。
// ...
Set<String> set = new LinkedHashSet<String>();
// insert from ArrayList
set.addAll(contact_names_list);
// or single elements
set.add("Homer");
set.add("Marge");
editor.putStringSet("CONTACT_LIST", set);
答案 3 :(得分:-1)
正如Android文档解释https://developer.android.com/training/data-storage/shared-preferences.html?hl=en一样,共享首选项是一种保存键值数据的方法。鉴于场景背后的字典可以几乎以随机顺序保存,您无法按顺序检索某些内容。但是我可以推荐几种解决方案:
将数据保存在本地SQLite数据库中:此解决方案在以后的术语中更加灵活(您可以添加新的联系人)。 https://developer.android.com/training/data-storage/sqlite.html通常您创建自己的SQLite数据库,然后将其导入资产中。您可以创建查询以按照您想要的顺序(可能为其创建一个列)。
InputStream myInput = context.getAssets( ).open( "test.db" );
// Path to the just created empty db
String outFileName = "/data/YOUR_APP/test.db";
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream( outFileName );
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while( ( length = myInput.read( buffer ) ) > 0 )
{
myOutput.write( buffer, 0, length );
}
// Close the streams
myOutput.flush( );
myOutput.close( );
myInput.close( );
简单不可扩展选项:在值folder
中,您始终可以创建strings.xml
文件,您可以将该数据设为string-array
。 https://developer.android.com/guide/topics/resources/string-resource.html?hl=en
<string-array name="types">
<item>A</item>
<item>B and users</item>
<item>C</item>
<item>D</item>
<item>E</item>
</string-array>
如果你需要更结构化的东西,我已经做过这样的事情(保存为JSON)。
<string-array name="quality_attributes">
<item>{id:1,name:Performance,sub:[{id:2,name:Scalability},{id:3,name:Response_Time}]}</item>
<item>{id:4,name:Security,sub:[{id:5,name:Availability,sub:[{id:6,name:Reliability},{id:7,name:Recovery}]},{id:8,name:Privacy}]}</item>
<item>{id:9,name:Interoperability}</item>
<item>{id:10,name:Maintainability}</item>
<item>{id:12,name:Testability}</item>
<item>{id:13,name:Usability,sub:[{id:14,name:Findability},{id:15,name:Correctness}]}</item>
</string-array>
在我的代码中:
String[] elementsArray = getResources().getStringArray(
R.array.quality_attributes);
for (int i = 0; i < attributesArray.length; i++) {
Gson gson = new Gson();
MyJsonObject obj = gson.fromJson(elementsArray[i],
MyJsonObject.class);
// Do something
}
一般而言,如果您需要更改某些内容,则第二个选项是最佳选项。如果你正在寻找简单的东西,第三种选择就足够了。
答案 4 :(得分:-2)
我们可以使用Java 8排序方式对集合进行排序。
Set<String> s = new HashSet<String>();
s.add("Kunal");
s.add("Bhism");
s.add("Work");
s.add("Abdgg");
s.add("Psss");
s.add("Work");
List<Object> l = s.stream().sorted().collect(Collectors.toList());
System.out.println(l);
试试上面这段代码,你现在就会得到排序好的HashSet。