2014-05-18 3 views
0

타임 스탬프, ID 및 기타 데이터가 포함 된 배열 또는 목록이 있습니다. 이 같은 뭔가 : 나는 어떤 MSGID이 목록의 항목에 대한 선택을 할 수있는 CanTrace의 목록 또는 배열을 만들 수있는 방법객체 목록에서 동일한 값을 가진 객체 항목 선택

public class CanMessage { 
    public int MsgID ; 
    public byte length ; 
    public byte [] dataByte = new byte[8]; 

} 

public class CanTrace { 
    public float timestamp ; 
    public CanMessage canMsg ; 
} 

질문입니다. 예를 들어 하나의 동일한 MsgID로 dataByte의 플롯을 만들 수 있습니다. 또는 예를 들어 while 루프를 사용하여 검색하는 경우에만 을 사용하여 ct 객체를 생성하면됩니다. List ct = new ArrayList(); (CanTrace) (map.get -

답변

0

MSGID 고유 있다면, 키는 HashMap을 사용하는 것이 유용 할 것이다 (MSGID, CanTrace)의 값 쌍 는 그런 다음 사용 dataByte에 액세스 할 수있을 것 (MsgID)). dataByte

0

Java 8을 사용하는 경우 필터 방법을 직접 listOfCanMessages.stream().filter(b -> b.MsgID == 1)으로 사용할 수 있습니다.

다른 경우는 예를 들어 Guava 또는 Lamdaj

을 시도 할 수 있습니다.

import static ch.lambdaj.Lambda.having; 
import static ch.lambdaj.Lambda.on; 
import static ch.lambdaj.Lambda.select; 
import static org.hamcrest.core.IsEqual.equalTo; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.stream.Collectors; 

import com.google.common.base.Predicate; 
import com.google.common.collect.Collections2; 
import com.google.common.collect.Lists; 

public class FilterSample { 

    public static void main(String[] args) { 
     List<CanMessage> list = new ArrayList<CanMessage>(); 
     CanMessage c = new CanMessage(); 
     c.MsgID = 1; 
     CanMessage c2 = new CanMessage(); 
     c2.MsgID = 2; 
     CanMessage c3 = new CanMessage(); 
     c3.MsgID = 1; 
     CanMessage c4 = new CanMessage(); 
     c4.MsgID = 4; 

     list.add(c); 
     list.add(c2); 
     list.add(c3); 
     list.add(c4); 

     //Java 8 example 
     List<CanMessage> filteredList1 = list.stream().filter(b -> b.MsgID == 1).collect(Collectors.toList()); 
     System.out.println(filteredList1); 

     //Guava example 
     List<CanMessage> filteredList2 = Lists.newArrayList(Collections2.filter(
       list, new Predicate<CanMessage>() { 
       @Override 
       public boolean apply(CanMessage input) { 
        if (input.MsgID == 1) { 
        return true; 
        } 
        return false; 
       } 
       })); 
     System.out.println(filteredList2); 

     //Lambdaj example. Please note 6 down vote accepted Lambdaj wraps your (non-final) classes with a Proxy 
     // and intercepts METHOD invocations on them. That means it cannot work on fields but only on methods. So 
     // you cannot access MsgID directly, you'll have to provide a getter method 
     List<CanMessage> filteredList3 = select(list, having(on(CanMessage.class).getMsgID(), equalTo(1))); 
     System.out.println(filteredList3); 
    } 

} 
0

빠른 답변 주셔서 감사합니다. 동료에게 물어 보면 다음과 같은 아이디어가 있습니다.

public void OrderedMsg(int Id){ 

    List<CanTrace> traces = new ArrayList<CanTrace>(); 

    Comparator<CanTrace> comparator = new Comparator<CanTrace>() { 
     public int compare(CanTrace o1, CanTrace o2) { 
      if (o1.time < o2.time) { 
       return -1; 
      } else if (o1.time > o2.time) { 
       return 1; 
      } else { 
       return 0; 
      } 
     } 
    }; 

    for (CanTrace trace : traces) { 
     int id = trace.canMsg.MsgID; 
     if (!orderedMap.containsKey(id)) { 
      orderedMap.put(id, new TreeSet<CanTrace>(comparator)); 
     } 
     orderedMap.get(id).add(trace); 
    } 


} 
관련 문제