Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Ok, I tried to ask this question earlier and it didn't go well. Maybe this is a better explanation of what I need.
I have a Loan object with over 30 attributes. I want to group by one of the attributes (the loan number, a String), sum another attribute (the loan amount, a Double), and just return the remaining attributes. So I tried:
Map<String, List<Loan>> groupedLoans = loanList.stream()
.collect(groupingBy(Loan::getLoanNum, summingDouble(Loan::getAmount));
However that does not give me a List of Loans. I get only the 2 attributes used in the collect statement, a Map<String, List<Double>>, which isn't what I want (I understand it though). I've lost all the other attributes of the Loan. I want a Map<String, List<Loan>>.
I've gathered from the research I've done that I probably need to 'new' up another Loan object, and pass the attributes in the constructor, but like I said, I have over 30 attributes, and that would be unwieldy.
How can I achieve this elegantly?
Take-2. Let's see if I can tackle it this time. So apparently you need a copy constructor like this first:
public Loan(Loan other, double value) {
this.someProp = other.someProp;
.....
this.value = value;
}
then you still need to collect to a Map with a Comparator that take into consideration the loanNum:
loans.stream().collect(Collectors.groupingBy(Function.identity(),
() -> new TreeMap<>(Comparator.comparing(Loan::getLoanNumber)),
Collectors.summingDouble(Loan::getValue)))
.entrySet()
.stream()
.map(e -> {
return new Loan(e.getKey(), e.getValue());
})
.collect(Collectors.toList());
e.getKey is actually the Loan; while e.getValue is the summing of all grouped values.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want a cloud function on real time database node, lets call 'waitingroom'
When 1st child inserted,then it wait for next child for 30 second for pairing.
If more than 2 child inserted at same time on this node then pair(2-2) them with random position
child and leave rest child to wait for another child to be inserted to pair with.
remove child from the node('waitingroom') if not paired with in 30seconds.
Every child contains key and value.Every Value contains name_imagename format data.When node contains minimum 2 child for pair then interchange value of both child(only value should be changed).
then insert final data to another node called 'matchingdone' with key and interchangedvalue.
In fire base you can run a query and then in the callback you can get the reference to the inserted child and check if there already exists a child with similar value.
let counter = 0
waitingroom.on("value", snap() => {
if ( counter == 2 ){
// compare the child added have value similar to existing one
Object.keys(snap.val()).map( k=> {
matchingdone.push(snap.val()[k])
})
}
counter++
})
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Making a simple api in golang, why does this tutorial use
var movies = map[string]*Movie{
"tt0076759": &Movie{Title: "Star Wars: A New Hope", Rating: "8.7", Year: "1977"},
"tt0082971": &Movie{Title: "Indiana Jones: Raiders of the Lost Ark", Rating: "8.6", Year: "1981"},
}
while this other tutorial uses something more like:
type Movies []Movie
var movies Movies
movies = append(movies, Movie{id: "tt0076759", Title: "Star Wars: A New Hope", Rating: "8.7", Year: "1977"})
It seems like the first one gives me a map containing key value pairs where the value is a pointer to a movie. And the second one gives me an array(slice?) of movies where the id serves as the key for lookup. Why are pointers used in the first one?
First of all you need to understand what pointer is used for. When you want to share the value use Pointer as the Tour of Go
A pointer holds the memory address of a value
As the first tutorial because they wanted to create variable that share value so it uses less memory.
As the second tutorial you don't need to add pointer variable to a slice.
Because Slice it self is a reference type.
Slices hold references to an underlying array, and if you assign one
slice to another, both refer to the same array.
some reference :
https://golang.org/doc/effective_go.html#slices
https://stackoverflow.com/a/2441112/2652524
I glanced through the article and I can't see any particular reason. Perhaps he's implemented something that wasn't covered in that post like a method receiver for *Movie.
Consider this example: https://play.golang.org/p/SZb47MKIr3
It'll fail to compile because the "Print" function has a Pointer Receiver, but the instances in the map are not pointers.
Whereas this example: https://play.golang.org/p/g0aXXcze5d Runs fine.
This is a good post explaining the difference: https://nathanleclaire.com/blog/2014/08/09/dont-get-bitten-by-pointer-vs-non-pointer-method-receivers-in-golang/
I am developing a web app on asp.net which is online exam system. now, in this i am fetching questions and answers from table on random bases. what i want to do is to ignore a number which is already generated once so that same question will not repeat. following is the code i have used. what to do here??
Random rnd = new Random();
int i = rnd.Next(startid, endid + 1);
getQuestion(i);
public void getQuestion(int no)
{
String str = "select * from asp_easy where no = '"+no+"'";
}
What I would do is create a List<int> object, containing the primary key value associated with each question in the asp_easy table (i.e. the question ID field, which looks to be no in your code).
Pick a random number from this list, remove it from the list of available numbers, then retrieve that question number from the database.
This ensures that you always get unique questions (as long as that object stays around) and should be constant time or get faster as the size of the list shrinks.
As others have mentioned you should probably tighten up your data access methods as building queries by concatenation of strings is a universally bad idea :)
Edit: Depending on how important proper randomness is to you, look into using an unbiased source of randomness. Otherwise, you may get the same questions being selected for a given set of candidate questions over larger sample sizes.
Have a list of string and random variable and push each element into the list with generating a random number. that may help you.
Or following may help.
select top 10 * from [asp_easy ] order by newid()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am passing a username in a session like below and would like to get the first letter of the first name and last name. So for example Doe, John I would like to get "JD" etc. Any idea how I would do this? Thank you
Session("Username")
As an example following your requirements. Lastname stored in first position and firstname stored in last position, but the order of the returned string is First/Last
string username = "Doe, John";
string[] parts = username.Split(' ');
string result = parts[1].Substring(0,1) + parts[0].Substring(0,1);
Console.WriteLine(result);
No error checking is present for clarity, but if you want this for a real work some checks on length of parts is mandatory
If you have only spaces between the parts of the username, try with this RegEx :
initials = Regex.Replace(Session("Username")," *([^ ])[^ ]*","$1")
--> the string "[^ ]" gets every character which is not a space (you can add the char '-' if you want)
By this way, you get a solution which works with people called "John Joe Jibbs" ^^
This is just a proof of concept.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a program that has a tree of directories that open into subtrees of files within those directories. There are two columns in my view:
- the tree itself
- and a text column next to it that says whether the file was selected
- The user can pick multiple files at once. After the user is done selecting, when they hit the "ok" button, the text should change to "selected" next to the appropriate files.
- The problem is, I can't figure out a way to tell it which indices to change the text of. I tried selectionModel()->selectedIndexes() (and selectedRows) but neither of these have a way to get the original index far as I can tell. How could I get the original from the overall tree?
As mumush mentions, Andrea's answer applies to the QTreeWidget only, not a QTreeView. QTreeView has no selectedItems method so you have to use selectedIndexes, which will return you a list of QModelIndex objects.
You can use these objects to access and update items in your tree model like so:
# Get the fields that are currently selected and loop over them
indexes = tree.selectedIndexes()
for index in indexes:
# We only care about the "Selected" column.
if index.column() != 1:
continue
# Change the tree value.
treeModel.setData(index, "[SELECTED]")