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 2 years ago.
Improve this question
How to get average using functional programming in java?
This is what I tried ...
It seems like its not working at IntStream.of
I would like to get average from a specific row of the array
public static void average(List<List<String>> rows){
IntStream stream = IntStream.of(e -> Integer.parseInt(e.get(2)));
OptionalDouble obj = stream.average();
if (obj.isPresent()) {
System.out.println(obj.getAsDouble());
}
else {
System.out.println("-1");
}
}
rows is the array are rows read from an excel file.
Stream.of(elem1, elem2) creates a stream with the stated elements.
Imagine you have a box with 100 fotos in it.
If you do Stream.of(box), you get a stream of boxes, returning 1 box.
What you wanted was a stream of fotos. To get that, you want box.stream(), not Stream.of(box).
Your next problem then is that you don't seem to understand what reduce does. You need to tell the system how to integrate two results, not just how to get a result.
What you want here isn't reducing in the first place, you want to map a given 'foto' (a List of string in your case) to an integer, which requires not just e.get(), but also an Integer.parseInt, and you want map, not reduce.
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 am working on a project that has a list of student name and numbers, for example
James Bloggs,1
Paul Jonson,43
Andt Peters,23
Once I have them in an array I then need them sorted.
What is the best way of going about this. Its not the sort Im stuck on its the referencing the names to the numbers. I would have thought if I do a 2 denominational array only one would be sorted.
Any help would be great,
EDIT: I just realized this question was asking about a 2-dimensional array and my answer doesn't directly deal with that. I am skeptical that arrays should be involved at all. Arrays are usually for dealing with primitive data, and maybe if you are coming from a C background you'd think they'd be the natural thing to use. If you really honestly have to use arrays then this probably isn't the way to go.
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
public void foo(){
// Use a TreeMap. It will sort keys on insertion.
Map<Integer,String> nameByNumber = new TreeMap<>();
nameByNumber.put(1, "James Boggs");
// etc. put all the entries in however you need to
List<Integer> sortedNumbers = personByNumber.getKeys();
List<String> namesSortedByNumber = personByNumber.getNames();
}
If you need it to be more organized and complex, you can encapsulate the name and number into a Class with a name and number property. Then you'd still use the number as the key, but you'd have the full class as the value. Do this if you need to have more than just a name, like last name, first name, address, etc.
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 6 years ago.
Improve this question
Consider a set with like 100 elements.
Set<String> originalSet; //[1....100] size is 100
From originalSet, (m) subset of elements of some size(n) with some starting index(i) have to retrieved.
Example:
m = 4, n = 45, i = 1
Following have to be retrieved
subset1[1-45], subset2[46-90], subset3[91-35], subset4[36-80]
Whats the best way of doing this.
First of all, Set is unordered so it does not make sense to talk about indices etc. List would make more sense here.
Next, you have to be explicit about what you mean by "best". Performance on insertion? Random access? Creation of your n-from-i subset? These are important question to choose the implementation.
I think two primary options would be linked list with special handling of the last element in subList operation or an array-based list.
Assuming your set has some notion of order, you could write this with
Iterable<Iterable<String>> slices =
Iterables.limit(
Iterables.partition(
Iterables.skip(
Iterables.cycle(originalSet),
i),
n),
m);
If you wanted a set out of this, you'd have to do a transform or something; if you have Java 8 that'd just be something like Iterables.transform(..., ImmutableSet::copyOf).
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 6 years ago.
Improve this question
I'm preparing for an exam. I can read the CSV file but I don't know how to sort a data line.
For example, if "SchoolName.csv" has
Cayuga,Elkhart,Slocum,Westwood
Neches,Palestine,central
Dibolon
Lufkin,Holiday
After I sort the data the output should be
Dibolon // becuase it only contain one name.
Lufkin,Holiday
Neches,Palestine,central
Cayuga,Elkhart,Slocum,Westwood
So you want to sort based on number of elements. Assuming you have those elements in a Collection<String> csvlines:
csvlines.stream()
.sorted(Comparator.comparing(line -> StringUtils.countMatches(line, ",")))
.collect(Collectors.toList())
If you want to sort each row based on the number of names, you could do it like this
// Read the file's lines
List<String> lines = Files.readAllLines(pathToFile);
List<String> sortedLines = lines.stream().sorted(Comparator.comparing(line -> line.split(",").length)).collect(Collectors.toList());
Define a comparator, That is a class extending comparator and pass it as an argument to arraylistname.sort(comparator);
A comparator returns a negative integer, zero, or a positive integer if the first argument is less than, equal to, or greater than the second.
so
class CompareByLength implements Comparator {
compare(list a, list b) {
return a.size()-b.size();
}
}
alternatively use b.size()-a.size() if you want your list ordered the other way round.
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 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm
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 8 years ago.
Improve this question
In groovy is there a specific way to remove a value from a collection. For example I have a list of form fields but two of them are hidden fields and I'm trying to figure out how to remove them from the collection. The two parameters I'm trying to remove are salesKey and topicSelection. Groovy newbie so code samples are most helpful
request.requestParameterMap.collect { key, value -> "$key: ${value[0].string}" }.join("\n")
key.remove("salesKey")
key.remove("topicSelection")
I think you could use findAll:
request.requestParameterMap.findAll { key, value ->
!( key in ["salesKey", "topicSelection"] )
}
Check out this answer.
Also, depending on your specific aims, there are a couple of other ways to remove a pair, including dropWhile (which is more or less iterating over your data struct) and minus (which isn't so much removing a pair as creating a new structure without the specified pair). Official doc here.