So I searched for Java7 that can perform smart collection operations like Stream and LinQ. (If you know any other way to avoid turning the for statement, please let me know)
Awesome Java: Great Java Framework Library Software It was introduced in the above article.
CQEngine Official repository Github: CQengin
License The license of "CQ Engine" is "Apache License 2.0".
// already defined Attribute in Car Class
// search brand-new AAACar
uery<Car> query1 = and(
endsWith(Car.NAME, "AAACar"),
equal(Car.DISCREPRION, "brandNew")
);
ResultSet<car> resultSet = cars.retrieve(query1));
import static com.googlecode.cqengine.codegen.AttributeBytecodeGenerator.*;
// generate Attribute
CQNParser<Car> parser = CQNParser.forPojoWithAttributes(Car.class, createAttributes(Car.class));
// search brand-new AAACar
ResultSet<Car> results = parser.retrieve(cars,
"and("
+ "equal(\"name\",\"AAACar\"),"
+ "equal(\"discription\", \"brandNew\")"
+ " )");
// generate Attribute
SQLParser<Car> parser = SQLParser.forPojoWithAttributes(Car.class, createAttributes(Car.class));
// search brand-new AAACar
ResultSet<Car> results = parser.retrieve(cars,
"SELECT * FROM cars " +
"WHERE (" +
" name = 'AAACar' " +
" AND discription = 'brandNew')"
);
for (Car car : results ) {
System.out.println(car.toString());
}
Maven ~~ The latest version at the moment is included ~~ Insert "2.12.4" that can be used with Java 7.
<dependency>
<groupId>com.googlecode.cqengine</groupId>
<artifactId>cqengine</artifactId>
<version>2.12.4</version>
</dependency>
Looking at the release notes, it seems that Ver3 to Java7 and below are excluded. CQEngine Release Notes
I made a Car. * @Data is described because lombok is used.
@Data
public class Car {
private int carId;
private String name;
private String discription;
private List<String> features;
}
// create car collection
Set<Car> cars = new HashSet<>();
cars.add( Car.builder().carId(0).name("AAACar").discription("brandNew").features( Arrays.asList("spare tyre", "sunroof")).build() );
cars.add( Car.builder().carId(1).name("BBBCar").discription("used").features( Arrays.asList("spare tyre", "radio")).build() );
cars.add( Car.builder().carId(2).name("CCCCar").discription("used").features( Arrays.asList("radio", "sunroof")).build() );
cars.add( Car.builder().carId(3).name("DDDCar").discription("brandNew").features( Arrays.asList("spare tyre", "sunroof")).build() );
cars.add( Car.builder().carId(4).name("AAACar").discription("used").features( Arrays.asList("radio", "sunroof")).build() );
cars.add( Car.builder().carId(5).name("BBBCar").discription("brandNew").features( Arrays.asList("spare tyre", "radio")).build() );
cars.add( Car.builder().carId(6).name("CCCCar").discription("brandNew").features( Arrays.asList("spare tyre", "sunroof")).build() );
cars.add( Car.builder().carId(7).name("DDDCar").discription("used").features( Arrays.asList("radio", "sunroof")).build() );
cars.add( Car.builder().carId(8).name("AAACar").discription("used").features( Arrays.asList("spare tyre", "sunroof")).build() );
cars.add( Car.builder().carId(9).name("BBBCar").discription("used").features( Arrays.asList("spare tyre", "radio")).build() );
cars.add( Car.builder().carId(10).name("CCCCar").discription("brandNew").features( Arrays.asList("spare tyre", "sunroof")).build() );
// search brand-new AAACar
Set<Car> results = new HashSet<>();
for (Car car : cars) {
if ( Objects.equals(car.getName(), "AAACar")
&& Objects.equals(car.getDiscription(), "brandNew") ) {
results.add(car);
}
}
for (Car car : results ) {
System.out.println(car.toString()); // => Car(carId=0, name=AAACar, discription=brandNew, features=[spare tyre, sunroof])
}
Since it is intended to be used as a substitute for Stream, we will verify it with a policy of not adding Attribute to POJO. Try using CQN notation without Attribute. Generate attributes from private class fields It seems difficult to get private fields of POJO, so I set the setting to use getter in createAttributes.
// generate Attribute
CQNParser<Car> parser = CQNParser.forPojoWithAttributes(Car.class, createAttributes(Car.class, MemberFilters.GETTER_METHODS_ONLY));
// create car collection
IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
cars.add( Car.builder().carId(0).name("AAACar").discription("brandNew").features( Arrays.asList("spare tyre", "sunroof")).build() );
//(abridgement)
cars.add( Car.builder().carId(10).name("CCCCar").discription("brandNew").features( Arrays.asList("spare tyre", "sunroof")).build() );
// search brand-new AAACar
ResultSet<Car> results = parser.retrieve(cars,
"and("
+ "equal(\"getName\",\"AAACar\"),"
+ "equal(\"getDiscription\", \"brandNew\")"
+ " )");
for (Car car : results ) {
System.out.println(car.toString()); // => Car(carId=0, name=AAACar, discription=brandNew, features=[spare tyre, sunroof])
}
// create car collection
Set<Car> cars = new HashSet<>();
cars.add( Car.builder().carId(0).name("AAACar").discription("brandNew").features( Arrays.asList("spare tyre", "sunroof")).build() );
//(abridgement)
cars.add( Car.builder().carId(10).name("CCCCar").discription("brandNew").features( Arrays.asList("spare tyre", "sunroof")).build() );
// search brand-new AAACar
Set<Car> results = cars.stream()
.filter(car -> Objects.equals(car.getName(), "AAACar")
&& Objects.equals(car.getDiscription(), "brandNew") )
.collect(Collectors.toSet());
for (Car car : results ) {
System.out.println(car.toString());
}
Since Java7 is not supported for CQEngine Ver3 and above, the following error will occur.
Unsupported major.minor version 52.0
Lightweight-Stream-API-I want to use Stream API with Java 6 and 7 This seems to be a library that can be used like Stream as it is When migrating to Java8, this library is used before migration, and the code after migration is Java8 Stream. I thought I had to be careful not to get into a chaotic situation This time, I decided to investigate from the viewpoint of whether CQ Engine can exceed the standard library even for Java 8 or later. So, in the future, I would like to evaluate the performance of CQ Engine in mass data processing.
(I will write in another article) It is difficult to prepare the data, so look for an API that can get a lot of data. Can be used by individuals! Recommended API list
Recommended Posts