将元素从一个数组列表移动到另一个 java

Move element from one array list to another java

我有问题

我有两个列表

List<SellableItems> table1 = new ArrayList();
List<SellableItems> table2 = new Arraylist();

在我的可售商品 class 中,我有名称和价格。

现在我想将元素从 table1 移动到 table2。因此,如果我的列表中包含价格为 20 的啤酒,我可以将其从表 1 移至表 2

如果您想移动特定项目:

moveSellableItem(List<SellableItem> source, SellableItem item, List<SellableItem> destination){
    destination.add(item);
    source.remove(item);
}

如果您想要移动具有特定参数(例如价格)的所有项目:

moveSellableItemWithPrice(List<SellableItem> source, double price, List<SellableItem> destination){
    for(SellableItem item : source){
        if(item.price == price) {
            destination.add(item);
            source.remove(item);
        }
    }
}
        import java.util.List;
        import java.util.ArrayList;
        public class Details
        {
            public static void main(String [] args)
            {
                //First ArrayList
       List<SellableItems> arraylist1=new ArrayList<SellableItems>();
                arraylist1.add(SellableItems);


                //Second ArrayList
          List<SellableItems> arraylist2=new ArrayList<SellableItems>();
                arraylist2.add(SellableItems);


    arraylist1.addAll(arraylist2);

                }
            }

这个可以看这个例子

you can refer to the begginers book for the collection framework

遍历源列表,如果某个项目符合您的条件,则将其从源列表中删除并将其添加到目标列表中:

for(int i=0; i<table1.size(); i++) {
    if(table1.get(i).price==20) {
        table2.add(table1.remove(i));
    }
}

或者使用 foreach 循环:

for(SellableItem item : table1) {
    if(item.price==20) {
        table1.remove(item);
        table2.add(item);
    }
}

我假设您想要过滤第一个列表并将结果存储到另一个列表中,以便您拥有同一个列表的原始版本和修改版本。

因为我不知道你的 SellableItem class 是如何构造的,所以我在示例中使用了一些整数:

// List of random Integers for the example
List<Integer> li = new Random().ints( 100,0,30 ).boxed().collect( Collectors.toList() );
// The list which will contain the results
List<Integer> target;

// A stream (since Java 8) which is filtering for a certain Predicate and
// collecting the result as a list.
target = li.stream().filter( i->i.intValue() > 20 ).collect( Collectors.toList() );

System.out.println( target );

在这种情况下 target 将仅包含适用于其值大于 20 的项目。

但是,如果您不想保留原件并删除存储在第二个列表中的项目,您可以调用 li.removeAll(target);

迭代第一个列表并在价格等于 20 时添加到第二个列表:

List<SellableItems> table1 = new ArrayList<>();
List<SellableItems> table2 = new ArrayList<>();

Iterator<SellableItems> itemsIterator = table1.iterator();
while (itemsIterator.hasNext()) {
    SellableItems next = itemsIterator.next();
    if (next.price.equals(20)) {
        table2.add(next);
        itemsIterator.remove();
    }
}

也许使用 streamname = beerprice = 20

的所有值相加
table1.stream().filter((table11) -> (table11.getName().equals("beer") && table11.getPrice() == 20)).forEach((table11) -> {
            table2.add(table11);
        });

然后从原始列表中删除所有内容(如果需要)

table1.removeAll(table2);

这里有两个附加选项:

Iterator<SellableItem> iter = table1.iterator();
while (iter.hasNext()) {
    SellableItem item = iter.next();
    if (item.getName().equals("beer") && item.getPrice() == 20) {
        iter.remove();
        table2.add(item);
    }
}

Predicate<SellableItem> test = item -> item.getName().equals("beer") && item.getPrice() == 20;
table1.stream().filter(test).forEach(table2::add);
table1.removeIf(test);