[英]How do I get the key from Map in a List and do a get rest request in Java
我想做一個獲取請求,並希望獲得所有有“老師”的工作。 在這種情況下,map 值 2 和 3。
如果我執行下面的代碼然后我只從 Map 中獲取值,我該如何到達那里 2. 最好的方法是通過列表? 但是我該怎么做呢?
我有這個方法。
@GET
@Path("jobs")
public Job getJobsId(@QueryParam("id") int id, @QueryParam("description") String description) {
for (final Map.Entry<Integer, Job> entry : jobs.entrySet()) {
if (entry.getValue().getDescription().toLowerCase().contains(description.toLowerCase())) {
System.out.println("I DID IT");
System.out.println(entry.getKey());
return berufe.get(entry.getKey());
}
}
return berufe.get(id);
}
和這個 Map:
jobs.put(1, new jobs(1, 1337, "student"));
jobs.put(2, new jobs(2, 420, "teacher"));
jobs.put(3, new jobs(3, 69, "schoolteacher"));
- - - - - - - - - - - - - - - - -編輯 - - - - - - - - ------------------
如果我這樣做:
@GET
@Path("jobs")
public Collection<Job> getJobsId(@QueryParam("id") int id, @QueryParam("description") String description) {
final Set<Beruf> result = new HashSet<>();
for (final Map.Entry<Integer, Job> entry : jobs.entrySet()) {
if (entry.getValue().getDescription().toLowerCase().contains(description.toLowerCase()) == true) {
result.add(jobs.get(entry.getKey()));
} else {
return jobs.values();
}
}
return result;
}
我得到了所有 Map 值的描述,但沒有收到錯誤。
我在這里做錯了什么?
您的方法getJobsId()
返回它找到的第一個項目(在if
語句內),這就是為什么您只得到一個結果的原因。
收集大量結果的通常模式是在for
循環之前實例化一個合適的Collection
,並將找到的每個項目添加到該集合中。 然后,在for
循環之后,返回集合。 我不完全理解你的代碼,但它會類似於下面的東西(我相信如果你只是復制粘貼它不會工作,所以閱讀並理解發生了什么;-)):
Set<Beruf> result = new HashSet<>();
for (final Map.Entry<Integer, Job> entry : jobs.entrySet()) {
if (entry.getValue().getDescription().toLowerCase().contains(description.toLowerCase())) {
result.add(berufe.get(entry.getKey()));
}
}
return result;
自 Java 8 以來,使用Streams
接口並在其上調用.filter
(可能還有.map
),然后將.collect
.collect 到合適的集合並返回它,變得更加簡潔(可讀)。 類似於:
return jobs.entrySet().stream()
.filter(entry -> (entry.getValue().getDescription().toLowerCase().contains(description.toLowerCase())))
.map(entry -> berufe.get(entry.getKey()))
.collect(Collectors.toSet());
function 只能返回一個元素。 如果有多個命中,則必須返回一個可以包含多個元素的 object。 正如您已經提到的,列表作為集合的代表可以完成這項工作。 對於您的用例, map 是一個更好的選擇:
@GET
@Path("jobs")
public Job getJobsId(@QueryParam("id") int id, @QueryParam("description") String description)
{
//Declare an object that can take on multiple elements
var Map<Job> jobList = new HashMap<>();
for (final Map.Entry<Integer, Job> entry : jobs.entrySet())
{
if (entry.getValue().getDescription().toLowerCase().contains(description.toLowerCase()))
{
System.out.println("I DID IT");
System.out.println(entry.getKey());
jobList.put(entry.getKey(), job.get(entry.getKey()));
}
}
return jobList.get(id);
}
首先,你應該做的是正確設計API,例如:
@GET
@Path("jobs")
public List<Integer> searchJobsIdByDescription(@QueryParam("description") String description) {
...
}
重要:你必須給方法一個合適的名字(fe: searchJobsIdByDescription
),你的方法應該只做一件事,所以你只聲明做那件事所需的參數(fe: (@QueryParam("description") String description)
) 並返回預期類型 ( List<Integer>
)。
注意注解@Path("jobs")
,要避免匹配前面的
接下來,你應該仔細閱讀@frIV的回答
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.