您好,我有两个字符串集,我想获取当前存在的索引,如下所示:
我想在第二组中使用第一组中相同的索引,这样我就可以在其他地方使用这些字符串,因此如果我拥有的标记是“VatU5fy1U4cgTc3uBBiGX7G7kuT2”,那么相关的出生日期将是“2011 年 2 月 8 日”。 (参见下面的输出)。
现在我知道集合是无序的,并且无法使用索引,因为我正在使用共享首选项中的方法,例如 getStringSet,它仅检索集合而不是数组中的字符串。
我尝试将集合转换为数组,但每次使用索引时,我总是返回 -1,其中找不到字符串! :(
这是我当前的代码(非常标准并且工作完美),用于日期的共享首选项,这与其他 token 集相同:
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final SharedPreferences.Editor editor = sharedPreferences.edit();
Set<String> myDates = sharedPreferences.getStringSet("myDates", new HashSet<String>());
String dates = sharedPreferences.getString("savedDate", "");
// Add the new value.
myDates.add(dates);
// Save the list.
editor.putStringSet("myDates", myDates);
editor.commit();
Log.w(TAG, "Onexit value of dates in array: " + String.valueOf(myDates));
输出:
Onexit value of tokens in array: [Ztg4GOqeucd6CqNAEokUe7eRHhY2, VatU5fy1U4cgTc3uBBiGX7G7kuT2]
Onexit value of dates in array: ["8th February 2012", "8th February 2011"]
以下是我尝试的将这些列表转换并视为数组的解决方案:
**//Attempt 1**
Set<String> myDates = sharedPreferences.getStringSet("myDates", new
HashSet<String>());
List<String> myDatesList = new ArrayList <String> (myDates);
int i = Arrays.asList(myDatesList).indexOf(dates);
myDates.add(dates);
List<String> myDatesList = new ArrayList <String> (myDates);
editor.putStringSet("myDates", myDates);
editor.commit();
int i = Arrays.asList(myDatesList).indexOf(dates);
返回-1 ^
**//Attempt 2**
List<String> myDatesList = new ArrayList <String> (myDates);
String datesString = TextUtils.join(";", myDatesList);
editor.putString("newdates", datesString);
editor.apply();
final String datesString = sharedPreferences.getString("newdates", "");
String dates = sharedPreferences.getString("savedDate", "");
List<String> myDatesList = new ArrayList<>();
editor.commit();
if (!datesString.isEmpty()){
myDatesList = new ArrayList<>(Arrays.asList(datesString.split(";")));
}
String datesString = TextUtils.join(";", myDatesList);
editor.putString("newdates", datesString);
editor.apply();
int i = Arrays.asList(datesString).indexOf(dates);
**returns -1** ^
**回复答案 - 更新**
回应评论:(我更改了日志)
int i = myDatesList.indexOf(datesString);
Log.w(TAG, "My dates list: " + dates + " and datesString is: " + datesString + i);
给我:
My dates list: "9th August 2017" and datesString is: "9th August 2017"0
My dates list: "9th February 2011" and datesString is: "9th August 2017";"9th February 2011"-1
为什么第二次给我的是 -1 而不是 1?
最佳答案
Arrays.asList()
接受可变参数,并不用于复制 Collection
类型,但用于创建 List<T>
哪里T
是作为参数传入的对象的类型。
当您满足以下条件时,就会发生以下情况:
// Contains each element from myDates
List<String> myDatesList = new ArrayList<String>(myDates);
并调用Arrays.asList(myDatesList).indexOf(dates)
,那是什么Arrays.asList()
返回的调用是 List<List<String>>
的实例。
// This is what's actually being returned:
List<List<String>> result = Arrays.asList(myDatesList);
在这种情况下,请调用 indexOf()
与 String
type 永远不会找到匹配项(因为 String
永远不会等于 List<String>
。事实上,这个 String
参数应该在您的 IDE 中显示为警告,指示 List<List<String>> may not contain objects of type String
。
您已经拥有 List<String>
( myDatesList
) 因此无需调用 Arrays.asList()
因为这是为了创建一个 List
传入的类型。只需调用 myDatesList.indexOf(dateString)
获取实际索引。
关于java - IndexOf 两个 HashSet 字符串 - 共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48696449/