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

How do I find out which textfield was not an integer
Java Resultset With Callablestatement
Simple JSP page with jsf giving JasperException [duplicate]
Java regular expression replace issue [duplicate]
How to import the java code as a project to IDE?
More bits versus less bits in programming
Unable to modify the Java codes
Transitioning from Main layout via button seems to break Radio Buttons in second Layout
how to fetch content in web crawling
Retrieve multiple objects using JPA
Which kind of jstl should i use? [closed]
Right way to use the #NonNull annotation in Android Studio
Pass-by-value & polymorphism [duplicate]
Java GC analyses [closed]
“Exceeded maximum allocated IDs”-Exception when allocating KeyRange (AppEngine Objectify)
HTTP response with java class is different from the response in the browser android?

Categories

HOME
maven
gitlab
deployment
omnet++
server
routes
jsrender
cakephp-2.5
angular-ui-bootstrap
android-youtube-api
izpack
hapi
lombok
fatal-error
angular-ui
footer
ab-testing
alpine
sqlcipher
smb
chromebook
tooltipster
elasticsearch-ruby
dbclient
jspm
windows-server-2000
gitignore
unboundid
flink-streaming
pdb
gtrendsr
file-format
automake
theano.scan
vapor
google-closure
reactive-cocoa-5
menuitem
upstart
revapi
celery-task
r-forge
flow-control
gcsfuse
iso8601
background-service
specrun
pintos
rotativa
bonobo
py2app
storekit
ubuntu-10.04
superstack
packagist
nessus
system.management
cloudbees
freelancer.com-api
pervasive-sql
tcpserver
google-places
eclipse-clp
ideamart
generic-programming
notify
dia
offloading
camanjs
client-side-templating
xcode-6.2
wp-query
hippomocks
xceed-datagrid
poller
kgdb
quantlib-swig
inbox
hamiltonian-cycle
mt
nsnetservice
first-responder
multipage
regsvr32
removeclass
code-cleanup
subscript
pydot
recent-documents
gdlib
mysql-error-1005
lpeg
sortable-tables
revisions
jquery-ui-droppable
rescale
memory-size
geneva-server

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App