小编Dig*_*tal的帖子

比较器基于对象的不同可空字段

我有一个Employee包含两个字段的对象namejobTitle.对于员工对象的排序,首先应该打开jobTitle,如果jobTitle为null,则排序应该基于名称.

下面是Employee对象

public class Employee {
    private String name;
    private String jobTitle;
}
Run Code Online (Sandbox Code Playgroud)

我用链式比较与JobTitlecomparatorNameComparator实现这一目标:

public class EmployeeChainedComparator implements Comparator<Employee> {

    private List<Comparator<Employee>> listComparators;

    @SafeVarargs
    public EmployeeChainedComparator(Comparator<Employee>... comparators) {
        this.listComparators = Arrays.asList(comparators);
    }

    @Override
    public int compare(Employee emp1, Employee emp2) {
        for (Comparator<Employee> comparator : listComparators) {
            int result = comparator.compare(emp1, emp2);
            if (result != 0) {
                return result;
            }
        }
        return 0;
    }
}

public class …
Run Code Online (Sandbox Code Playgroud)

java comparator java-8

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

打印从1到10 ^ n的所有整数,其中包含唯一数字

我有一个工作示例来打印唯一的数字(即123有唯一的数字但11没有)

下面是代码

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any number : ");
int n = Integer.parseInt(br.readLine());
double val = Math.pow(10, n);
List<Integer> list = new ArrayList<>(); 
for(int k=1;k<=val;k++){
    String s = Integer.toString(k);
    if(s.length() == 1){
        list.add(k); 
    } else{
        int l = s.length();
        int flag = 0;
        for (int i = 0; i < l - 1; i++) {
            for (int j = i + 1; j < l; j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    flag = 1;
                    break; …
Run Code Online (Sandbox Code Playgroud)

java java-8

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

将具有return语句的Java普通for循环转换为Java8 IntStream

下面是我的普通for循环,我想重构相同的代码以使用java8 IntStream.

for(int i=0; i<= historyList.size(); i++) {
            if (isExist(historyList, status, i)) {
                return historyList.get(i).getCreated();
            }
        }
Run Code Online (Sandbox Code Playgroud)

以下是重构IntStream版本

IntStream.rangeClosed(0, historyList.size()).forEach(i -> {
            if (isExist(historyList, status, i)) {
                return historyList.get(i).getCreated(); -- Error: Unexpected return value
            }
        });
Run Code Online (Sandbox Code Playgroud)

但如上图所示,在返回时收到错误.

错误:意外的返回值

如何以正确的方式重构上面的代码?

java java-8

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

如何使用java8在字符串中查找第一个重复和不重复的字符

我有一个有效的示例,使用java 7在字符串中查找第一个重复和不重复的字符

以下是工作示例

public class FindFirstRepeatedAndNonRepeatedChar {
    static void firstRepeatedNonRepeatedChar(String inputString) {

        HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();

        char[] strArray = inputString.toCharArray();

        for (char c : strArray) {
            if (charCountMap.containsKey(c)) {
                charCountMap.put(c, charCountMap.get(c) + 1);
            } else {
                charCountMap.put(c, 1);
            }
        }

        for (char c : strArray) {
            if (charCountMap.get(c) == 1) {
                System.out.println("First Non-Repeated Character In '" + inputString + "' is '" + c + "'");

                break;
            }
        }

        for (char c : strArray) {
            if (charCountMap.get(c) …
Run Code Online (Sandbox Code Playgroud)

java java-8

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

Spring Boot应用程序启动时出现IllegalAccessError

将Spring Boot版本从1.2.6.RELEASE升级到1.3.4.RELEASE后出现以下错误

较早的应用程序可以通过Spring Boot正常启动,但是在升级版本应用程序后,如果无法启动,则需要对最新版本进行任何修改,然后才能启动应用程序。

Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.springframework.core.convert.support.DefaultConversionService.addCollectionConverters(Lorg/springframework/core/convert/converter/ConverterRegistry;)V from class org.springframework.boot.bind.RelaxedConversionService
    at org.springframework.boot.bind.RelaxedConversionService.<init>(RelaxedConversionService.java:52)
    at org.springframework.boot.bind.RelaxedDataBinder.modifyProperties(RelaxedDataBinder.java:148)
    at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:128)
    at org.springframework.validation.DataBinder.bind(DataBinder.java:631)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:269)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:241)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.bindToSpringApplication(ConfigFileApplicationListener.java:230)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:181)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:166)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:152)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
    at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:65)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:330)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:126)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:75)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:55)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
    at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:65)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:330)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at com.gap.mosaic.trailerevent.ebo.integration.service.configuration.Application.main(Application.java:19)
Run Code Online (Sandbox Code Playgroud)

以下是我的gradle依赖项 …

spring-integration spring-boot

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