我有數組列表的自定義對象類型,我想搜索的字符串是存在於對象的值,那我怎麼才能找到它指出,反對那些 有搜索字符串如何在自定義對象類型數組列表中搜索?什麼是最好和最快的方式
回答
你可以嘗試一些像這樣:
package com.company;
import java.util.ArrayList;
import java.util.List;
public class Test {
private String val;
public Test(String s) {
this.val = s;
}
public static void main(String[] args) {
List<Test> values = new ArrayList<>();
values.add(new Test("one"));
values.add(new Test("two"));
values.add(new Test("three"));
System.out.println(listContains(values, "two"));
System.out.println(listContains(values, "five"));
}
public static boolean listContains(List<Test> customTypeList, String searchedString) {
return customTypeList.stream().anyMatch((v) -> v.val.contains(searchedString));
}
}
如果你在你的列表(子不感興趣)尋找最快的解決方案並搜查字符串正是從對象的值,那麼你可能想從你的對象的字符串映射到這些對象的這樣的參考資料:
(...)
List<Test> values = new ArrayList<>();
values.add(new Test("one"));
values.add(new Test("two"));
values.add(new Test("three"));
Map<String, Test> indices = new HashMap<>();
for (Test v : values) {
indices.put(v.val, v);
}
System.out.println(indices.containsKey("two"));
System.out.println(indices.containsKey("five"));
// or...
System.out.println(indices.keySet().contains("two"));
System.out.println(indices.keySet().contains("five"));
(...)
重要提示:只要您更改列表的內容,就需要更新索引。請注意,在這種情況下,對象內部的字符串值必須是這些對象的有效鍵(唯一值)。例如:
public static void main(String[] args) {
List<Test> values = new ArrayList<>();
Map<String, Test> indices = new HashMap<>();
addToList(values, indices, new Test("one"));
addToList(values, indices, new Test("two"));
addToList(values, indices, new Test("three"));
System.out.println(indices.keySet().contains("two"));
System.out.println(indices.keySet().contains("five"));
removeFromList(values, indices, "two");
System.out.println(indices.keySet().contains("two"));
}
private static void addToList(List<Test> values, Map<String, Test> indices, Test item) {
values.add(item);
indices.put(item.val, item);
}
private static void removeFromList(List<Test> values, Map<String, Test> indices, String key) {
Test item = indices.remove(key);
values.remove(item);
}
沒有將方法命名爲stream()數組列表 –
In Java 8有: java.util.Collection => public java.util.stream.Stream
我有Java(TM)SE運行環境(build 1.8.0_92-b14),它不給Array列表上的任何方法流 –
- 1. 什麼是最好的方式來存儲和搜索類對象(在PHP中)?
- 2. 返回C#中對象列表的最快方式是什麼?
- 3. 什麼是刪除模型對象數組的最快方法?
- 4. 什麼是最好的方式來搜索SQL與多個表?
- 5. 在數組數組中搜索兩件事物的最快方法是什麼?
- 6. 什麼是最好的方式遍歷一個數組列表
- 7. 什麼是在我的網站上實現良好搜索的最快方式?
- 8. 什麼類型是最好的在C#中鬆散的數字索引列表?
- 9. 什麼是最好的QT列表和表格對象
- 10. 獲取Google搜索網址列表的最快方式是什麼?
- 11. 什麼是搜索網絡位置的最快捷方式?
- 12. 什麼是全文搜索的最快方式?
- 13. 什麼是在二維數組中搜索字符串的最快方法
- 14. 搜索和更新整數值列表的最快數據結構是什麼?
- 15. 保存每個包含自定義對象數組的自定義對象數組,最好的方法?
- 16. 存儲大型對象/數組進行歸檔,低訪問和搜索的最佳方式是什麼?
- 17. 什麼是最好,最快捷的學習方式Drupal 7
- 18. 如何在JavaScript中最好地搜索和更新兩個對象數組?
- 19. 什麼是簡單合併文件的最快捷方式,什麼是分割數組的最快捷方式?
- 20. 在Java中搜索堆棧中最大元素的最快方法是什麼?
- 21. 什麼是存儲和搜索對象事務的最佳方式?
- 22. 用jQuery搜索JavaScript對象列表的最簡單方法是什麼?
- 23. 在Java中自定義排序的最佳方式是什麼?
- 24. 什麼是在javascript中定義類的最佳方式
- 25. 什麼是Java中最快的子串搜索方法
- 26. 什麼是包裝搜索區域的最佳語義方式?
- 27. 什麼是獲取文件列表和重複搜索文件列表的最快方式?
- 28. 什麼是存儲持久對象數組的最佳方式?
- 29. 什麼是在JavaScript上循環數組的最快方式?
- 30. 向後走過Excel Range對象的最快方式是什麼?
請創建MCVE向我們展示你嘗試過什麼http://stackoverflow.com/help/mcve的 –
可能的複製【JAVA:查找數組列表字符串(http://stackoverflow.com/questions/32304214/java-find-string-in-array-list) – brianlmerritt
這些答案不是我的解決方案我想用最快的方式在數組列表中使用HashSet進行搜索 –