如何使用java中的另一个字符串列表搜索对象列表

时间:2017-06-14 07:34:39

标签: java

我有下面的对象列表 -

Trace trace1=new Trace(1,"2345","test1","BLORE");
Trace trace2=new Trace(2,"2341","test2","BLORE");
Trace trace3=new Trace(3,"6574","test3","BLORE");
Trace trace4=new Trace(4,"9878","test4","BLORE");
Trace trace5=new Trace(5,"0902","test5","BLORE");
Trace trace6=new Trace(6,"2121","test6","BLORE");
Trace trace7=new Trace(7,"3232","test7","BLORE");
Trace trace8=new Trace(8,"7878","test8","BLORE");
List<Trace> list=new ArrayList<Trace>();

从这里,我想搜索下面的字符串列表 -

List<String> trace_list=new ArrayList<String>();
trace_list.add("2345");
trace_list.add("6574");
trace_list.add("0902");
trace_list.add("3232");

我该怎么做,请评论。

3 个答案:

答案 0 :(得分:3)

嘿,如果你使用java 8,为什么不使用像:

这样的流
public static void main(final String[] args) {
    Trace trace1 = new Trace(1, "2345", "test1", "BLORE");
    Trace trace2 = new Trace(2, "2341", "test2", "BLORE");
    Trace trace3 = new Trace(3, "6574", "test3", "BLORE");
    Trace trace4 = new Trace(4, "9878", "test4", "BLORE");
    Trace trace5 = new Trace(5, "0902", "test5", "BLORE");
    Trace trace6 = new Trace(6, "2121", "test6", "BLORE");
    Trace trace7 = new Trace(7, "3232", "test7", "BLORE");
    Trace trace8 = new Trace(8, "7878", "test8", "BLORE");
    List<Trace> list = new ArrayList<Trace>();
    list.add(trace1);
    list.add(trace2);
    list.add(trace3);
    list.add(trace4);
    list.add(trace5);
    list.add(trace6);
    list.add(trace7);
    list.add(trace8);

    List<String> trace_list = new ArrayList<String>();
    trace_list.add("2345");
    trace_list.add("6574");
    trace_list.add("0902");
    trace_list.add("3232");

    List<Trace> newTraceList = list.stream().filter(t -> !trace_list.contains(t.getTrace_number())).collect(Collectors.toList());
    System.out.println(newTraceList.toString());
}

答案 1 :(得分:0)

您可以使用HashMap实现上述方案,

Trace trace1=new Trace(1,"2345","test1","BLORE");
Trace trace2=new Trace(2,"2341","test2","BLORE");
Trace trace3=new Trace(3,"6574","test3","BLORE");
Trace trace4=new Trace(4,"9878","test4","BLORE");
Trace trace5=new Trace(5,"0902","test5","BLORE");
Trace trace6=new Trace(6,"2121","test6","BLORE");
Trace trace7=new Trace(7,"3232","test7","BLORE");
Trace trace8=new Trace(8,"7878","test8","BLORE");

HashMap<String,Trace> traceMap = new HashMap<String,Trace>();

traceMap.put("2345",trace1);
traceMap.put("2341",trace2);
traceMap.put("6574",trace3);
traceMap.put("9878",trace4);
traceMap.put("0902",trace5);
traceMap.put("2121",trace6);
traceMap.put("3232",trace7);
traceMap.put("7878",trace8);

将所有视图添加到地图后,您可以使用foreach循环或使用迭代器来检索它。

for(String key:traceMap.keyset()){ 
      Trace traceObject = traceMap.get(key);
}

使用迭代器,

Iterator it = traceMap.keySet().iterator();
   while (it.hasNext()) {
      String key = it.next();
      Trace value = traceMap.get(key);
}

答案 2 :(得分:0)

使用Stream的另一种方式:

    HashMap<String,Trace> traceMap = new HashMap<String,Trace>();

    traceMap.put("2345", trace1);
    traceMap.put("2341", trace2);
    traceMap.put("6574", trace3);
    traceMap.put("9878", trace4);
    traceMap.put("0902", trace5);
    traceMap.put("2121", trace6);
    traceMap.put("3232", trace7);
    traceMap.put("7878", trace8);

    Optional<Trace> result = traceMap.entrySet().stream().
                             filter(map -> "0902".equals(map.getValue())).
                             map(map -> map.getValue()).findFirst();