小编Kun*_*Lun的帖子

使用排序随机播放流 - IllegalArgumentException

我有这个命令:

list.stream()
    .filter(e -> ...)
    .sorted(comparatorShuffle())
    .findAny()
    .orElse(null);
Run Code Online (Sandbox Code Playgroud)

这是comparatorShuffle()

public static <E> Comparator<E> comparatorShuffle(){

    return new Comparator<>(){
        private final List<Integer> temp = List.of(-1, 1);
        private final Random random = new Random();
        @Override
        @SuppressWarnings("ComparatorMethodParameterNotUsed")
        public int compare(E o1, E o2){
            return temp.get(random.nextInt(temp.size()));
        }
    };

}
Run Code Online (Sandbox Code Playgroud)

有时我会遇到异常:IllegalArgumentException: Comparison method violates its general contract!

我明白为什么我得到这个,这是因为排序(它是随机的)不遵守规则:if A > B && B > C then A > C

有办法抑制/忽略这个错误吗?或者不使用的另一种方式来随机播放流collect

java java-stream

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

将字符串转换为ZonedDateTime并更改TimeZone

我有这串 "Tue Apr 09 2019 12:59:51 GMT+0300"

我想转换成ZonedDateTime

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss OOOO");
ZonedDateTime zdt = ZonedDateTime.parse(a, dtf);
Run Code Online (Sandbox Code Playgroud)

转换为后ZonedDateTime,我想将时区从更改GMT+0300为其他时区。

我的第一个问题是parse。我得到:

DateTimeParseException: Text 'Tue Apr 09 2019 12:59:51 GMT+0300' could not be parsed at index 25(在GMT + 0300,我认为OOOO这是不对的,但我不知道这是什么)

之后,我不知道如何更改时区。

java datetime zoneddatetime

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

Map &lt;String,Map &lt;String,String &gt;&gt;-使用Stream选择值键

我有这张地图:

Map<String, Map<String, String>> listMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

我想KeysMapmain中选择所有与之不同的值MaplistMap.value.key

List<String> distinct = listMap.entrySet().stream()
                                .map(e -> e.getValue()) //Map<String, String>
                                //Select key of value
                                .distinct().collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

我不知道如何选择keyvaluelistMap

java lambda java-stream

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

Intellij warning: Boolean method foo() is always inverted

I have this code:

private boolean changeNcheckIP() {

    //try 3 times before false
    for(int i = 0; i < 3; i++) {

        if(changeIP() && checkIP()) { return true; }

    }

    return false;

}
Run Code Online (Sandbox Code Playgroud)

Intellij give me an warning at changeNcheckIP():

Boolean method changeNcheckIP() is always inverted

How can I fix that?

java intellij-idea

0
推荐指数
1
解决办法
518
查看次数

Java Spring Session not persist between requests

I have this controller

@Controller
@RequestMapping(value = "/login")
public class Login{

    @RequestMapping
    public ModelAndView mainPage(HttpServletRequest request){

        request.getSession().setAttribute("testSession", "Session test");

        return new ModelAndView("/login");

    }

    @RequestMapping(value = "/check")
    public View check(HttpServletRequest request){

        System.out.println(request.getSession(false)); //null

        return new RedirectView("/login");

    }

}
Run Code Online (Sandbox Code Playgroud)

访问时,/login我创建一个会话并向其中添加属性"testSession"request.getSession().setAttribute("testSession", "Session test");

在页面上/login有这个<form action="/login/check" method="post">

在上,/login/check我尝试在创建会话/login,但它为null。

为什么会话在请求之间不持久?

PS:我的应用程序以Tomcat和Apache作为反向代理在远程服务器上运行,我通过以下方式访问我的应用程序 https://mydom.com

更新

我创建了一个控制器来测试会话:

@Controller
@RequestMapping(value = "/sess")
public class TestSession{

    @RequestMapping(method = RequestMethod.GET)
    public void mainPage(HttpServletRequest request, HttpServletResponse response) throws IOException{ …
Run Code Online (Sandbox Code Playgroud)

java session spring

0
推荐指数
1
解决办法
312
查看次数

JQuery .change() 只工作一次

我有这段 javascript:

jQuery(document).ready(function(){

    const select = $('...'); //it's a simple select
    const input = $('...'); //it's a simple input

    select.change(doSomething());
    input.change(doSomething());

    function doSomething(){

        console.log("yes");

    }

}
Run Code Online (Sandbox Code Playgroud)

yes当页面加载时,在控制台打印两次。

当我更改 select 或 input 的值时yes,控制台不再打印。

jquery version: 3.4.0

javascript jquery

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