小编Shi*_*rty的帖子

具有多个if条件的编码标准

我需要根据某些条件在实体中设置数据.

我用下面的数据来设置

if (StringUtils.isNotBlank(customerVO.getGender())) {
    mstCustomer.setGender(customerVO.getGender());
}
if (StringUtils.isNotBlank(customerVO.getBirthDate())) {
    mstCustomer.setDob(DateUtils.getUtilDate(customerVO.getBirthDate()));
}

if (StringUtils.isNotBlank(customerVO.getAdd1())) {
    mstCustomer.setAddress1(customerVO.getAdd1());
}
if (StringUtils.isNotBlank(customerVO.getAdd2())) {
    mstCustomer.setAddress2(customerVO.getAdd2());
}
if (StringUtils.isNotBlank(customerVO.getAdd3())) {
    mstCustomer.setAddress3(customerVO.getAdd3());
}
if (StringUtils.isNotBlank(customerVO.getPincode())) {
    mstCustomer.setPinCode(customerVO.getPincode());
}
if (StringUtils.isNotBlank(customerVO.getStateName())) {
    MstState state = mstStateRepository.findByName(customerVO.getStateName());
    mstCustomer.setMstState(state);
}

if (StringUtils.isNotBlank(customerVO.getCity())) {
    MstCity city = mstCityRepository.findByName(customerVO.getCity());
    mstCustomer.setMstCity(city);
}

if (StringUtils.isNotBlank(customerVO.getIdentificationType())) {
    mstCustomer.setIdentificationType(customerVO.getIdentificationType());
}

if (StringUtils.isNotBlank(customerVO.getIdentificationData())) {
    mstCustomer.setIdentificationData(customerVO.getIdentificationData());
}

MstStatus mstStatus = mstStatusRepository.findOne(MstStatusEnum.CUST_ACTIVE.getStatusCode());
if (mstStatus != null) {
    mstCustomer.setMstStatus(mstStatus);
}

if (!StringUtils.isBlank(customerVO.getMaritalStatus())) {
    mstCustomer.setMaritalStatus(customerVO.getMaritalStatus());
}
if (StringUtils.isBlank(customerVO.getWeddingAnniversary())) {
    mstCustomer.setWeddingAnniversary(DateUtils.getDateFromString(customerVO.getWeddingAnniversary())); …
Run Code Online (Sandbox Code Playgroud)

java

15
推荐指数
4
解决办法
2291
查看次数

12
推荐指数
3
解决办法
6943
查看次数

从字符串数组中查找重复值

我发现了两种从字符串数组中找到重复值的方法.

第一种方式:

private static String FindDupValue(String[] sValueTemp) {
    for (int i = 0; i < sValueTemp.length; i++) {
      String sValueToCheck = sValueTemp[i];
      if(sValueToCheck==null || sValueToCheck.equals(""))continue;
      for (int j = 0; j < sValueTemp.length; j++) {
        if(i==j)continue;
        String sValueToCompare = sValueTemp[j];
        if (sValueToCheck.equals(sValueToCompare)){
          return sValueToCompare;
        }
      }

    }
    return "";

  }
Run Code Online (Sandbox Code Playgroud)

第二种方式:

private static String FindDupValueUsingSet(String[] sValueTemp) {
    Set<String> sValueSet = new HashSet<String>();
    for(String tempValueSet : sValueTemp) {
      if (sValueSet.contains(tempValueSet))
        return tempValueSet;
      else
        if(!tempValueSet.equals(""))
          sValueSet.add(tempValueSet);
    }
    return "";
  }
Run Code Online (Sandbox Code Playgroud)

两种方法都是正确的.

我的问题是哪种方法最好,为什么?或者有没有其他最好的方法来找出数组的重复值?

java arrays

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

不能转换为 [Ljava.lang.Comparable

所以我需要做动态有序列表。

    public class DynArrayListOrd<T extends Comparable<T>>  {
        private T[] tab ;

        public DynArrayListOrd()
        {
          tab = (T[])new Object[startSize];
        }
        ....

        main {
          DynArrayListOrd tab = new DynArrayListOrd();

          tab.add("John");
          tab.add("Steve");
        }
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,出现错误:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    at structures.DynArrayListOrd.<init>(DynArrayListOrd.java:14)
    at structures.DynamicArrayAppp.main(DynArrayListOrd.java:119)
Run Code Online (Sandbox Code Playgroud)

java compareto

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

Java:无限循环

有很多选项可用于无限循环,但这主要是使用 while(true) or for(;;)

我知道这while(true)是最好的选择,因为它更容易理解.

但我想用for(;;).

我想知道for loop当我;在for循环中使用两个时发生了什么.

for(;;)

分号意味着它是一个空话.但是当我们在内部使用for loop无限执行时它是如何工作的?

java

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

该方法的Cyclomatic Complexity大于授权

我使用下面的方法来检查null或空字段:

public boolean isVoNotNull(){
        return null != this.cardNo &&  StringUtils.isNotBlank(this.cardNo) 
                && null != this.otp && StringUtils.isNotBlank(this.otp) 
                && null != this.password && StringUtils.isNotBlank(this.password)
                && null != this.userid && StringUtils.isNotBlank(this.userid) 
                && null != this.type && StringUtils.isNotBlank(this.type) 
                && null != this.walletMobileNo && StringUtils.isNotBlank(this.walletMobileNo);
    }
Run Code Online (Sandbox Code Playgroud)

但是在使用SonarLint验证此代码时,请低于异常

EXCEPTION:此方法"isVoNotNull"的Cyclomatic Complexity为12,大于10授权.

如何解决此异常或如何从代码中删除复杂性?

java cyclomatic-complexity

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

Java中的多重返回语句

private void test() {           
        // Nested if block 
        if (true) { // If condition is true
            if (true) { // If condition is true
                if (true) { // If condition is true
                    if (true) { // If condition is true
                         // statement
                    }
                }
            }
        }

        // Above block breaking in below way
        // Breaking nested if in multiple with return statement
        if (false) { // If condition is false
            return;
        }

        if (false) { // If condition is false …
Run Code Online (Sandbox Code Playgroud)

java

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

计算两个任意形状之间的最小距离

我有两个任意形状。现在我想计算两个形状之间的最小距离。这里我附上图片

在此输入图像描述

首先绘制部分完成。这个形状是圆弧和直线的组合。现在,当我要计算这些形状之间的最小距离时,我遇到了问题。使用 GWT (java) html5 canvas 绘制此形状。

为了计算两个形状之间的最小距离,我在java中使用了下面的代码,但我没有得到任何优化的方法来做到这一点 -

private double calculateMinimumDistance(Coordinate[] coordinates_1, Coordinate[] coordinates_2) { 
    double minDistance = 100000;
    double currentDistance = 0;

    for(int i = 0; i < coordinates_1.length; ++i) {
      for(int j = 0; j < coordinates_2.length; ++j) {
        currentDistance = coordinates_1[i].distanceTo(coordinates_2[j]);
        if(currentDistance < minDistance) {
          minDistance = currentDistance;
        }  
      } 
    }

    return minDistance;
}
Run Code Online (Sandbox Code Playgroud)

坐标_1包含形状1的点的集合。
坐标_2包含形状2的点的集合。

有没有优化的方法来计算两个形状之间的距离?这些形状可以是任何地点和任何类型的形状。

我们可以通过计算线到线、线到圆弧或圆弧到圆弧之间的距离来以优化的方式来计算两组点之间的最小距离。这样我们就可以以优化的方式计算出最小距离。

java geometry

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

从 switch 重构多个 case

TestDTO testDTO = new TestDTO();
    
for (Object attribute : row.getAttributes()) {
    switch (attribute) {
    case "CATEGORY":
        testDTO.setCategory((String) attribute);
        break;
    case "DESCRIPTION":
        testDTO.setDescription((String) attribute);
        break;
    case "NOTE":
        testDTO.setNote((String) attribute);
        break;
    case "FEATURES":
        testDTO.setFeatures((String) attribute);
        break;
    case "INDICATOR":
        testDTO.setIndicator((String) attribute);
        break;
    case "LABEL":
        testDTO.setLabel((String) attribute);
        break;
    case "TYPE":
        testDTO.setType((String) attribute);
        break;
    default:

    }
}
Run Code Online (Sandbox Code Playgroud)

正如你在上面的代码中看到的,我们使用多个 case 来设置数据。代码工作正常。

有什么方法可以减少设置这些数据的多种情况。

在上面的代码中,问题是可维护性。因为假设如果我们有 30 个字段,那么我们需要为此放置 30 个案例。

有没有其他方法可以实现相同的目标?

java

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

int to Long解析错误

  class Test{  
    static void testCase_1(long l){System.out.println("Long");}  
    static void testCase_2(Long l){System.out.println("Long");}

    public static void main(String args[]){  
        int a = 30; 

        testCase_1(a);  // Working fine
        testCase_2(a);  // Compilation time error

        //Exception - The method testCase_2(Long) in the type Test is not applicable for the arguments (int)
      }   
    } 
Run Code Online (Sandbox Code Playgroud)

testCase - 1:int - 长期正常工作

testCase - 2:int to L ong抛出异常

为什么testCase_2()方法抛出编译异常?

java

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