[CQ Engine] I want to handle collections like Stream or .Net LINQ even in Java 7.

background

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)

About CQ Engine

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".

How to describe collection operations in CQEngine

Use of Query

// 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));


Using CQN dialects

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\")"
								+ " )");

Using SQL dialects

// 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());
}

Verification

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

Preparing data for comparison

Preparing a model for the collection

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;
}

Comparison

Java for statement

// 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])
}

Use CQN (CQ Engine Native)

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])
}

Java 8 Stream

// 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());
}

How to use CQ Engine with Java 7 or lower

Since Java7 is not supported for CQEngine Ver3 and above, the following error will occur.

 Unsupported major.minor version 52.0

Conclusion

Use Stream more straightforwardly with Java7-

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.

Use with large amounts of data

(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

[CQ Engine] I want to handle collections like Stream or .Net LINQ even in Java 7.
I want to do something like "cls" in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
I want to send an email in Java.
rsync4j --I want to touch rsync in Java.
I want to be eventually even in kotlin
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
How to handle exceptions coolly with Java 8 Stream or Optional
I want to create a Parquet file even in Ruby
I want to simplify the conditional if-else statement in Java
[Java] I want to check that the elements in the list are null or empty [Collection Utils]
[Java] I want to perform distinct with the key in the object
I want to get the IP address when connecting to Wi-Fi in Java
I want to ForEach an array with a Lambda expression in Java
[Java Spring MVC] I want to use DI in my own class
I want to RSpec even at Jest!
I want to stop Java updates altogether
I want to use @Autowired in Servlet
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (gray magic that is not so much as black magic)
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Royal road edition that is neither magic nor anything)
Run R from Java I want to run rJava
I want to use arrow notation in Ruby
I tried to implement deep learning in Java
I want to use java8 forEach with index
I want to pass APP_HOME to logback in Gradle
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to output multiplication table in Java
[Xcode] I want to manage images in folders
I want to write quickly from java to sqlite
I tried to create Alexa skill in Java
I want to get the value in Ruby
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
I want to return an object in CSV format with multi-line header & filter in Java
# 1_JAVA I want to get the index number by specifying one character in the character string.