小编ytt*_*rrr的帖子

从流中获取最后n个元素

我想知道是否有替代方案

List<X> lastN = all.subList(Math.max(0, all.size() - n), all.size());
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

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

引导表.如何从表中删除所有边框?

如何从引导表中删除所有(特别是外部的)边框?这是一张没有内边框的桌子:

HTML

<style>
    .table th, .table td { 
        border-top: none !important;
        border-left: none !important;
    }
</style>
<div class="row">
    <div class="col-xs-1"></div>
    <div class="col-xs-10">
        <br/>
        <table data-toggle="table" data-striped="true">
            <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>A</td>
                <td>B</td>
            </tr>
            <tr>
                <td>C</td>
                <td>D</td>
            </tr>
            <tr>
                <td>E</td>
                <td>F</td>
            </tr>
            </tbody>
        </table>
    </div>    
    <div class="col-xs-1"></div>
</row>   
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/sba7wkvb/1/

需要覆盖哪些CSS样式才能删除所有边框?

css twitter-bootstrap twitter-bootstrap-3 bootstrap-table

14
推荐指数
2
解决办法
6万
查看次数

使用分组,计数和过滤操作收集流

我正在尝试收集丢弃很少使用的项目的流,如下例所示:

import java.util.*;
import java.util.function.Function;
import static java.util.stream.Collectors.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import org.junit.Test;

@Test
public void shouldFilterCommonlyUsedWords() {
    // given
    List<String> allWords = Arrays.asList(
       "call", "feel", "call", "very", "call", "very", "feel", "very", "any");

    // when
    Set<String> commonlyUsed = allWords.stream()
            .collect(groupingBy(Function.identity(), counting()))
            .entrySet().stream().filter(e -> e.getValue() > 2)
            .map(Map.Entry::getKey).collect(toSet());

    // then
    assertThat(commonlyUsed, containsInAnyOrder("call", "very"));
}
Run Code Online (Sandbox Code Playgroud)

我觉得有可能做得更简单 - 我是对的吗?

java java-8 java-stream

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

在Spring Beans中初始化字段的正确方法是什么?

我想知道如何初始化Spring Beans中的字段?这有几种可能的解决方案:

1.直接在声明上初始化字段

import org.springframework.stereotype.Component;

@Component
public class DeclarationInit {

    private final int field = Integer.MAX_VALUE;

    public int getField() {
        return field;
    }
}
Run Code Online (Sandbox Code Playgroud)

2.使用@Value注释初始化字段

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ValueInit {

    @Value("#{T(Integer).MAX_VALUE}")
    private int field;

    public int getField() {
        return field;
    }
}
Run Code Online (Sandbox Code Playgroud)

3.使用@Autowired注释初始化字段

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AutowiredInit {

    private int field;

    @Autowired
    private void initField() {
        field = Integer.MAX_VALUE;
    }

    public int getField() {
        return field;
    }
} …
Run Code Online (Sandbox Code Playgroud)

java spring dependency-injection autowired in-class-initialization

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

在Java流中拆分对象

我想知道是否可以在流内拆分对象.例如Employee:

public class Employee {

    String name;
    int age;
    double salary;

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() { return name; }

    public int getAge() { return age; }

    public double getSalary() { return salary; }
}
Run Code Online (Sandbox Code Playgroud)

我想在流中执行一些操作.为简单起见,让它像这样(假设我的代码架构不允许将它放在Employee类中 - 否则它会太容易了):

public void someOperationWithEmployee(String name, int age, double salary) {
    System.out.format("%s %d %.0f\n", name, age, salary);
}
Run Code Online (Sandbox Code Playgroud)

现在它看起来像这样:

Stream.of(new Employee("Adam", 38, 3000), new Employee("John", 19, …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream method-reference

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

使用varargs的Java方法,它执行操作并打印已知结果

在Java中是否可以创建类似于此execute方法的方法:

public void execute(Runnable... mrs) {
    for (Runnable mr : mrs) {
        mr.run();
        // should print some expected string here
    }
}

public void sleep(int seconds) {
    try {
        Thread.sleep(1000 * seconds);
    } catch (InterruptedException ex) {
        log.error("Error occured", ex);
    }
}

public void customExecute() {
    execute(() -> sleep(1), () -> sleep(2));
}
Run Code Online (Sandbox Code Playgroud)

但在执行每个操作后,它应该打印传递的String参数和(问题的关键时刻)它应该支持一行使用 - 类似于:

execute(("Action 1 passed") -> someAction(), ("Other action passed") -> otherAction());
Run Code Online (Sandbox Code Playgroud)

java variadic-functions java-8

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

方法引用和链式谓词

我正在尝试用方法引用来压缩我的代码.这是我要改进的一条线:

assertThat("only true & false strings allowed",
        records.stream().map(Record::getType)
        .allMatch(s -> "true".equals(s) || "false".equals(s)));
Run Code Online (Sandbox Code Playgroud)

使用方法参考,它可以更好:

assertThat("only true & false strings allowed",
       records.stream().map(Record::getType).allMatch("true"::equals));
Run Code Online (Sandbox Code Playgroud)

但是,无论如何我可以在谓词中添加"false"吗?

java predicate java-8 java-stream method-reference

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

确保按元素中的字段排序的元素列表

使用Guava可以通过这种方式确保升序排序:

import com.google.common.collect.Ordering;
import io.predictor.dao.ohlcv.OhlcvHm;
import static java.util.stream.Collectors.toList;

assertThat("Ordered by age", Ordering.natural().isOrdered(
    employees.stream().map(Employee::getAge).collect(toList())));
Run Code Online (Sandbox Code Playgroud)

对我来说,Guava(因为它与Java lambdas密切相关)并不能为这种情况提供简单的解决方案.当然,我可以写一些帮助方法并包装它,但也许有人已经在库中完成了它.有一种最简单的方法吗?就像是:

XLibrary.isOrdered(employees, Employee::getAge);
Run Code Online (Sandbox Code Playgroud)

collections guava java-8 method-reference

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