I have the following result set :
Name Occurrence
Jack 2
Jill 1
Tom 3
I want to put this in HashMap? I was able to display only the one entry
Or is it best to use a ArrayList ?
Dictionary<string, int> dictionary =
new Dictionary<string, int>();
dictionary.Add("Jack ", 2);
dictionary.Add("Jill ", 1);
dictionary.Add("Tom ", 3);
Related
I have a native query
searchSql = "Select firstname,lastname from students order by id desc"
and then i do
Query query = entityManager.createNativeQuery(searchSql);
List<Map<String, Object>> results = query.getResultList();
Now if i print the results with KEYS
List<String>headers = new ArrayList<>();
for(String header : results.get(0).keySet()){
headers.add(header);
}
i get random order of the column names.
How can i get the exact order as in the select statement ?
LinkedHashMap should be the answer but i get class cast exceptions... Any generic ideas?
I checked if LinkedHashMap causes the randomness of columns:
#Test
public void linkedHashMap() {
Map<String, Integer> map = new LinkedHashMap<>();
IntStream.range(0, 10).forEach(integer -> {
map.put(
String.valueOf(integer),
integer
);
});
for (String val : map.keySet()){
System.out.println(val);
}
}
but instead it prints:
0
1
2
3
4
5
6
7
8
9
The randomness seems to be a limitation of Hibernate, as stated here
If simply changing the signature to List<Object[]> results = query.getResultList(); does NOT work, then you can give a shot at SqlResultSetMapping. That way, the implicit map insertion would be avoided and order would be maintained I guess (I am not 100% sure, needs to be tested)
See https://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html.
I have a resultSet which is having multiple rows. I want to make a hashmap. But while storing the arrayList and making key as one of the columns, for each key all the rows are stored in arrayList.
That means for each key all the rows are stored rather than the exact rows for that key.
HashMap> map = new HashMap>();
Pojo pojo = null;
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList list = null;
try {
ps = DatabaseManager.prepareStatement(query);
ps.setInt(1, number);
rs = ps.executeQuery();
while(rs.next()){
pojo = new Pojo();
pojo.setId(rs.getInt("ID"));
pojo.setDate(rs.getDate("DATE"));
pojo.setTotal(rs.getDouble("TOTAL"));
list.add(pojo); map.put(rs.getInt("ID"), list);
}
rs.close();
So for all the keys the size of list is 15. While it should be like
ID Rows
1A 3
2A 4
3C 2
4W 6
So how the code should be to get the desired result?
You can try something like this,
while(rs.next()){
// Check if map contains list
if (map.contains(rs.getInt("ID"))
list = map.get(rs.getInt("ID"));
else
list = new ArrayList<Pojo>();
pojo = new Pojo();
pojo.setId(rs.getInt("ID"));
pojo.setDate(rs.getDate("DATE"));
pojo.setTotal(rs.getDouble("TOTAL"));
list.add(pojo);
map.put(rs.getInt("ID"), list);
}
Hope it works.
I have a doc like the following:
as you can see I have an array entity: {1,3,4}
Now I want to just change 4 to 10 in that array and update it for that I have the following code:
DBCollection coll = db.getCollection("test");
BasicDBObject newDocument = new BasicDBObject();
BasicDBObject searchQuery = new BasicDBObject().append("time", "20141105230000");
coll.update(searchQuery, newDocument);
String[] str = { "1", "3", "10" };
DBObject updateMatchingElem = new BasicDBObject("$set",
new BasicDBObject().append("entity", str));
coll.update(searchQuery, updateMatchingElem);
But this way is not a good way because I kind of remove entity and then insert the whole array again. Is there anyway that I can just change the one element like 4 to 10?
Now I want to just change 4 to 10 in that array and update it
You can do it in the following way, using the $ positional operator.
//db.collection.update({"entity":4},{$set:{"entity.$":10}})
DBObject find = new BasicDBObject( "entity", 4);
DBObject set = new BasicDBObject( "entity.$", 10);
DBObject update = new BasicDBObject().append("$set", set);
coll.update(find, update);
Note that you can at most update only one single matching array element, even if there are other matching elements in the array. For instance, if there are two 4s in the array, only the first occurrence of 4 will get updated. This is how the positional operator works.
Whenever you use the positional operator in the update query, the find query must contain the field in the find part of the query.
I have a query like this
start n = node:node_auto_index('ids:"123", "456" ... ') return n
Here 123, 456 is a list of keys as a single param {list}. Now when I try to write this in Java
String q = " START n=node:node_auto_index('key:{ids}') return n "
Map<String, Object> map = new HashMap<String, Object>();
map.put("ids", keyList); // keyList is a list of strings
But somehow calling graphstoreclient.executeCypher(q, map) fails with parse error, can you point me to any documentation / correct syntax on this.
PS - This query works fine on console.
Since you're supplying a lucene query string, parameterize the entire string:
String q = " START n=node:node_auto_index({ids}) return n "
Map<String, Object> map = new HashMap<String, Object>();
map.put("ids", keyList);
keyList should now look like ids:"123", "456" ...
I am trying to take an ArrayList of strings and separate it so that i can extract a title from each line.
Here is the contents of the ArrayList:
Tom Sawyer;Twain, Mark;1972;8.50;f;
Leaves of Grass;Whitman, Walt;1975;29.99;p;
Romeo and Juliet;Shakespeare, William;1980;4.99;d;
Great Gatsby;Fitzgerald, F. Scott;1979;5.99;f;
Scarlet Letter;Hawthorne, Nathaniel;1981;4.78;f;
Whisper of the River;Sams, Ferrol;1984;21.95;f;
Moby Dick;Melville, Herman;1962;13.98;f;
Last of the Mohicans;Cooper, James Fenimore;1968;8.75;f;
Odyssey;Homer;1950;9.99;f;
Christmas Carol;Dickens, Charles;1966;12.50;f;
Les Miserables;Hugo, Victor;1988;19.98;f;
Heart of Darkness;Conrad, Joseph;1970;14.45;f;
Animal Farm;Orwell, George;1978;10.00;f;
Canterbury Tales;Chaucer, Geoffrey;1965;20.00;d;
Old Man and the Sea;Hemingway, Ernest;1966;9.95;f;
I need to make a method or something that gets the first part of the line up until the ";".
Here is the code I was using for something similar except this is reading a data file:
while (datascanner.hasNext())
File datafile = new File (data name)
Scanner datascanner = new Scanner(datafile)
String data = datascanner.nextLine()
StringToeknizer dataParser = new StringTokenizer(data, ";")
String title = dataParser.nextToken()
I need something similar to this but instead it searches the arraylist and not the file. I will then extract the other contents on each line:
String author = dataParser.nextToken()
int CopyR = dataParser.nextToken()
and so on...
Just iterate through the list and apply your logic
for(String line : myArrayList)
{
StringTokenizer st = new StringTokenizer(line, ";");
String author = st.nextToken();
//etc.
}