标签: java-8

如何存储invokedynamic的结果?

Java 8引入了对第一类函数的支持,它允许将函数赋值给变量.在这种情况下,变量必须是函数类型,它由函数接口(只有一个抽象方法的接口)定义.

因此,考虑一个接口IA具有以下定义的类的示例:

interface I{ int foo(); }
class A implements I{ 
  public int foo(){return 7;} 
  public static int bar(){return 11;}
}
Run Code Online (Sandbox Code Playgroud)

我们可以分配给类型的变量I的实例A方法参考该方法barA.两者都可以存储在类型的变量中I,例如:

I i1 = new A(); 
I i2 = A::bar;
Run Code Online (Sandbox Code Playgroud)

如果我们分析前面代码编译产生的字节码,我们将得到:

0: new           #2                  // class A
3: dup
4: invokespecial #3                  // Method A."<init>":()V
7: astore_1
8: invokedynamic #4,  0              // InvokeDynamic #0:foo:()LI;
13: astore_2
Run Code Online (Sandbox Code Playgroud)

因为i1 …

jvm invokedynamic java-8

2
推荐指数
1
解决办法
122
查看次数

随机/随机比较器

有没有办法模拟Collections.shuffle的行为而没有比较器易受排序算法实现的影响,结果是安全的?

我的意思是不打破可比合同等.

java sorting collections java-8

2
推荐指数
2
解决办法
3525
查看次数

Java中列表上的AND/OR操作

让我们考虑两个arraylists.

ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(3);

ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(3);
list2.add(4);
list2.add(5);
Run Code Online (Sandbox Code Playgroud)

我想在这些列表之间执行AND和OR操作.例如,这里当我执行AND操作时,b/w list1&list2输出应该是一个只包含3的列表,当我执行OR操作时b/w list1&list2输出应该是一个包含1,2,3,4,5的列表(不要重复两次).

Java中是否有可能实现这种情况?在这种情况下我们可以使用Java 8 Streams吗?请给我最有效的方法来得到答案.

java list java-8

2
推荐指数
1
解决办法
138
查看次数

JDialog没有出现

该程序大多数工作正常,但不打开任何窗口.它应该在桌面右下方显示一个小对话框.但是对于另一个人来说,编译相同的代码没有问题.我们有相同的Java运行时(1.8_u40).我怎样才能解决这个问题?

我把代码放在下面:

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ProgressDialog {
    private JDialog dialogFrame;
    private JProgressBar progressBar;
    private JLabel headingLabel;
    private Uploader callerUploader;

    public ProgressDialog() {

        dialogFrame = new JDialog();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException ex) {
            System.err.println(ex.toString());
        }

        dialogFrame.setSize(200, 50);
        dialogFrame.setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints(); …
Run Code Online (Sandbox Code Playgroud)

java swing awt jdialog java-8

2
推荐指数
1
解决办法
467
查看次数

如何将普通Java for循环转换为Java 8流?

如何将此代码转换为Java 8流?

String getFirst(String key) {
    for (Param p : params) {
        if (key.equals(p.getKey())) {
            if (!p.getValues().isEmpty()) {
                return p.getValues().get(0);
            }
        }
    }
    return "";
}
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

2
推荐指数
1
解决办法
139
查看次数

部署到tomcat 8.0.21和java 8时,Spring上下文初始化失败,出现java.lang.IllegalArgumentException

我的Web应用程序在JDK 1.7上正常运行但在1.8上崩溃,但有以下异常(在应用程序服务器启动时 - tomcat 8期间).我使用的是Spring版本:3.2.2.RELEASE.

我编译为目标1.7,我只将运行时改为java 8.

10-Apr-2015 10:50:44.250 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
10-Apr-2015 10:50:44.266 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
10-Apr-2015 10:50:51.832 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class com.nrift.finch.inf.startup.web.OperationContextListener
 java.lang.IllegalStateException: application init failed
    at com.nrift.finch.inf.startup.web.OperationContextListener.contextInitialized(OperationContextListener.java:85)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1738)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException
    at …
Run Code Online (Sandbox Code Playgroud)

spring-3 java-8 tomcat8

2
推荐指数
1
解决办法
5434
查看次数

使用Integer foo列出<String>到List <Integer>(String s)

我有一个list List<String>(list1)和一个函数Integer foo(String s).使用Java 8的强大功能我希望通过应用于每个项目来转换list1为a .以下代码有效,但有一点问题:List<Integer>foolist1

List<Integer> list2 = list1.stream().mapToInt(s -> foo(s)).boxed().collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

foo回报null的一些元素list1一个NullPointerException被抛出.另外,我的解决方案看起来有点不方便.那么有更好的吗?

java list java-8 java-stream

2
推荐指数
1
解决办法
106
查看次数

在java中,使用String :: length进行Comparator.comparing()

我有一个像这样的代码:

String[] yetToSortedArray = {"Abc","hello world","nihao?","chilemaNin"};
Arrays.parallelSort(yetToSortedArray, Comparator.comparing(String::length));
for(String str : yetToSortedArray){
    System.out.println(str + ", ");
}
Run Code Online (Sandbox Code Playgroud)

我的问题在这里:"String :: length"我真正传递给Comparator.comparing()的是什么?为什么String :: length()没有括号?

我想我正在使用它:https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparing-java.util.function.Function-java.util.Comparator-

并且它说"接受一个从类型T中提取Comparable排序键的函数,并返回一个比较键的比较器."

dos String :: length如何匹配这种类型?

谢谢.

java comparator java-8

2
推荐指数
1
解决办法
3837
查看次数

特定年份的某个月中的天数不起作用

注意:Donot标记它重复,我知道它是重复的问题,但我没有得到它的帮助.

我想计算特定年份的一个月中的天数.我看了这个,但它不能正常工作.我试过跟随

public class NumOfDays {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("Enter month: ");
        int month = input.nextInt();
        System.out.print("Enter year: ");
        int year = input.nextInt();

        Calendar mycal = new GregorianCalendar(year, month, 1);

        System.out.println("Number of days are: " + mycal.getActualMaximum(Calendar.DAY_OF_MONTH));

    }
}
Run Code Online (Sandbox Code Playgroud)

我的控制台是

Enter month: 2
Enter year: 2000
Number of days are: 31 /// Wrong
Run Code Online (Sandbox Code Playgroud)

Enter month: 10
Enter year: 1999
Number of days …
Run Code Online (Sandbox Code Playgroud)

java date java-8

2
推荐指数
2
解决办法
2916
查看次数

如何根据其值的参数对Map进行排序?

Stream<Map.Entry<String, List<Object>>> sorted = index.entrySet().stream()
                .sorted(Map.Entry.comparingByValue());
Run Code Online (Sandbox Code Playgroud)

sorted(Comparator<? super Map.Entry<String,List<NFArticle>>>)类型中的方法Stream<Map.Entry<String,List<NFArticle>>>不适用于参数(Comparator <Map.Entry<Object,Comparable<? super Comparable<? super V>>>>)

我想Hashmap根据size()列表中的值来排序HashMap.如何使用Java 8中的Stream库实现此目的?

java java-8

2
推荐指数
1
解决办法
1709
查看次数