[英]How can i use the return value of a private static method (extension of object) in a different method in that same class? (java)
I have a java project with 3 classes.我有一个 java 项目,有 3 个班级。 1 class is used as a static private method in another class. I need to add the results of that method to be added to a list in another method within that class.
1 class 在另一个 class 中用作 static 私有方法。我需要将该方法的结果添加到该 class 中另一个方法的列表中。
Is there anyone who can send me in a good direction?有没有人可以送我一个好的方向?
I understand that you want the implementation of method validWords()
.我知道您想要方法
validWords()
的实现。 The following works for me.以下对我有用。
public static List<WordScore> validWords(final List<String> wordList, final int[] tileCounts) {
return wordList.stream()
.map(word -> wordScore(word, tileCounts))
.collect(Collectors.toList());
}
The above code uses the stream API that was added in Java 8. You can also refer to the lesson entitled Aggregate Operations in Oracle's Java tutorials.上述代码使用了Java 8中新增的stream API,也可以参考Oracle的Java教程中的聚合操作一课。
Method stream() that is inherited by interface java.util.List
returns a "stream" of all the elements in wordList
.接口
java.util.List
继承的方法stream()返回wordList
中所有元素的“流”。 Each element is a string.每个元素都是一个字符串。
Method map
takes a single parameter which is a functional interface .方法
map
采用一个函数接口参数。 A functional interface can be implemented by a lambda expression .函数式接口可以通过lambda 表达式来实现。 The method that needs to be implemented takes a single string parameter and returns a
WordScore
object.需要实现的方法采用单个字符串参数并返回
WordScore
object。
Method collect()
also takes a single parameter that is a functional interface.方法
collect()
还采用一个函数接口参数。 Utility class Collectors
contains an implementation that returns a List
that contains all the elements in the stream. In this case the stream contains WordScore
elements.实用程序 class
Collectors
包含一个实现,该实现返回一个包含 stream 中所有元素的List
。在这种情况下,stream 包含WordScore
元素。 Hence collect()
in the above code returns the list that you want the method validWords()
to return.因此,上面代码中的
collect()
返回您希望方法validWords()
返回的列表。
If this is not what you are looking for, let me know and I will delete it.如果这不是您要查找的内容,请告诉我,我会删除它。
Member functions of the same class can call and pass data to each other.同一个class的成员函数可以互相调用和传递数据。 You just need to call
wordScore()
from validWords()
for each string in the list.您只需为列表中的每个字符串从
validWords()
调用wordScore()
。
public static List<WordScore> validWords (final List<String> wordList, final int[] tileCounts) {
List<WordScore> results = new ArrayList<WordScore>();
for(String word : wordList) {
results.add(wordScore(word, tileCounts));
}
return results;
}
This is the simple way.这是简单的方法。 @Abra's code also works, it uses the Stream API introduced in Java 8. Java 8 has also introduced lambda expressions which will help simplify your
Comparator
: @Abra 的代码也有效,它使用 Java 8 中介绍的 Stream API。Java 8 还介绍了 lambda 表达式,这将有助于简化您的
Comparator
:
Collections.sort(list, new Comparator<WordScore>()
{
@Override
public int compare(final WordScore wsA, final WordScore wsB)
{
return (wsA.score() == wsB.score())
? 0
: (wsA.score() > wsB.score()) ? -1 : 1;
}
});
This can be rewritten as:这可以重写为:
Collections.sort(list, (wsA) -> (wsA.score() == wsB.score())
? 0
: (wsA.score() > wsB.score()) ? -1 : 1);
This works because Comparator
is a functional interface, an interface that contains only one method compare()
.这是有效的,因为
Comparator
是一个功能接口,一个只包含一个方法compare()
的接口。 Instead of creating an anonymous class and overriding compare()
, what you do is simply specify the arguments in parantheses (type isn't required, the compiler is smart enough to infer them) followed by the ->
operator.无需创建匿名 class 并覆盖
compare()
,您只需在括号中指定 arguments(不需要类型,编译器足够聪明,可以推断出它们),然后是->
运算符。 After this, you specify the return value, the return
keyword can also be omitted.在此之后,您指定返回值,
return
关键字也可以省略。
You also don't have to do these multiple condition checks, simply return the difference of the scores:您也不必进行这些多重条件检查,只需返回分数的差异:
Collections.sort(list, (wsA, wsB) -> wsA.score() - wsB.score() );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.