I have Enumutils like below:
public interface EnumUtil {
String getValue();
static <T extends Enum<T> & EnumUtil> T fromValue(String enumValue, Class<T> type) {
EnumSet<T> all=EnumSet.allOf(type);
for (final T t: all) {
System.out.println("Value: " + t.getValue());
System.out.println("Name: " + t.name());
T val = T.valueOf(t.getDeclaringClass(), t.name()).;
System.out.println("ValValVal: " + val);
if (t.getValue().equalsIgnoreCase(enumValue)) {
return t;
}
}
return null;
}
}
Then I have created an enum which appears as follows:
#Getter
#AllArgsConstructor
public enum SupportedOptions implements EnumUtil {
PART("PART"),
MSRV("MSRV");
private final String value; //If we add this line then we need not to override getValue()
public static SupportedOptions fromValue(final String text) {
return EnumUtil.fromValue(text, SupportedOptions.class);
}
}
This works fine without any compilation issues. (getValue also returns the enum parameter successfully in fromvalue in EnumUtils interface for this enum)
But in the following scenario it results in a compile time exception (need to implement abstract methods).
#Getter
#AllArgsConstructor
public enum RejectedResponseCode implements EnumUtil {
UNPR("300", "849", "700", "701", "702", "703", "705", "730","704"),
IMSG("302", "105", "113", "114"),
PARS("107", "100", "102", "103", "115","720"),
SECU("302", "668", "669", "670", "671");
//approach 1
private final String value; //Here it want some value as i am using constructor like below
RejectedResponseCode(final String... codes) {
this.codes = Arrays.asList(codes);
//this.value = getValue(); <--- Is this approach fine. (For appraoch 1)
}
private final List<String> codes;
public static RejectedResponseCode getValueOfData(final String value) {
final Optional<RejectedResponseCode> result = Arrays.stream(values()).filter(rejectedResponseCode -> rejectedResponseCode.codes.contains(value)).findFirst();
if (result.isPresent()) {
return result.get();
}
return null;
}
public static RejectedResponseCode fromValue(final String text) {
return EnumUtil.fromValue(text, RejectedResponseCode.class);
}
//Approach 2
//#Override
//public String getValue(){
// return null; //Is this approach correct?
//}
}
How to ignore either not to override getValue()?
Or how to override getValue() in such a way that it returns parameter
or any other way to manage this in EnumUtil?
You need to search each enum value supporting multiple search keys, so implementing getValue is no use. On way around this is to reverse the operation such that the enum supplies a set of search criteria to be used, and implement a simple utility method on EnumUtil:
public class EnumUtil {
#SafeVarargs
public static <T,V> T match(T[] values, V fieldValue, BiPredicate<T,V> ... checks) {
for (var pred : checks)
for (T item : values)
if(pred.test(item, fieldValue))
return item;
return null; // OR throw new IllegalArgumentException("Not found: "+fieldValue);
}
}
Then your enum classes can setup any number of search parameters. SupportedOptions just matches on name:
enum SupportedOptions {
PART,
MSRV;
public static SupportedOptions fromValue(final String text) {
return EnumUtil.match(values(), text.toUpperCase(), (e, s) -> e.name().equals(s));
}
}
RejectedResponseCode matches on name() and codes.indexOf:
enum RejectedResponseCode {
UNPR("300", "849", "700", "701", "702", "703", "705", "730","704"),
IMSG("302", "105", "113", "114"),
PARS("107", "100", "102", "103", "115","720"),
SECU("302", "668", "669", "670", "671");
RejectedResponseCode(final String... codes) {
this.codes = Arrays.asList(codes);
}
private final List<String> codes;
public static RejectedResponseCode fromValue(final String text) {
return EnumUtil.match(values(), text.toUpperCase(), (e, s) -> e.name().equals(s), (e, s) -> e.codes.indexOf(s) >= 0);
}
}
So, anything can be located by name:
public static void main(String[] args)
{
for (String s : new String[] { "PART", "msrv", "other"})
System.out.println("SupportedOptions.fromValue("+s+") => "+ SupportedOptions.fromValue(s));
for (String s : new String[] { "UNPR", "imsg", "102", "671", "999999"})
System.out.println("RejectedResponseCode.fromValue("+s+") => "+ RejectedResponseCode.fromValue(s));
}
SupportedOptions.fromValue(PART) => PART
SupportedOptions.fromValue(msrv) => MSRV
SupportedOptions.fromValue(other) => null
RejectedResponseCode.fromValue(UNPR) => UNPR
RejectedResponseCode.fromValue(imsg) => IMSG
RejectedResponseCode.fromValue(102) => PARS
RejectedResponseCode.fromValue(671) => SECU
RejectedResponseCode.fromValue(999999) => null
As directed by Joachim Sauer in comment:
EnumUtil.fromValue() simply assumes that there's always exactly one identifiying string for each enum value. That's not true for your second sample. So either don't use EnumUtil.fromValue() or extend it to also support multiple values (probably by having a second interface that can return an array or collection of potentital identifiers) –
Accordingly, EnumUtils Removed from enum and created methods inside enums only.
Related
I have an enum like below. Until recently, all variables were single-valued. However, now TYPE4 can have one of three acceptable values. I was hoping to simply modify this enum to accommodate for TYPE4, but thinking perhaps having only one type that is multi-valued means I need to use an object for mapping rather than an enum. I would be grateful for any insights. Thank you.
public enum Record {
TYPE1("TYPE1"),
TYPE2("TYPE2"),
TYPE3("TYPE3"),
TYPE4_MULTI(TYPE_A or TYPE_B or TYPE_C);
private final String value;
public static final Map<Record, String> enumMap = new EnumMap<Record, String>(
Record.class);
static {
for (Record e : Record.values())
enumMap.put(e, e.getValue());
}
Record(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
Operationally, I use this enum in a factory class to determine which of 4 types of subclasses I should instantiate. I do this by have each of the subclasses know its own type like this:
#Override
public String getType() {
return Record.TYPE1.getValue();
}
,and then the factory class pre-builds a set of the subclasses like this:
#Component
public class RecordProcessorFactory {
#Autowired
public RecordProcessorFactory(List<RecordProcessor> processors) {
for (RecordProcessor recordProcessor : processors) {
processorCache.put(recordProcessor.getType(), recordProcessor);
}
}
private static final Map<String, RecordProcessor> processorCache = new HashMap<String, RecordProcessor>();
public RecordProcessor getSyncProcessor(String type) {
RecordProcessor service = processorCache.get(type);
if(service == null) throw new RuntimeException("Unknown service type: " + type);
return service;
}
}
You could use a String array to store multiple values, note that your logic may change with enumMap that way.
public enum Record {
TYPE1("TYPE1"),
TYPE2("TYPE2"),
TYPE3("TYPE3"),
TYPE4_MULTI("TYPE_A", "TYPE_B", "TYPE_C");
private final String[] values;
public static final Map<Record, String[]> enumMap = new EnumMap<Record, String[]>(Record.class);
static {
for (Record e : Record.values())
enumMap.put(e, e.getValues());
}
Record(String... values) {
this.values = values;
}
public String[] getValues() {
return values;
}
}
In case you need to get the Enum from a String value, you could add this static method:
public static Optional<Record> optionalValueOf(final String value) {
for (Record record : values()) {
for (String recordValue : record.values) {
if (null == value && null == recordValue || value.equals(recordValue)) {
return Optional.of(record);
}
}
}
return Optional.empty();
}
I think it's better to encapsulate values in the enum. It should be immutable (array is not immutable data storage).
#lombok.Getter
public enum Record {
TYPE1("TYPE1"),
TYPE2("TYPE2"),
TYPE3("TYPE3"),
TYPE4_MULTI("TYPE_A", "TYPE_B", "TYPE_C");
// immutable list
private final List<String> values;
Record(String... values) {
this.values = Arrays.stream(values)
.collect(Collectors.toList());
}
}
P.S. Map<Record, String> enumMap I think is useless, because you have a Record already and all you need just call record.getValues() instead of Record.enumMaps.get(record). Also, this is breakes OOP encapsulation.
Problem
We have multiple enum types that have some public static EnumType valueOfText(String text), for the purpose of mapping the contents of a data file cell to enum.
I'm trying to write a generic enum util that takes a comma-separated string and return multiple enum values. For example, we have the following enum:
public enum Frequency {
SEMI_ANNUAL("S"), MONTHLY("M"), QUARTERLY("Q"), ANNUAL("A")
public final String textValue;
public Frequency(String textValue) {
this.textValue = textValue;
}
public static Frequency valueOfText(String textValue) {
for (Frequency frequency : values()) {
if (frequency.textValue.equals(textValue))
return frequency;
}
return null;
}
}
and string "A,S" which we want to convert to [Frequency.ANNUAL, Frequency.SEMI_ANNUAL].
Attempted solution
I create some EnumUtils like so:
import java.util.stream.Collectors
public final class EnumUtils {
public static final String LIST_SEPARATOR = ",";
public static <E extends Enum<E>> List<E> CreateFromText(String text) {
List<String> textList = text.split(this.LIST_SEPARATOR)
return textList.stream()
.map { txt ->
E.valueOfText(txt)
}
.collect(Collectors.toList())
}
}
What happen after said solution
We go to use it, like this:
EnumUtils.CreateFromText<Frequency>(row[3])
and the IDE compain, immediately, about the <>.
How can we specify enum type in this?
In Groovy you can do it if you pass the actual Class instead of just using a type parameter.
enum Frequency {
SEMI_ANNUAL("S"), MONTHLY("M"), QUARTERLY("Q"), ANNUAL("A")
final String textValue;
Frequency(String textValue) {
this.textValue = textValue;
}
static Frequency valueOfText(String textValue) {
return values().find { it.textValue == textValue }
}
}
final class EnumUtils {
static <E extends Enum<E>> List<E> createFromText(Class<E> clazz, String text) {
return text.split(",").collect { clazz.valueOfText(it) }
}
}
EnumUtils.createFromText(Frequency, "S,M")
The same idea won't work in Java, since clazz won't have valueOfText at compile time.
Perhaps the Util class doesn't save you much typing, though:
"S,M".split(",").collect(Frequency.&valueOfText)
Coming from C++ and currently employed in a Java environment, I was wondering how I would be able to create a mapping of void* and void* in Java in order to create a generic mapping from A to B and from B to A. I am aware that Java doesn't have pointers and references the way C++ does, but am failing to find a method that would still allow this.
An example of what I am trying to achieve:
public class A{
#GenericMapping(1)
private Integer temp1;
}
public class B{
#GenericMapping(1)
private Integer temp2;
}
public class Mapper{
private List<Pair<Integer, Integer>> mapping;
public void map(Object ObjectOfAnyClassButLetsAssumeA, Object ObjectOfAnyClassButLetsAssumeB){
// Get all parameters with GenericMapping above it, get its value
// and match the corresponding value with the value of B
// Resulting in A.temp1 = B.temp2;
}
}
However, if possible I'd rather create a map (like map[A.temp1] = B.temp2) in order to avoid using the #GenericMapping, seeing as that would allow me to not modify the class in any way and still facilitate its mapping.
I think I understand what you want to do here and you can accomplish it with some metadata and Java 8's Lambdas.
What we do is set up a helper class that contains all mappings identified by class and IDs (analogous to your #GenericMapping but without actually annotating the classes) and containing methods for setting and getting the value. It's important that all mappings for the same ID have the same value type or a ClassCastException may be thrown when transferring values.
My example uses three classes where not all mappings apply to all classes.
Here's the code:
public class GenericMappingDemo {
static class A {
private Integer integerA;
private String stringA;
private Float floatA;
public A(final Integer integerA, final String stringA, final Float floatA) {
this.integerA = integerA;
this.stringA = stringA;
this.floatA = floatA;
}
public Integer getIntegerA() {
return integerA;
}
public void setIntegerA(final Integer integerA) {
this.integerA = integerA;
}
public String getStringA() {
return stringA;
}
public void setStringA(final String stringA) {
this.stringA = stringA;
}
public Float getFloatA() {
return floatA;
}
public void setFloatA(final Float floatA) {
this.floatA = floatA;
}
#Override
public String toString() {
return "A{integerA=" + integerA + ", stringA='" + stringA + "', floatA=" + floatA + '}';
}
}
static class B {
private Integer integerB;
private String stringB;
public Integer getIntegerB() {
return integerB;
}
public void setIntegerB(final Integer integerB) {
this.integerB = integerB;
}
public String getStringB() {
return stringB;
}
public void setStringB(final String stringB) {
this.stringB = stringB;
}
#Override
public String toString() {
return "B{integerB=" + integerB + ", stringB='" + stringB + '\'' + '}';
}
}
static class C {
private Float floatC;
private String stringC;
public Float getFloatC() {
return floatC;
}
public void setFloatC(final Float floatC) {
this.floatC = floatC;
}
public String getStringC() {
return stringC;
}
public void setStringC(final String stringC) {
this.stringC = stringC;
}
#Override
public String toString() {
return "C{floatC=" + floatC + ", stringC='" + stringC + "'}";
}
}
static class GenericMapping<C, T> {
final int id;
final Class<C> type;
final Function<C, T> getter;
final BiConsumer<C, T> setter;
public GenericMapping(final int id,
final Class<C> type,
final Function<C, T> getter,
final BiConsumer<C, T> setter) {
this.id = id;
this.type = type;
this.getter = getter;
this.setter = setter;
}
}
static class Mapper {
// All mappings by class and id
private final Map<Class<?>, Map<Integer, GenericMapping<?, ?>>> mappings
= new HashMap<>();
public void addMapping(GenericMapping<?, ?> mapping) {
mappings.computeIfAbsent(mapping.type,
c -> new TreeMap<>()).put(mapping.id, mapping);
}
/**
* Map values from one object to another,
* using any mapping ids that apply to both classes
* #param from The object to transfer values from
* #param to The object to transfer values to
*/
public <From, To> void map(From from, To to) {
Map<Integer, GenericMapping<?, ?>> getters
= mappings.get(from.getClass());
Map<Integer, GenericMapping<?, ?>> setters
= mappings.get(to.getClass());
if (getters == null || setters == null) {
// Nothing to do
return;
}
// Create a set with the ids in both getters and
// setters, i.e. the mappings that apply
Set<Integer> ids = new HashSet<>(getters.keySet());
ids.retainAll(setters.keySet());
// Transfer all mappings
for (Integer id : ids) {
GenericMapping<From, ?> getter
= (GenericMapping<From, ?>) getters.get(id);
GenericMapping<To, ?> setter
= (GenericMapping<To, ?>) setters.get(id);
transfer(from, to, getter, setter);
}
}
private <From, To, V> void transfer(final From from,
final To to, final GenericMapping<From, ?> getter,
final GenericMapping<To, V> setter) {
// This will throw an exception if the mappings are invalid
final V value = (V) getter.getter.apply(from);
setter.setter.accept(to, value);
}
}
public static void main(String[] args) {
final Mapper mapper = new Mapper();
// Mapping definition for class A
mapper.addMapping(new GenericMapping<>(1, A.class,
A::getIntegerA, A::setIntegerA));
mapper.addMapping(new GenericMapping<>(2, A.class,
A::getStringA, A::setStringA));
mapper.addMapping(new GenericMapping<>(3, A.class,
A::getFloatA, A::setFloatA));
// Mapping definition for class B
mapper.addMapping(new GenericMapping<>(1, B.class,
B::getIntegerB, B::setIntegerB));
mapper.addMapping(new GenericMapping<>(2, B.class,
B::getStringB, B::setStringB));
// Mapping definition for class C
mapper.addMapping(new GenericMapping<>(2, C.class,
C::getStringC, C::setStringC));
mapper.addMapping(new GenericMapping<>(3, C.class,
C::getFloatC, C::setFloatC));
// Use the mappings
A a = new A(7, "foo", 3.7f);
B b = new B();
C c = new C();
System.out.printf("A before map: %s%n", a);
System.out.printf("B before map: %s%n", b);
System.out.printf("C before map: %s%n", c);
// This will transfer a.integerA to b.integerB and a.stringA to b.stringB
mapper.map(a, b);
// This will transfer a.stringA to c.stringC and a.floatA to c.floatC
mapper.map(a, c);
System.out.println();
System.out.printf("A after map: %s%n", a);
System.out.printf("B after map: %s%n", b);
System.out.printf("C after map: %s%n", c);
}
}
And the result after running it:
A before map: A{integerA=7, stringA='foo', floatA=3.7}
B before map: B{integerB=null, stringB='null'}
C before map: C{floatC=null, stringC='null'}
A after map: A{integerA=7, stringA='foo', floatA=3.7}
B after map: B{integerB=7, stringB='foo'}
C after map: C{floatC=3.7, stringC='foo'}
Java 7
The same general solution can be used for Java 7, but it will be a lot more verbose. Since Java 7 doesn't have the functional interfaces Function<U, V> and BiConsumer<U, V> you'll need to define these yourself, which isn't that much trouble. It could be argued that they should be defined in Java 8 too so interface and method names makes more sense (e.g. Getter.get and Setter.set).
The big thing is the mapping definitions which will have to use anonymous classes instead of lambdas - lambdas is mostly syntactic sugar for anonymous classes with only one method anyways, but they make the code a lot more readable.
The mapping for a.integerA will look like this in Java 7:
mapper.addMapping(new GenericMapping<>(1, A.class,
new Function<A, Integer>() {
#Override
public Integer apply(final A a1) {
return a1.getIntegerA();
}
},
new BiConsumer<A, Integer>() {
#Override
public void accept(final A a1, final Integer integerA) {
a1.setIntegerA(integerA);
}
}));
You could also have a look at Apache Commons BeanUtils, which also have a quite sophisticated, although explicit (not Annotation-based), Converter API:
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.3/apidocs/org/apache/commons/beanutils/Converter.html
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.3/apidocs/org/apache/commons/beanutils/ConvertUtilsBean.html
I've got widely used method like:
public Map<String, Double> parseData(String[] data) {
.................
Where data is something like new String[] { "column1 -> 2.00", "column2 -> New York", ... }
Problem: It appears that data can contains both: String -> Double & String -> String values. So I need smth like:
public Map<String, String or Double> parseData(String[] data) {
................
Question: Any ideas besides return Map<String, Object>?
Create a Wrapper StringOrDouble which will look a bit like this:
public class StringOrDouble {
private String internalString;
private Double internalDouble;
public StringOrDouble(String input) {
internalString = input;
}
public StringOrDouble(Double input) {
internalDouble = input;
}
public boolean hasString() {
return internalString != null;
}
public boolean hasDouble() {
return internalDouble != null;
}
public String getString() {
return internalString;
}
public Double getDouble() {
return internalDouble;
}
}
Then have a map of type Map<String, StringOrDouble> and use that. When you use the values, you can check which one it is by testing with hasString() and/or hasDouble(). Alternatively you could have an enum which determines which type it is.
public Map<String, Container> parseData(String[] data)
You can introduce a wrapper class for this
public class Container {
private String s;
private Double d;
public Container(String s) {
this.s=s;
}
public Container(Double d) {
this.d=d;
}
public hasString() {
return s!=null;
}
public hasDouble() {
return d!=null;
}
//getters/setters
}
As far as I understand, you want something like Map<String, ? extends String || Double as the return type, but no such thing is supported in Java:
4.9 Intersection Types An intersection type takes the form T1 & ... & Tn, n>0, where Ti, 1in, are type expressions. Intersection types arise
in the processes of capture conversion (§5.1.10) and type inference
(§15.12.2.7). It is not possible to write an intersection type
directly as part of a program; no syntax supports this. The values of
an intersection type are those objects that are values of all of the
types Ti, for 1in.
So you'd better parse the input array and hold different arrays for each different type or you can use a wrapper class to represent the values in the map returned, as some other answerers explained.
Use superclass:
public Map<String, Object> parseData(String[] data)
Just an alternative to #blalasaadri. don't pretend to be better:
public static class StringDoubleValue {
private final Optional<String> sValue;
private final Optional<Double> dValue;
public MetricValue(String sValue) {
this.sValue = Optional.of(sValue);
this.dValue = Optional.absent();
}
public MetricValue(Double dValue) {
this.sValue = Optional.absent();
this.dValue = Optional.of(dValue);
}
public Object get() {
return (sValue.isPresent()) ? sValue.get() : dValue.get();
}
#Override
public String toString() {
if (sValue.isPresent()) ? sValue.get() : dValue.get().toString();
}
}
How can I find an object, Carnet, in a ArrayList<Carnet> knowing its property codeIsin.
List<Carnet> listCarnet = carnetEJB.findAll();
public class Carnet {
private String codeTitre;
private String nomTitre;
private String codeIsin;
// Setters and getters
}
In Java8 you can use streams:
public static Carnet findByCodeIsIn(Collection<Carnet> listCarnet, String codeIsIn) {
return listCarnet.stream().filter(carnet -> codeIsIn.equals(carnet.getCodeIsin())).findFirst().orElse(null);
}
Additionally, in case you have many different objects (not only Carnet) or you want to find it by different properties (not only by cideIsin), you could build an utility class, to ecapsulate this logic in it:
public final class FindUtils {
public static <T> T findByProperty(Collection<T> col, Predicate<T> filter) {
return col.stream().filter(filter).findFirst().orElse(null);
}
}
public final class CarnetUtils {
public static Carnet findByCodeTitre(Collection<Carnet> listCarnet, String codeTitre) {
return FindUtils.findByProperty(listCarnet, carnet -> codeTitre.equals(carnet.getCodeTitre()));
}
public static Carnet findByNomTitre(Collection<Carnet> listCarnet, String nomTitre) {
return FindUtils.findByProperty(listCarnet, carnet -> nomTitre.equals(carnet.getNomTitre()));
}
public static Carnet findByCodeIsIn(Collection<Carnet> listCarnet, String codeIsin) {
return FindUtils.findByProperty(listCarnet, carnet -> codeIsin.equals(carnet.getCodeIsin()));
}
}
You can't without an iteration.
Option 1
Carnet findCarnet(String codeIsIn) {
for(Carnet carnet : listCarnet) {
if(carnet.getCodeIsIn().equals(codeIsIn)) {
return carnet;
}
}
return null;
}
Option 2
Override the equals() method of Carnet.
Option 3
Storing your List as a Map instead, using codeIsIn as the key:
HashMap<String, Carnet> carnets = new HashMap<>();
// setting map
Carnet carnet = carnets.get(codeIsIn);
If you use Java 8 and if it is possible that your search returns null, you could try using the Optional class.
To find a carnet:
private final Optional<Carnet> findCarnet(Collection<Carnet> yourList, String codeIsin){
// This stream will simply return any carnet that matches the filter. It will be wrapped in a Optional object.
// If no carnets are matched, an "Optional.empty" item will be returned
return yourList.stream().filter(c -> c.getCodeIsin().equals(codeIsin)).findAny();
}
Now a usage for it:
public void yourMethod(String codeIsin){
List<Carnet> listCarnet = carnetEJB.findAll();
Optional<Carnet> carnetFound = findCarnet(listCarnet, codeIsin);
if(carnetFound.isPresent()){
// You use this ".get()" method to actually get your carnet from the Optional object
doSomething(carnetFound.get());
}
else{
doSomethingElse();
}
}
To find an object in an ArrayList by the property, We can use a function like this:
To find all the objects with a specific codeIsIn:
public static List<Item> findBycodeIsin(Collection<Carnet> listCarnet, String codeIsIn) {
return items.stream().filter(item -> codeIsIn.equals(item.getCodeIsIn()))
.collect(Collectors.toList());
}
To find a Single item (If the codeIsIn is unique for each object):
public static Carnet findByCodeIsIn(Collection<Carnet> listCarnet, String codeIsIn) {
return listCarnet.stream().filter(carnet-> codeIsIn.equals(carnet.getCodeIsIn()))
.findFirst().orElse(null);
}
Here is a solution using Guava
private User findUserByName(List<User> userList, final String name) {
Optional<User> userOptional =
FluentIterable.from(userList).firstMatch(new Predicate<User>() {
#Override
public boolean apply(#Nullable User input) {
return input.getName().equals(name);
}
});
return userOptional.isPresent() ? userOptional.get() : null; // return user if found otherwise return null if user name don't exist in user list
}
Here is another solution using Guava in Java 8 that returns the matched element if one exists in the list. If more than one elements are matched then the collector throws an IllegalArgumentException. A null is returned if there is no match.
Carnet carnet = listCarnet.stream()
.filter(c -> c.getCodeIsin().equals(wantedCodeIsin))
.collect(MoreCollectors.toOptional())
.orElse(null);
Following with Oleg answer, if you want to find ALL objects in a List filtered by a property, you could do something like:
//Search into a generic list ALL items with a generic property
public final class SearchTools {
public static <T> List<T> findByProperty(Collection<T> col, Predicate<T> filter) {
List<T> filteredList = (List<T>) col.stream().filter(filter).collect(Collectors.toList());
return filteredList;
}
//Search in the list "listItems" ALL items of type "Item" with the specific property "iD_item=itemID"
public static final class ItemTools {
public static List<Item> findByItemID(Collection<Item> listItems, String itemID) {
return SearchTools.findByProperty(listItems, item -> itemID.equals(item.getiD_Item()));
}
}
}
and similarly if you want to filter ALL items in a HashMap with a certain Property
//Search into a MAP ALL items with a given property
public final class SearchTools {
public static <T> HashMap<String,T> filterByProperty(HashMap<String,T> completeMap, Predicate<? super Map.Entry<String,T>> filter) {
HashMap<String,T> filteredList = (HashMap<String,T>) completeMap.entrySet().stream()
.filter(filter)
.collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue()));
return filteredList;
}
//Search into the MAP ALL items with specific properties
public static final class ItemTools {
public static HashMap<String,Item> filterByParentID(HashMap<String,Item> mapItems, String parentID) {
return SearchTools.filterByProperty(mapItems, mapItem -> parentID.equals(mapItem.getValue().getiD_Parent()));
}
public static HashMap<String,Item> filterBySciName(HashMap<String,Item> mapItems, String sciName) {
return SearchTools.filterByProperty(mapItems, mapItem -> sciName.equals(mapItem.getValue().getSciName()));
}
}
For finding objects which are meaningfully equal, you need to override equals and hashcode methods for the class. You can find a good tutorial here.
http://www.thejavageek.com/2013/06/28/significance-of-equals-and-hashcode/