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. So if i my list contains a beer with the price 20, i can move it from table1 to 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 :
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
Iterate on the first list and add to the second list when price equals 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:
// 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.
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);
.
Perhaps use a stream
to add all the values where name = beer
and price = 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.