Combine lists of 2 different objects into list of single object using Java 8 Stream API - arraylist

I have two lists as below:
List ids = Arrays.asList(1,2,3);
List reps = Arrays.asList("abc","pqr","xyz");
Now I want to create list of Prediction objects with values mapped from above two lists in sequence like below:
List results = [ Prediction(1,"abc") , Prediction(2,"pqr"), Prediction(3,"xyz") ]
class Prediction {
int id;
String rep;
}
How this can be done using Java8 Stream API.

The operation you described is called zipping.
If you are sure the lists are evenly of length, you could do something like:
IntStream.range(0, ids.size())
.mapToObj(i -> new Prediction(ids.get(i), reps.get(i)))
.toList()
Assuming the Prediction class has a constructor like that...

Related

Merge 2 Immutable JS Ordered Map objects

I am using Immutable JS as a redux store for my React Native App. I have 2 Ordered Map which is a Keyed Collection of Lists. When I try to merge these 2 Ordered Maps, if the keys overlap, then the data is being overwritten.
For eg: Consider I have 2 Ordered Maps with dates as their keys. When I merge them, if both of them have keys with the same date, the data of that key will be replaced. How can I concatenate them without losing data?
OrderedMap1: {
'21-07-2017': List(10),
'22-07-2017': List(10),
'23-07-2017': List(10),
'24-07-2017': List(10)
}
OrderedMap2: {
'24-07-2017': List(5)
}
When I try to merge them, the data # key '24-07-2017' gets replaced
OrderedMap1.merge(OrderedMap2) gives
{
'21-07-2017': List(10),
'22-07-2017': List(10),
'23-07-2017': List(10),
'24-07-2017': List(5)
}
I have tried concat(), merge() and mergeDeep() methods
Maybe http://facebook.github.io/immutable-js/docs/#/Map/mergeWith is what you're looking for. In the merger function you can concat the lists.
According the the provided example for mergeWith it should look like this, I guess:
OrderedMap1.mergeWith(
(dates1, dates2) => dates1.concat(dates2),
OrderedMap2
)

Using lucene to search data differently for different users conditionally

Consider that the entities that I need to perform text search are as following
Sample{
int ID, //Unique ID
string Name,//Searchable field
string Description //Searchable field
}
Now, I have several such entities which are commonly shared by all the users but each user can associate different tags, Notes etc to any of these entities. For simplicity lets say a user can add tags to a Sample entity.
UserSampleData{
int ID, //Sample ID
int UserID, //For condition
string tags //Searchable field
}
When a user performs search, I want to search for the given string in the fields Name, Description and tags associated to that Sample by the current user. I am pretty new to using lucene indexing and I am not able to figure how can I design a index and also the queries for such a situation. I need the results sorted on the relevance with the search query. Following approaches crossed my mind, but I have a feeling there could be better solutions:
Separately query 2 different entities Samples and UserSampleData and somehow mix the 2 results. For the results that intersect, we need to combine the match scores by may be averaging.
Flatten out the data by combining both the entities => multiple entries for same ID.
You could use a JoinUtil Lucene class but you must rename the second "ID" field of UserDataSample document into SAMPLE_ID (or another name different from "ID").
Below an example:
r = DirectoryReader.open(dir);
final Version version = Version.LUCENE_47; // Your lucene version
final IndexSearcher searcher = new IndexSearcher(r);
final String fromField = "ID";
final boolean multipleValuesPerDocument = false;
final String toField = "SAMPLE_ID";
String querystr = "UserID:xxxx AND yourQueryString"; //the userID condition and your query String
Query fromQuery = new QueryParser(version, "NAME", new WhitespaceAnalyzer(version)).parse(querystr);
final Query joinQuery = JoinUtil.createJoinQuery(fromField, multipleValuesPerDocument, toField, fromQuery, searcher, ScoreMode.None);
final TopDocs topDocs = searcher.search(joinQuery, 10);
Check the bug https://issues.apache.org/jira/browse/LUCENE-4824). I don't know if the bug is automatically solved into the current version of LUCENE otherwise I think you must convert the type of your ID fields to String.
I think that you need Relational Data. Handling relational data is not simple with Lucene. This is a useful blog post for.

Linq where a record contains 2 matched fields

I’m working with an existing database with a design I’m not in control of, I’m using EF4, and querying using LINQ. I work in VB.Net but would be quite happy to translate a c# solution.
I would like to pull records from a table where two of the fields match a pair of items from a list.
So i have a list of
Public Class RequestInfo
Public Property INSP_ROUTINE_NM As String
Public Property FEATURE_ID As String
End Class
And I would like to query a table and pull any records where both INSP_ROUTINE_NM and FEATURE_ID match one of the Request Info items.
I can use contains easy enough on either of the fields
Dim Features = (From F In MLDb.TBL_FeatureInfoSet _
Where (C_Request.Select(Function(x) x.INSP_ROUTINE_NM)).Contains(F.INSP_ROUTINE_NM) Select F.FEATURE_ID, F.FEATURE_RUN_NO, F.INSP_ROUTINE_NM).ToList
I could use two contains calls but that would pull any record where both records match somewhere in the list not necessarily any one pair from the request.
You can try this:
C#
var Features= (from f in MLDb.TBL_FeatureInfoSet
let q = C_Request.Select(x=>x.INSP_ROUTINE_NM)
where q.Contains(f.INSP_ROUTINE_NM) || q.Contains(f.INSP_ROUTINE_NM)
// where q.Contains(f.INSP_ROUTINE_NM) && q.Contains(f.INSP_ROUTINE_NM)
select new {f.FEATURE_ID, f.FEATURE_RUN_NO}).ToList();

How to split information on different tables within one report?

I need to display the information of the Main Report in two tables, each of them containing data filtered based on a condition X (1- Bigger than X, 2 - Smaller than X). I tried to create separate data-sets for each table but it leads to an increase on time load. How can I fill different tables within the report, with different filtered information based on main report?
If you have a field listRecords of type collection with all your records, you can define the table datasource as :
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(
Collections2.filter($F{listRecords}, new Predicate<Integer>() {
#Override
public boolean apply(final Integer input) {
return input > 2;
}
})
)
Using the guava library to filter the collection.

going through an A4Solution

I am currently using the Alloy api in my project, and I need to display A4Solutions.I can do that easily with the vizualiser Alloy provides (vizGUI) , but it is a bit too limited for my purpose. So I am willing to generate my own graphs ( using any other graph api ) from an A4Solution objects.
I was able to get the Atoms without any problems (that was pretty straight forward ) but I can't really see how to retrieve the relations between those atoms.
I looked online for some example about how to parse an A4Solution, but found nothing unfortunately.
Relations, or fields, you can retrieve from sigs, and then you can evaluate them to obtains concrete atoms, something like this:
A4Solution sol = ...;
SafeList<Sig> sigs = sol.getAllReachableSigs();
for (Sig sig : sigs) {
SafeList<Field> fields = sig.getFields();
for (Field field : fields) {
A4TupleSet ts = (A4TupleSet)(sol.eval(field));
for(A4Tuple t: ts)
for(int i=0; i<t.arity(); i++)
t.atom(i);
}
}