0

I have List of object like below-

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>();

From here i want to search below list of Strings-

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

How can i do this, pls comment.

4
  • what's the Trace Object Commented Jun 14, 2017 at 7:37
  • its a pure pojo class with some parameter like id , Trace_number, name , city and gettter & setter and arg-construction. Commented Jun 14, 2017 at 7:39
  • 1
    You should probably keep them in a map instead. Commented Jun 14, 2017 at 7:42
  • can you give me one code example pls Commented Jun 14, 2017 at 7:50

3 Answers 3

3

Hey if you are using java 8 why not use streams like:

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());
}
Sign up to request clarification or add additional context in comments.

Comments

0

The another way of using 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();

Comments

0

You can use HashMap to achieve above scenario,

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);

After adding all vales to the map you can retrieve it either by using foreach loop or by using iterator.

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

by using iterator,

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.