我正在尝试根据条件过滤列表。是否有其他替代方法可以中断Java 8流以停止过滤?
举个例子:假设我有以下列表。
List<String> list = Arrays.asList("Foo","Food" ,"Fine","Far","Bar","Ford","Flower","Fire");
list.stream()
.filter(str -> str.startsWith("F")) //break when str doesn't start with F
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我希望从头开始以“ F”开头的所有字符串,一旦找到不以“ F”开头的字符串,我想停止过滤。没有流,我将执行以下操作:
List<String> result = new ArrayList<>();
for(String s : list){
if(s.startsWith("F")){
result.add(s);
}
else{
break;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在信息流中使用“中断”?
class Thing<T> {
public Map<String, List<String>> getData() { ... }
}
class Thingamajig {
void doStuff() {
Thing myThing = ...;
List<String> data = myThing.getData().get("some_key");
}
}
Run Code Online (Sandbox Code Playgroud)
的调用myThing.getData()产生以下错误:
不兼容的类型:java.lang.Object无法转换为java.util.List
通过为Thing实例提供通用参数可以解决此问题:
void doStuff() {
Thing<?> myThing = ...;
List<String> data = myThing.getData().get("some_key");
}
Run Code Online (Sandbox Code Playgroud)
即使是通用通配符也可以解决此问题。对我来说,这是没有意义的,因为getData()甚至没有引用泛型类型参数。
是否getSortedUsers()总是返回的orderd流getUsers() 独立的的getUsers()-collection类型?
public Set<User> getUsers(){
// unordered collection type
HashSet<User> set = new HashSet<>();
set.add(..);
set.add(..);
set.add(..);
return set;
}
// Is the stream only sorted if getUsers() is hold within a sortable collection type?
public Stream<User> getSortedUsers(Comparator<User> comp){
return getUsers().stream().sorted(comp);
}
Run Code Online (Sandbox Code Playgroud) 在迭代之前检查流是否为空或不为null的推荐/好的方法
我能想到的是协助stream()流Streamable并检查为空和空
是否有任何Java 8函数/功能来检查?
参考:-https: //www.baeldung.com/java-null-safe-streams-from-collections
我将a HashMap<String, Double>转换为HashMap<String, Double>内容按值排序的位置。当我打印出以下内容时:
Stream<Map.Entry<String, Double>> sorted = map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
数据以正确的顺序打印出来,按值排序。但是,我不需要打印数据,我想将此流的内容折叠到具有新排序顺序的新HashMap中。我尝试了一些选择,但是当我这样做时,我似乎会找回原始的,未排序的HashMap:
return map.entrySet()
.stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));
Run Code Online (Sandbox Code Playgroud)
如何修改流,以便将已排序的HashMap发送回去?
我对Swing Event Dispatcher Thread(EDT)的理解是,它是执行事件处理代码的专用线程。因此,如果我的理解是正确的,那么在下面的示例中:
private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
// START EDT
String command = e.getActionCommand();
if( command.equals( "OK" )) {
statusLabel.setText("Ok Button clicked.");
} else if( command.equals( "Submit" ) ) {
statusLabel.setText("Submit Button clicked.");
} else {
statusLabel.setText("Cancel Button clicked.");
}
// END EDT
}
}
Run Code Online (Sandbox Code Playgroud)
在之间的所有代码START EDT,并END EDT在在美国东部时间执行,并且它的任何代码以外的主应用程序线程上执行。同样,另一个示例:
// OUTSIDE EDT
JFrame mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent …Run Code Online (Sandbox Code Playgroud) 编译器如何确保以下语句的等效lambda
BinaryOperator<String> concatOperator = String::concat;
Run Code Online (Sandbox Code Playgroud)
是
BinaryOperator<String> concatOperator = (resultString, inputString) -> resultString.concat(inputString);
Run Code Online (Sandbox Code Playgroud)
并不是
BinaryOperator<String> concatOperator = (resultString, inputString) -> inputString.concat(resultString);
Run Code Online (Sandbox Code Playgroud) 我正在查看HashMap的源代码,但是二进制运算符使很多人感到困惑。
我确实了解以下的一般目的,公平分配并将hashCode限制在存储桶限制之内。
有人可以在这里解释评论吗?立即进行操作有什么好处?
/**
* Computes key.hashCode() and spreads (XORs) higher bits of hash
* to lower. Because the table uses power-of-two masking, sets of
* hashes that vary only in bits above the current mask will
* always collide. (Among known examples are sets of Float keys
* holding consecutive whole numbers in small tables.) So we
* apply a transform that spreads the impact of higher bits
* downward. There is a tradeoff between speed, utility, and
* …Run Code Online (Sandbox Code Playgroud) 我正在打字,我想知道如何在静态上下文中调用默认方法,我有一个这样的代码段。
interface InterfaceWithDefaultMethod{
public default void saySomething(final int a,final int b){System.out.println(String.format("Parameters are %d %d",a,b));}
}
public class StaticsMethodIsNotOverriden2 implements InterfaceWithDefaultMethod {
public static void main(String[] args) {
final StaticsMethodIsNotOverriden2 clazz = new StaticsMethodIsNotOverriden2();
clazz.saySomething(13,20);//Overriden method call with 13 20
clazz.callDefaultSaySomethingFromInstance();//Parameters are 1 2
/*HOW CALL public default void saySomething(final int a,final int b) METHOD HERE....????*/
clazz.InterfaceWithDefaultMethod.super.saySomething(1, 2);/*no enclosing instance of type InterfaceWithDefaultMethod is in scope*/
}
@Override
public void saySomething(int a, int b) {
System.out.println(String.format("Overriden method call with %d %d",a,b)); …Run Code Online (Sandbox Code Playgroud) I have a function like this:
public static Xyz getXyz(P p) {
if (p == null) {
return null;
}
List<Object> bs = p.getB();
if (CollectionUtils.isEmpty(Bs)) {
return null;
}
for (Object b : bs) {
if (b instanceof R) {
R r = (R) b;
List<Object> cObjects = r.getB();
for (Object cObject : cObjects) {
if (cObject instanceof C) {
C c = (C) cObject;
Object vObject = cObject.getV();
if (vObject instanceof V) {
return r.getXyz();
}
}
} …Run Code Online (Sandbox Code Playgroud)