In Java, how to check if a collection of objects contains an object that depends on one of its properties.
For example, I would like to check if it Collection<myObjects>contains an instance of myObjects that has myObjects.name = "myName".
Collection<myObjects>
You will have to iterate and perform the comparison.
Consider using a map.
Map<String,myObjects> myMap = new HashMap<String,myObjects>(); myMap.put(myObject.name,myObject);
apache, , :
public class MyPredicate implements Predicate { public boolean evaluate(Object input) { return (MyObject).name = "myName"; } }
, :
CollectionUtils.find(myCollection, new MyPredicate());
, , null, .
If you have control over how data is collected, you can also have some kind of “reverse index” for this property; An index can be a mapping between a name and a set of myObject objects with that name. Thus, you can effectively get all the elements with the given name. You can add other indexes in the same way.
Source: https://habr.com/ru/post/1742430/More articles:PHP OOP: Avoid Singleton / Static Methods in Domain Model Template - oopCan a program that controls IE detect if HTTP 30x code exists? - internet-explorerКак сделать Date независимым от языка? - javaCOM Explorer Internet Explorer Automation: Converting a Numeric Error Code to a String - internet-explorerFinding a number in an array in minimal time - cGetting AJAX HTTP response code as 0 - ajaxCssResource examples? - gwtKey Data: Overkill for a simple, static UITableView iPhone app? - objective-cOn linux, what can cause dlopen to emit SIGFPE? - linuxКак вызвать внутреннюю функцию в bash, если я определил одноименное имя? - bashAll Articles