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

Java 将元素从一个数组列表移动到另一个数组列表,java,arraylist,move,Java,Arraylist,Move,我有个问题 我有两张单子 List<SellableItems> table1 = new ArrayList(); List<SellableItems> table2 = new Arraylist(); List table1=new ArrayList(); List table2=新的Arraylist(); 在我的sellable items类中,我有一个名称和价格 现在我想将元素从表1移动到表2。因此,如果我的列表中包含价格为20的啤酒,我可以将其从表1

我有个问题

我有两张单子

List<SellableItems> table1 = new ArrayList();
List<SellableItems> table2 = new Arraylist();
List table1=new ArrayList();
List table2=新的Arraylist();
在我的sellable items类中,我有一个名称和价格


现在我想将元素从表1移动到表2。因此,如果我的列表中包含价格为20的啤酒,我可以将其从表1移动到表2,如果您想移动特定项目:

moveSellableItem(List<SellableItem> source, SellableItem item, List<SellableItem> destination){
    destination.add(item);
    source.remove(item);
}
moveSellableItem(列表来源、SellableItem项目、列表目的地){
目的地。添加(项目);
来源。删除(项目);
}
如果要移动具有特定参数(示例中为价格)的所有项目:

moveSellableItemWithPrice(列出来源、双倍价格、列出目的地){
for(可销售项目:来源){
如果(item.price==价格){
目的地。添加(项目);
来源。删除(项目);
}
}
}
导入java.util.List;
导入java.util.ArrayList;
公开课详情
{
公共静态void main(字符串[]args)
{
//第一阵列列表
List ArrayList 1=新建ArrayList();
arraylist1.添加(可销售项目);
//第二阵列列表
List ArrayList 2=新建ArrayList();
arraylist2.添加(可销售项目);
arraylist1.addAll(arraylist2);
}
}
可以这样做,请参见此示例

您可以参考begginers手册了解收集框架


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

for(int i=0; i<table1.size(); i++) {
    if(table1.get(i).price==20) {
        table2.add(table1.remove(i));
    }
}
我假设您希望过滤第一个列表,并将结果存储到另一个列表中,以便获得同一列表的原始版本和修改版本

由于我不知道您的
SellableItem
类是如何构造的,因此我在示例中使用了一些整数:

// 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 );
//示例的随机整数列表
List li=new Random().int(100,0,30).boxed().collect(Collectors.toList());
//将包含结果的列表
列出目标;
//一个流(从Java8开始),它过滤某个谓词和
//将结果收集为列表。
target=li.stream().filter(i->i.intValue()>20.collect(Collectors.toList());
系统输出打印项次(目标);
在这种情况下,
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();
    }
}
List table1=new ArrayList();
List table2=新的ArrayList();
迭代器itemsIterator=table1.Iterator();
while(itemsIterator.hasNext()){
SellableItems next=itemsIterator.next();
如果(下一个价格等于(20)){
表2.添加(下一步);
itemsIterator.remove();
}
}

也许可以使用
来添加
name=beer
price=20

table1.stream().filter((table11) -> (table11.getName().equals("beer") && table11.getPrice() == 20)).forEach((table11) -> {
            table2.add(table11);
        });
然后从原始列表中删除所有内容(如果需要)


以下是两个附加选项:

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);
    }
}
Iterator iter=table1.Iterator();
while(iter.hasNext()){
SellableItem=iter.next();
如果(item.getName().equals(“啤酒”)和&item.getPrice()==20){
iter.remove();
表2.增加(项目);
}
}

Predicate test=item->item.getName().equals(“啤酒”)和&item.getPrice()=20;
表1.stream().filter(test).forEach(表2::add);
表1.移除(试验);

循环检查
表1
是否
价格==20
如果是,则检查
表2.add()
表1.remove()
?将元素添加到第二个列表中,并将其从第一个列表中删除。您还想做什么?这不符合
的OPs要求。如果我的列表中有一瓶价格为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();
    }
}
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);