简体   繁体   English

Java比较并添加项目到列表

[英]Java comparing and adding item to list

I have two list 我有两个清单

List<Person> person = new ArrayList<Person>();
List<Employee> employee = new Arraylist<Employee>();

and another list of Person type , 以及另一个Person类型列表,

List<Person> temp_person = new ArrayList<Person>();

Those two list are different , but has one "key" in common that is "id". 这两个列表不同,但是有一个共同的“键”即“ id”。

Now , I want to check if the "id" of the person is same as "id" of the employee . 现在,我要检查person “ id”是否与employee “ id”相同。 If I find any match , then add that elements of person to temp_person 如果找到任何匹配项,则将person元素添加到temp_person

The idea here is simple: for all elements contained in person , check to see if a value p.id matches any e.id for all elements contained in employee , and collect them into a new list temp_person . 这里的想法很简单:包含在所有的元素person ,检查,看看是否值p.id任何匹配e.id中包含的所有元素employee ,并把它们收集到一个新的列表temp_person

The result here would be a double-nested loop, or O(nk) efficiency if you couldn't guarantee any relationship between the two entities. 如果您不能保证两个实体之间的任何关系,那么这里的结果将是双重嵌套循环或O(nk)效率。

In Java 8, that becomes slightly less painful to read with the Stream API. 在Java 8中,使用Stream API进行读取时,痛苦会减轻一些。 It takes what I've described above and makes it executable. 它采用了我上面描述的内容并使它可执行。

final List<Person> temp_person =
            person.stream()
                  .filter(p -> employee.stream().anyMatch(e -> e.getId() == p.getId()))
                  .collect(Collectors.toList());

You have to make two for loops one for loop on the person list and the other to loop on employee list and check if there is any matching (the id's are equal) then add that element to temp_person list 您必须进行两个for循环,一个在person列表上进行循环,另一个在employee列表上进行循环,并检查是否存在匹配项(id相等),然后将该元素添加到temp_person列表中

The break inside the loop in order to break that loop and start the other loop (As you already found a match so you need to get another element) break ,以打破这种循环,并启动其他环路(正如你已经找到了一个匹配,所以你需要得到另一个元素)内循环

Assuming that you can get your id via getId() and Id is int if it's not int and it is String use equals() 假设您可以通过getId()获得id ,并且如果id不是int且为String ,则id为int,请使用equals()

for(int i = 0 ; i < person.size() ; i++){
  for(int j = 0 ; j < employee.size() ; j++){
    if(person.get(i).getId()==employee.get(j).getId()){
       temp_person.add(person.get(i));
       break;
    } 
  }
}

You have so few elements that you don't need to worry too much about perf. 您拥有的要素很少,因此您无需过多担心perf。 But for the sake of argument a more effective way for large lists would be as follows: Put persons and empoyees in maps by id (or better yet keep them like this always). 但是,为了进行辩论,对大型列表而言,更有效的方法如下:按ID将人员和受雇者放在地图上(或者最好始终保持这种状态)。 Do an intersection between personById.keySet() and employee.keySet() and finally iterate the resulting key set, fetching from the person map and putting into your temp list. 在personById.keySet()和employee.keySet()之间进行交集,最后迭代生成的键集,从人图中获取并放入临时列表中。

Assuming that the maps are maintained like this and don't have to be computed, this would be a O(1) for the keySets, the intersection would run in O(k) (Guavas) and the copy loop time is relative to the number of matches, worst case O(k), where each copy is O(1) (amortized). 假设地图是这样维护的,不必计算,则密钥集将为O(1),交集将以O(k)(Guavas)运行,并且复制循环时间相对于匹配数,最差情况为O(k),其中每个副本为O(1)(摊销)。 So you end up with O(k) time complexity. 因此,您的时间复杂度为O(k)。

It can be done in this manner as below :

List<Person> temp_person = new ArrayList<Person>();
Iterator<Person> firstIterator = firstArrayList.iterator();
while (firstIterator.hasNext()) {
  String str1 = (String) firstIterator.next().ID();
  Iterator<Employee> secondIterator = secondListArrayList.iterator();
  while (secondIterator.hasNext()) {
    String str2 = (String) secondIterator.next().ID();
    if (str1.equals(str2)) {
    temp_person.add(firstIterator.next()); 
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM