java


Check if lists contain same objects


I have two filled lists.
The first list contains for example:
"Shoppinglist-fruit", "Shoppinglist-drinks", "Shoppinglist-dinner"
The second list contains:
"Shoppinglist-drinks"
Now i wanna print all items in the first list, except if there's a same object in the second list with the same name (Shoppinglist-drinks).
Looking like:
"Shoppinglist-fruit", "Shoppinglist-dinner"
So how can i check if the name of the object inside the second list is also in one of the objects of the first list.
Eventually i want to end up with a string containing all the names of the shoppinglists that are in the first list and not in the second one.
I started with some code below but i haven't been able to finish it.
I have the two lists, one called listShoppinglists, this is a list filled with different shopping lists.
And the second list filled with somebody's shoppinglists.
So i wanna check if the name of the shoppinglists are equal.
If done that by doing so.
public String getAllShoppingLists(List listShoppinglists, Customer customer, List shoppinglists) {
String namesOfTheShoppingListNames = ""
for (Shoppinglist shoppinglist : listShoppinglists) {
for (int i = 0; i < customer.shoppinglists.size(); i++) {
if (customer.shoppinglists.get(i).getName().equals(shoppinglist.getName())) {
// Some action here
}
}
}
return namesOfTheShoppingListNames;
}
Use Collections.removeAll() method to do this. Quoted from JavaDoc:-
Removes all of this collection's elements that are also contained in
the specified collection (optional operation). After this call
returns, this collection will contain no elements in common with the
specified collection
.List<String> list1=new ArrayList<String>();
list1.add("Shoppinglist-fruit");list1.add("Shoppinglist-drinks");list1.add("Shoppinglist-dinner");
List<String> list2=new ArrayList<String>();
list2.add("Shoppinglist-drinks");
list1.removeAll(list2);
System.out.println(list1);
//Output:- [Shoppinglist-fruit, Shoppinglist-dinner]
In case, lists contains a custom objects, override equals and hashcode methods in that custom object.
You can try this:
List<ShoopingList> firstShoppingListNames = new ArrayList<>();
firstShoppingListNames.add(new ShoppingList("fruit"));
firstShoppingListNames.add(new ShoppingList("dinner"));
firstShoppingListNames.add(new ShoppingList("juice"));
List<ShoppingList> secondShoppingListNames = new ArrayList<>();
secondShoppingListNames.add(new ShoppingList("fruit"));
List<ShoppingList> distinct = firstShoppingListNames.stream().
filter( list -> secondShoppingListNames.stream().
noneMatch(o -> o.getName().equals(list.getName()))).
collect(Collectors.toList());
distinct.forEach(o -> System.out.print(o.getName()));
In this case you are using stream to achieve what you want. You filter first list to obtain those elements, which are not present in other list.
Additionaly if you want to obtain only names of those lists you can use map:
List<String> distinct = firstShoppingListNames.stream().
filter( list -> secondShoppingListNames.stream().
noneMatch(o -> o.getName().equals(list.getName()))).
map(ShoppingList::getName).collect(Collectors.toList());

Related Links

Java: Reorder elements of a multi-dimentional array
Usage of Regex for phone number validation in java
Draw Filled Shape from Four Points
Return Final List from Filtered Observable
No Persistence provider for EntityManager named with eclipselink
When 1st start the application how to make it this page? (ListNearablesActivity.java)
Unable to Create Managed Bean due to Missing Dependencies [duplicate]
How do I call an object an object from a .fxml file in my controller file?
MyBatis to XML or CSV
Why isn't my JMenu working?
When to use #BeforeClass in JunitTests?
jtable ( adding new row)
java.lang.NullPointerException always on third looping cycle [duplicate]
Build OData API without Entity Framework
how can i modify main method to get input and output from the comandline?
Drools list iteration issue

Categories

HOME
arduino-uno
pdf
client
coq
server
drivers
google-api-php-client
kalman-filter
jxls
disassembler
uber-api
autotools
user-input
midi
correlation
commonmark
zebra-printers
decimal
quartz-scheduler
caml
serverless-framework
fatal-error
alpine
scichart
introduction
errorlevel
orleans
sylius
extjs5
kudan
cultureinfo
restful-authentication
java-7
kannel
fgetcsv
msys2
quote
delicious-api
nat
tinymce-4
preg-match-all
karaf
html5-fullscreen
wixsharp
git-merge
dynamics-crm-2013
android-browser
code-search-engine
leading-zero
sas-visual-analytics
nxlog
outlook-api
unixodbc
sas-jmp
auto-update
menuitem
rainbowtable
network-flow
aurelia-validation
brightcove
nsarray
imanage
specrun
watchconnectivity
abcpdf9
static-ip-address
google-web-starter-kit
markojs
hover-over
independentsoft
prerequisites
lib.web.mvc
wireshark-dissector
android-listview
sailfish-os
ami
rake-task
xc16
seaside
android-2.2-froyo
cloud-connect
stxxl
aqtime
runtime.exec
jmapviewer
limejs
anonymous-methods
bitsharp
cascalog
uiviewanimation-curve
pydot
hamachi
servicehost
wise
firefox4
adrotator

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App