简体   繁体   English

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

[英]Move element from one array list to another java

i have a problem我有个问题

I have two lists我有两个清单

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

In my sellable items class, i have a name and a price.在我的可出售物品类中,我有一个名称和一个价格。

Now I wish to move elements from table1 to table2.现在我希望将元素从 table1 移动到 table2。 So if i my list contains a beer with the price 20, i can move it from table1 to table2因此,如果我的列表包含价格为 20 的啤酒,我可以将其从 table1 移到 table2

Iterate over the source list, if an item matches your criteria remove it from the source list and add it to the target list :迭代源列表,如果某个项目符合您的条件,则将其从源列表中删除并将其添加到目标列表中:

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

Or with a foreach loop :或者使用 foreach 循环:

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

If you want to move a specific item :如果要移动特定项目:

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

If you want to move all item with a specific parameter (price in example) :如果要移动具有特定参数的所有项目(例如价格):

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

                }
            }

this can be done see this example这可以完成,请参阅此示例

you can refer to the begginers book for the collection framework集合框架可以参考begginers book

Iterate on the first list and add to the second list when price equals 20:迭代第一个列表并在价格等于 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();
    }
}

I assume that you want to filter the first list and store the result into another list so that you have the original and a modified version of the same list.我假设您想过滤第一个列表并将结果存储到另一个列表中,以便您拥有同一列表的原始版本和修改版本。

Since I do not know how your SellableItem class is structured I used some Integers for my example:由于我不知道您的SellableItem类是如何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 );

In this case target will only contain items which are applicable to their value being bigger than 20.在这种情况下, target将仅包含适用于其值大于 20 的项目。

However if you don't want to keep the original and remove the items you stored in the 2nd list you can call li.removeAll(target);但是,如果您不想保留原件并删除您存储在第二个列表中的项目,您可以调用li.removeAll(target); . .

Perhaps use a stream to add all the values where name = beer and price = 20也许使用stream添加name = beerprice = 20所有值

table1.stream().filter((table11) -> (table11.getName().equals("beer") && table11.getPrice() == 20)).forEach((table11) -> {
            table2.add(table11);
        });

Then remove all from the original list (if required)然后从原始列表中删除所有内容(如果需要)

table1.removeAll(table2);

Here are two additional options:这里有两个附加选项:

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

and

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

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

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