Refactoring a for loop into a stream [closed] - java

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 change this for loop into stream?
public int calculateForSpliterator(String[] matchTab, String spliterator, RulesChecker rulesChecker) {
int finalScore = 0;
for (String element : matchTab) {
String[] splitScroes = element.split(spliterator);
int ourPoints = Integer.parseInt(splitScroes[0]);
int theirPoints = Integer.parseInt(splitScroes[1]);
finalScore += rulesChecker.checkRules(ourPoints, theirPoints);
}
return finalScore;
}

Assuming RulesChecker#checkRules returns an int, then you can use:
public int calculateForSpliterator(String[] matchTab, String spliterator, RulesChecker rulesChecker) {
return Arrays.stream(matchTab)
.map(element -> element.split(spliterator))
.mapToInt(splitScores -> rulesChecker.checkRules(Integer.parseInt(splitScores[0]),
Integer.parseInt(splitScores[1])))
.sum();
}
Notice how this isn't much more readable than your current solution, so I probably would keep the for-loop if I were you.

Related

How to remove leading zero's in between characters of a string [closed]

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
I have dates like this in string that need leading zero's removed.
Here is what I need:
Remove leading zero's in between [- and :
Below are some of the examples and expected results
20200831000000.000[-05:EST] -> 20200831000000.000[-5:EST]
20200831000000.000[-08:30:EST] -> 20200831000000.000[-8:30:EST]
20200831000000.000[-10:EST] -> 20200831000000.000[-10:EST]
I can't figure this out as I'm new to regular expressions. I have tried the below but not working
"0+(?!$)"
this will work like you went :
public static void main(String[] args) {
String arr[] = {"20200831000000.000[-05:EST]","20200831000000.000[-08:30:EST]","20200831000000.000[-10:EST]"};
for(int i=0; i < arr.length ; i++){
arr[i] = arr[i].replaceAll("(?<=\\[-?)(0*)(?=.*\\])", "");
}
for(String s : arr){
System.out.println(s);
}
}
output :
20200831000000.000[-5:EST]
20200831000000.000[-8:30:EST]
20200831000000.000[-10:EST]

How tho check in Java if there is same characters in a String? [closed]

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
I've got a 6 char long string, and I would like to check, if there is just one from each character. How to?
You could compare the number of distinct characters to the length of the string:
boolean charMoreThanOnce = s.chars().distinct().count() < s.length();
You can do it using a Set. You need unique elements and Set gurantees you containing the unique elements. HashSet is implementation of Set, you can use it to implement this idea.
public boolean ifAllCharsUnique(String input){
char[] arr = input.toCharArray();
int length = arr.length;
Set<Character> checker = new HashSet<>();
for(int i =0 ; i < length; i++){
if(checker.contains(arr[i]){
return false;
}
checker.add(arr[i]);
}
return true;
}

All combinations of ArrayList<ArrayList<String>> Lists in JAVA [closed]

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 need help with a implementation of a function that gives me all combinations of ArrayList<ArrayList<String>> (not List<List<String>>) List in fix order.
An Example for what I need:
From:
List1:[1,2]
List2:[3,4]
List3:[5,6]
To:
String 1 "1,3,5"
String 2 "1,3,6"
String 3 "1,4,5"
String 4 "1,4,6"
String 5 "2,3,5"
String 6 "2,3,6"
String 7 "2,4,5"
String 8 "2,4,6"
All Strings in ArrayList<String> Object
Please help me, I have no idea how to implement it.
PS: The size of ArrayList<ArrayList<String>> is variable.
PSS: ...and the size from the ArrayList lists are variable.
After little changes from the old rev it works now (ArrayIndexOutOfBoundsException and deleting to much on result):
void generate(ArrayList<ArrayList<String>> lists,
ArrayList<String> result, int index) {
if (index >= lists.size()) {
System.out.println(String.valueOf(result));
return;
}
ArrayList<String> list = lists.get(index);
for (int i = 0; i < list.size(); i++) {
result.add(list.get(i));
generate(lists, result, index + 1);
result.remove(result.size() - 1);
}
}
}

How to decrease my repeated code [closed]

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 8 years ago.
Improve this question
Hi How I write these several code in only one line
if (stimee.getText().toString().equals("0")) {stimevar="00"; }
if (stimee.getText().toString().equals("1")) {stimevar="01"; }
if (stimee.getText().toString().equals("2")) {stimevar="02"; }
if (stimee.getText().toString().equals("3")) {stimevar="03"; }
if (stimee.getText().toString().equals("4")) {stimevar="04"; }
if (stimee.getText().toString().equals("5")) {stimevar="05"; }
if (stimee.getText().toString().equals("6")) {stimevar="06"; }
if (stimee.getText().toString().equals("7")) {stimevar="07"; }
if (stimee.getText().toString().equals("8")) {stimevar="08"; }
if (stimee.getText().toString().equals("9")) {stimevar="09"; }
all you really need is:
stimevar="0"+stimee.getText().toString();
since i dont see an else clause in your question, this must solve your problem.
You can use a Map to store the values and then set the variable.
Map<String, String> map = new HashMap<>();
map.put("0", "00");
map.put("0", "01");
...
map.put("9", "09");
stimevar = map.get(stimee.getText().toString());
String str = stimee.getText().toString();
for (int i = 0; i <= 9; i++)
if (("" + i).equals(str))
stimevar = "0" + i;
// parse input value as integer
int value = Integer.parseInt(stimee.getText().toString());
// check input for values from 0 to 9
for (int i = 0; i < 10; i ++) {
// if found a match
if (value == i) {
// set variable
stimevar = String.format("%02d", i);
// and stop checking
break;
}
}
EDIT: Thanks to #Tom for pointing out parsing the String only once beforehand would be more effective.

Comparing multiple strings in Java [closed]

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
I have 5 distinct strings called, say, string1 through string5.
I want to write a simple if statement that runs if any two of the five strings contain the same string. How would I do that?
Thanks in advance!
Comparison is a binary operation, therefore you can always compare only two objects at a time. I would suggest using a cycle and comparing each string to the remaining ones.
public boolean multipleStringEquals(String[] strings) {
for (int i = 0; i < strings.length; i++) {
for (int j = i + 1; j < strings.length; j++) {
if (strings[i].equals(strings[j])) {
return true;
}
}
}
return false;
}

Categories