I'm using jopendocument 1.2 with Railo 3.3.1.000
from http://www.jopendocument.org/start_text_2.html
List<Map<String, String>> months = new ArrayList<Map<String, String>>();
months.add(createMap("January", "-12", "3"));
months.add(createMap("February", "-8", "5"));
months.add(createMap("March", "-5", "12"));
months.add(createMap("April", "-1", "15"));
months.add(createMap("May", "3", "21"));
template.setField("months", months);
How to write that code in cfml, or anyone have experience with jopendocument to add row in odt template file with cfml?
List<Map<String, String>> months = new ArrayList<Map<String,
String>>();
In CF terms, that code creates an array of structures. Because java is strongly typed the code uses generics to indicate what type of objects each one contains
List< Map<...> > // Array containing structures
Map< String, String > // Structure containing "String" values
Fortunately CF arrays are java.util.List objects internally and structures are java.util.Map objects. So you only need to create a CF array of structures with the proper keys and values. Then pass the array into template.setField(...).
I was not sure which keys to use in the structure, so I downloaded the "test.odt" template from jOpenDocument-template-1.2.zip. It revealed each structure should contain three (3) keys, one for each column in the table: name, min, max. As long as you populate the structures with strings, this should work:
// Create an array of structures. Each structure represents a table row.
// The key names for columns 1-3 are: "name", "min", "max"
months = [
{name="January", min="-12", max="3"}
, {name="February", min="-8", max="5"}
, {name="March", min="-5", max="12"}
, {name="April", min="-1", max="15"}
, {name="May", min="3", max="21"}
, {name="June", min="5", max="32"}
];
// populate table rows
template.setField("months", months);
Related
I can add individual values from the detected row to a LIST<> one at a time as follows
var currentRowIndex = myGrid.Items.IndexOf(myGrid.CurrentItem);
int inNum = currentRowIndex;
TextBlock x0 = myGrid.Columns[0].GetCellContent(myGrid.Items[inNum]) as TextBlock;
Global.vpItems.Add(x0.Text);
Since I have about 150 values, this is not efficient at all. Is there a way to directly populate the List (aka vpItems)
Any help is greatly appreciated
I'm new to scala and trying to concatenate two varying size list based on condition,
Below are the lists,
val check1:String = "NULL||BLANK||LENGTH"
val check2:String = "LENGTH||DUPLICATE"
val check3:String = "NUMERIC"
val checkLists = List(check1,check2,check3)
checkLists: List[String] = List(NULL||BLANK||LENGTH, LENGTH||DUPLICATE, NUMERIC)
val condList = List(">=2","<7")
I'm trying to concatenate checkLists & condList based on condition and create new list, whenever List contains String "LENGTH" it should concatenated with condList like below
List(NULL||BLANK||LENGTH~>=2, LENGTH~<7||DUPLICATE, NUMERIC)
I can able to use zip, foreach and case to concatenate of two equal size lists but here I'm facing trouble with different size lists.
Using zipAll will give the answer you are looking for:
checkLists.zipAll(condList, "", "").map {
case (check, cond) => check.replaceAll("LENGTH", "LENGTH~" + cond)
}
List(NULL||BLANK||LENGTH~>=2, LENGTH~<7||DUPLICATE, NUMERIC)
The missing element of condList is given as "", but a different default condition could be used if required.
Note that if the second LENGTH string is in the third element of checkLists rather than the second element, it will not get any condition. This may or may not be what is required.
I am trying to learn to do this in Java 8 using streams.
Given the follow:
List<PartModel> models = new ArrayList<>();
models.add(new PartModel("Part1", "AModel"));
models.add(new PartModel("Part1", "BModel"));
models.add(new PartModel("Part2", "AModel"));
I would like to convert this list into a map: Map<String, Set<String> where the values would be:
"Part1" -> ["AModel", "BModel"]
"Part2" -> ["AModel"]
How can I achieve this with using a Java 8 stream?
You can use the groupingBy collector like this:
Map<String, Set<String>> partModelMap = models.stream()
.collect(Collectors.groupingBy(Partmodel::getPart,
Collectors.mapping(Partmodel::getModel, Collectors.toSet())));
The Collectors.mapping part will map the values of the map from List<PartModel> to a Set<String>.
We can group by getPart() and then return a Map<Integer, Set<String>>
Map<Integer, Set<String>> collect = models.stream()
.collect(Collectors.groupingBy(PartModel::getPart,
Collectors.mapping(PartModel::getModel), Collectors.toSet())));
A simple stream grouping by the first field and transforming the groups can look like this:
Map<String, String[]> map = models.stream()
.collect(Collectors.groupingBy(PartModel::getPartName,
Collectors.mapping(PartModel::getModelName, Collectors.toList())))
.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey,
entry -> entry.getValue().toArray(new String[entry.getValue().size()])));
This question already has answers here:
How to filter a Java Collection (based on predicate)?
(29 answers)
Closed 6 years ago.
I want to search in a List and my List is look like
List<Employee> oneEmp= new ArrayList<Employee>();
List<Employee> twoEmp= new ArrayList<Employee>();
oneEmp= [Employee [eid=1001, eName=Sam Smith, eAddress=Bangluru, eSalary=10000000], Employee [eid=0, eName=, eAddress=, eSalary=null], Employee [eid=1003, eName=Amt Lime, eAddress=G Bhagyoday, eSalary=200000], Employee [eid=1004, eName=Ash Wake, eAddress=BMC, eSalary=200000], Employee [eid=1005, eName=Will Smith, eAddress= Delhi, eSalary=200000], Employee [eid=1006, eName=Shya Ymwar, eAddress=Madras, eSalary=50000], Employee [eid=1007, eName=Nag Gam, eAddress=Pune, eSalary=10000000], Employee [eid=1008, eName=Arti, eAddress=Delhi, eSalary=10000000]]
twoEmp= [Employee [eid=0, eName=null, eAddress=null, eSalary=100000], Employee [eid=0, eName=null, eAddress=null, eSalary=50000], Employee [eid=0, eName=null, eAddress=null, eSalary=200000]]
I am using code like this:-
for(Employee two : twoEmp){
for (Iterator<Employee> iterator = oneEmp.iterator(); iterator.hasNext(); ) {
Employee e = iterator.next();
if (e.geteSalary() != null && two.geteSalary() != null && e.geteSalary().compareTo(two.geteSalary()) == 0) {
finalEmpList.add(e);
}
}
}
But this still required 2 for loop
I am using JAVA 1.6
My Employee class has attributes:
//Employee class
int eid;
BigInteger eSalary;
String eName, eAddress;
Now I want to get all the objects in List who's Salary = 10000000
result should be :
[Employee [eid=1001, eName=Sam Smith, eAddress=Bangluru, eSalary=10000000], Employee [eid=1007, eName=Nag Gam, eAddress=Pune, eSalary=10000000], Employee [eid=1008, eName=Arti, eAddress=Delhi, eSalary=10000000],.........................]
I would like to achieve this without using any loop or minimum loop required because data will be large
Yes, it is possible to avoid the loop using streams.
First, consider using a generic collection:
List<Employee> employees = new ArrayList<>():
//add all employees to the list
Now you can use streams to filter your list
List<Employee> filtered = employees.stream()
.filter(emp -> emp.getSalary() == 10000000)
.collect(Collectors.toList());
Edit: Probably Stream library is still using some kind of loop internally but while its implementation is hidden from me I do not worry.
A List is a sequential container, to do any kind of filtering on a list, your only option is to iterate over it.
For the query you mentioned,you can use the Map data structure with a BigInteger type for the key (representing the salary) and a List<Employee> for the mapped value type. This will enable you to look for all the employees that earn a certain salary in constant time without having to iterate over the whole list.
Unfortunately though, this solution can't help you do any other queries like "how many employees earn more than 60000", to preform all types of queries on a large data set you should use a database.
PS: You don't need to use the BigInteger type for the salary, unless you think someone earns more than 2,147,483,647
Something like this should do the trick; iterate over the List, and remove the items which you don't want, leaving only the ones which you do want.
List myList = new ArrayList(); //... add items
[...]
for (Iterator<Employee> iterator = myList.iterator(); iterator.hasNext(); ) {
Employee e = iterator.next();
if (e.getSalary() != 10000000) {
iterator.remove();
}
}
//your list now contains only employees whose salary = 10000000
Edit: And no, you cannot do this without a loop. In order to do this kind of thing, you have to iterate over your Collection using a loop. Even if you use a library or the Java Streams API to do this, it will still use a loop of some sort under the hood. However, this will be quite efficient, even with as large dataset. (How large ? Why do you want to avoid using a loop ?)
I'm using PicoJSON to handle JSON that I get from JavaScript into C++.
If I have a JSON with keys and values of the form {"val1":3, "val2":4} I can get the value of every specific key like so:
picojson::value v;
const string json2("{\"val1\":3,\"val2\":4}");
int val1 = (int)v.get("val1").get<double>(); // val1 will be equal to 3
int val2 = (int)v.get("val2").get<double>(); // val2 will be equal to 4
The problem is that I have an array of objects coming from JavaScript in the form of JSON that look like [{"name": 3},{"name": 1},{"name": 2}]. So now the code will look like this:
picojson::value v;
const string json1("[{\"name\": 3},{\"name\": 1},{\"name\": 2}]");
I am expected to sort them so the expected output should be:
"[{\"name\": 1},{\"name\": 2},{\"name\": 3}]"
Do I have to use a 'for loop' with a linked list to somehow extract that data to sort it? If so, how?
Maybe this can be extracted using regular expressions? If so, how?
Should the array can be converted into array of arrays in order to sort it? If so, how?
Do I have to build a struct or a vector to get a sortable data structure recognisable by
C++? If so, how?
Note: The number of elements varies - it can be greater/smaller.
EDIT:
Can this task be solved if the data structure is in this form (just like I had with {"val1":3, "val2":4} ) :
{"name3": 3, "name1" : 1, "name2": 2, "name97" : 97, ... }
I am flexible to changing it so that C++ would be able to handle it.
Would it then be possible to traverse every key and sort in by value ? If yes, how to do it?
You can put your data into a vector and sort it.
array arr = v.get<array>();
vector<int> vi;
for (array::const_iterator it = arr.begin(); it != arr.end(); ++it) {
vi.push_back((int)(*it).get("name").get<double>());
}
sort(vi.begin(), vi.end());
stringstream ss;
vector<int>::const_iterator it;
for (ss<<"[", it = vi.begin(); (it+1) != vi.end(); ++it) {
ss<<"{\"name\": "<<(*it)<<"},";
}
ss<<"{\"name\": "<<(*it)<<"}]";
cout<<"Output: "<<ss.str()<<endl;
Here is outputs:
Input: [{"name": 3},{"name": 1},{"name": 2}]
Output: [{"name": 1},{"name": 2},{"name": 3}]