【问题标题】:Selecting object items with same value from a list of objects从对象列表中选择具有相同值的对象项
【发布时间】:2014-05-18 16:53:06
【问题描述】:

我有一个包含时间戳、ID 和其他数据的数组或列表。 像这样的:

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

}

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

问题是如何制作 CanTrace 的列表或数组,在其中我可以对具有特定 MsgID 的列表项进行选择。例如,我可以用一个相同的 MsgID 绘制 dataByte 的图。 或者这是否只能通过使用 while 循环搜索来实现,例如使用 ct 对象创建 列表 ct =new ArrayList();

【问题讨论】:

    标签: java arrays list


    【解决方案1】:

    如果 MsgID 是唯一的,那么使用带有 (MsgID,CanTrace) 键值对的 HashMap 会很有用 然后你就可以使用 - (CanTrace)(map.get(MsgID)).dataByte

    【讨论】:

      【解决方案2】:

      如果您使用的是 Java 8,您可以直接使用过滤器方法为listOfCanMessages.stream().filter(b -> b.MsgID == 1)

      在其他情况下,您可以尝试GuavaLamdaj

      例如。

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

      【讨论】:

        【解决方案3】:

        感谢您的快速答复。 我问了一位同事,他提出了这个想法:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-10
          • 2022-01-12
          • 1970-01-01
          • 2019-09-17
          • 2011-06-11
          • 1970-01-01
          • 2021-08-11
          • 2017-06-24
          相关资源
          最近更新 更多