小编Fre*_*ded的帖子

使用System.arraycopy(...)比复制数组的for循环更好吗?

我想创建一个新的对象数组,将两个较小的数组放在一起.

它们不能为空,但大小可能为0.

我无法在这两种方式之间进行选择:它们是等效还是更高效(例如system.arraycopy()复制整个块)?

MyObject[] things = new MyObject[publicThings.length+privateThings.length];
System.arraycopy(publicThings, 0, things, 0, publicThings.length);
System.arraycopy(privateThings, 0, things,  publicThings.length, privateThings.length);
Run Code Online (Sandbox Code Playgroud)

要么

MyObject[] things = new MyObject[publicThings.length+privateThings.length];
for (int i = 0; i < things.length; i++) {
    if (i<publicThings.length){
        things[i] = publicThings[i]
    } else {
        things[i] = privateThings[i-publicThings.length]        
    }
}
Run Code Online (Sandbox Code Playgroud)

唯一的区别是代码的外观?

编辑:感谢链接的问题,但他们似乎有一个未解决的讨论:

如果it is not for native types:byte [],Object [],char [] 真的更快吗?在所有其他情况下,执行类型检查,这将是我的情况,因此将是等效的......不是吗?

在另一个相关问题上,他们说the size matters a lot,对于size> 24,system.arraycopy()获胜,小于10,手动for循环更好......

现在我真的很困惑.

java

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

Java8 Lambdas和Exceptions

我想知道是否有人可以向我解释以下奇怪之处.我正在使用Java 8更新11.

鉴于这种方法

private <F,T> T runFun(Function<Optional<F>, T> fun, Optional<F> opt) {
   return fun.apply(opt) ;
}
Run Code Online (Sandbox Code Playgroud)

如果我首先构造一个函数Object,并将其传递给上面的方法,那么事情就会编译.

private void doesCompile() {
    Function<Optional<String>, String> fun = o -> o.orElseThrow(() -> new RuntimeException("nah"));
    runFun(fun, Optional.of("foo"));

}
Run Code Online (Sandbox Code Playgroud)

但是,如果我将函数内联为lambda,编译器会说

未报告的例外X; 必须被抓住或宣布被抛出

private void doesNotCompile () {
    runFun(o -> o.orElseThrow(() -> new RuntimeException("nah")), Optional.of("foo"));
}
Run Code Online (Sandbox Code Playgroud)

更新:原来错误消息由maven缩写.如果直接用javac编译,则错误是:

error: unreported exception X; must be caught or declared to be thrown
            runFun(o -> o.orElseThrow(() -> new RuntimeException("nah")), Optional.of("foo"));
                                     ^
  where X,T are type-variables:
    X extends Throwable declared …
Run Code Online (Sandbox Code Playgroud)

java lambda java-8

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

将eclipse升级到java 8

我正在尝试将我的eclipse环境更新为java 8.我安装了jdk和jre版本8.我也这样做了:

https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler

然后在项目 - >属性 - > java构建路径 - > JRE系统库 - >编辑 - >执行环境 - > JavaSE-1.8(未绑定)这是唯一一个在其名称中包含java 8的选项.但是当我选择它时它会说:工作空间中没有与指定执行环境兼容的JRE:JavaSE-1.8

java eclipse upgrade java-8

34
推荐指数
3
解决办法
7万
查看次数

Jquery按钮单击()函数不起作用

我正在尝试创建动态表单,用户可以根据需要添加动态文本字段.这是我的jquery代码..

$(document).ready(function() {
    $("#add").click(function() {
        var intId = $("#buildyourform div").length +1;
        var fieldWrapper = $("<div class=\"fieldwrapper\" name=\"field" + intId + "\" id=\"field" + intId + "\"/>");
        var fName = $("<input type=\"text\" name=\"name\" class=\"fieldname\" id=\"tb"+ intId +"_1\"/>");
        var lname = $("<input type=\"text\" name=\"email\" class=\"lastname\" id=\"tb"+ intId +"_2\"/>");
        var removeButton = $("<input type=\"button\" class=\"remove\" value=\"-\" />");

        var addButton = $("<input type=\"button\" class=\"add\" id=\"add\" value=\"+\" />")
        removeButton.click(function() {
            $(this).parent().remove();
        });
        fieldWrapper.append(fName);
        fieldWrapper.append(lname);
        fieldWrapper.append(removeButton);
        fieldWrapper.append(addButton);
        $(this).remove();
        $("#buildyourform").append(fieldWrapper);

    });

});
Run Code Online (Sandbox Code Playgroud)

和Html代码是......

<fieldset id="buildyourform">
    <legend>Build your …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery

26
推荐指数
2
解决办法
11万
查看次数

如何使用jsp传递Object:将param标签包含到另一个jsp中

我试图使用jsp:include标签将DTO对象从一个jsp发送到另一个jsp.但它始终将其视为字符串.我无法在包含的jsp文件中使用DTO.

这是一个代码..

<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">  
         <jsp:include page="attributeSubFeatureRemove.jsp" >
             <jsp:param name="attribute" value="${attribute}" />
         </jsp:include>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)

attributeSubFeatureRemove.jsp文件..

<c:set value="${param.attribute}" var="attribute" />
<c:forEach items="${attribute.subFeatures}" var="subAttribute">
    <c:forEach items="${subAttribute.attributeValues}" var="subValue">
        <c:if test="${ subValue.preSelectionRequired}">
            <c:set var="replaceParams" value=":${subAttribute.name}:${subValue.name}" />
            <c:set var="removeURL" value="${fn:replace(removeURL, replaceParams, '')}" />
        </c:if>
    </c:forEach> 
    <jsp:include page="attributeSubFeatureRemove.jsp">
        <jsp:param name="subAttribute" value="${subAttribute}" />
    </jsp:include> 
</c:forEach>
Run Code Online (Sandbox Code Playgroud)

这里我试图从param获取属性值,它总是发送String Type Value.有没有办法在attributeSubFeatureRemove jsp文件中发送Object(DTO)?请帮忙.

html java jsp jstl jsp-tags

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

"omniauth-twitter"电子邮件ID不是来自ruby on rails的twitter

我正在使用omn​​iauth-twitter gem在我的rails应用程序中启用Twitter登录.这是我的代码......

gemfile -

gem 'omniauth', '~> 1.1.1'
gem 'omniauth-twitter'
Run Code Online (Sandbox Code Playgroud)

routes.rb -

 match '/auth/twitter/callback', to: 'users#twitter_login'
 match 'auth/failure', to: 'static_pages#home'
Run Code Online (Sandbox Code Playgroud)

User_controller.rb -

     def twitter_login
       auth = request.env['omniauth.auth'] 
       authentication = Authentication.find_by_provider_and_uid(auth['provider'],auth['uid'])
       if authentication
          sign_in authentication.user
          redirect_to root_url
       else
         if(User.where(:email => auth['extra']['raw_info']['email']).exists?)
            flash[:notice] = "You already have account in ibetter"
            redirect_to root_url        
         else
            user = User.new
            user.apply_omniauth(auth)        
            if user.save(:validate => false)     
              sign_in user           
              flash[:notice] = "Welcome to Ginfy"          
              redirect_to root_url
            else
              flash[:error] = "Error while creating a user account. Please try …
Run Code Online (Sandbox Code Playgroud)

ruby twitter ruby-on-rails omniauth ruby-on-rails-3

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

如何在GSP中使用Spring Security Grails插件获取current_user

我是Grails的新手.我使用Spring Security Grails插件进行身份验证.我想在我的视图gsp文件中获取当前用户.

我这样想...

<g:if test="${post.author == Person.get(springSecurityService.principal.id).id }">
      <g:link controller="post" action="edit" id="${post.id}">
            Edit this post
      </g:link>
</g:if>
Run Code Online (Sandbox Code Playgroud)

在这里,我想显示编辑此帖子链接,仅显示由signed_in用户创建的帖子.但它显示出错误 -

Error 500: Internal Server Error

 URI
    /groovypublish/post/list
 Class
   java.lang.NullPointerException
 Message
   Cannot get property 'principal' on null object
Run Code Online (Sandbox Code Playgroud)

这是我的Post.groovy -

class Post {

static hasMany = [comments:Comment]

String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
Person author

....... more code ....
Run Code Online (Sandbox Code Playgroud)

这是我的Person.groovy域类文件 -

class Person {

transient springSecurityService

String realName
String …
Run Code Online (Sandbox Code Playgroud)

grails spring-security gsp grails-2.0

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

如何锁定Hybris在生产环境中初始化系统

有没有办法通过在生产环境中意外初始化系统来锁定Hybris.如果有人错误地点击初始化按钮,那就太冒险了.

hybris

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

Java8流出奇怪的行为

我正在测试一些Java8流API代码,但我无法弄清楚这个代码发生了什么.

我在考虑ParallelStream以及它是如何工作的,我进行了一些比较.两个不同的方法进行大迭代,添加32.768.000个BigDecimals,一个使用ParallelStream,另一个使用正常迭代.我参加了一项测试,我知道它无效,但有些事情引起了我的注意.

测试是:

并行流:

private static void sumWithParallelStream() {
    BigDecimal[] list = new BigDecimal[32_768_000];
    BigDecimal total = BigDecimal.ZERO;
    for (int i = 0; i < 32_768_000; i++) {
        list[i] = new BigDecimal(i);
    }
    total = Arrays.asList(list).parallelStream().reduce(BigDecimal.ZERO, BigDecimal::add);
    System.out.println("Total: " + total);
}
Run Code Online (Sandbox Code Playgroud)

普通代码:

private static void sequenceSum() {
    BigDecimal total = BigDecimal.ZERO;
    for (int i = 0; i < 32_768_000; i++) {
        total = total.add(new BigDecimal(i));
    }
    System.out.println("Total: " + total);
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Total: 536870895616000
sumWithParallelStream(): 30502 ms

Total: 536870895616000 …
Run Code Online (Sandbox Code Playgroud)

java performance java-8 java-stream

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

Hybris Mockito:获取异常没有(分离的)模型的LocaleProvider

我正在Hybris项目的门面层面编写测试用例.我正在创建模型实例并设置名称和代码.模型有一些属性本地化,因为我得到了no LocaleProvider异常.

java.lang.IllegalStateException: there is no LocaleProvider for (detached) model de.hybris.platform.servicelayer.model.ItemModelContextImpl@66c677a7
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.getLocaleProvider(ItemModelContextImpl.java:481)
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.getCurrentLocale(ItemModelContextImpl.java:469)
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.toDataLocale(ItemModelContextImpl.java:406)
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.getLocalizedValue(ItemModelContextImpl.java:323)
at de.hybris.platform.catalog.model.classification.ClassificationAttributeModel.getName(ClassificationAttributeModel.java:227)
at de.hybris.platform.catalog.model.classification.ClassificationAttributeModel.getName(ClassificationAttributeModel.java:217)
Run Code Online (Sandbox Code Playgroud)

这是一个测试类

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Locale.class, Config.class })
public class HCCB2BClassificationFacadeUnitTest {

@InjectMocks
private final HCCB2BClassificationFacade hccb2bClassificationFacade = new HCCB2BClassificationFacadeImpl();

@Mock
HCCB2BClassificationService hccb2bClassificationService = new HCCB2BClassificationServiceImpl();

@Mock
private SessionService sessionService;

@Mock
private HCCB2BClassificationDAO hccb2bClassificationDAO;

@Mock
private SearchRestrictionService searchRestrictionService;

@Before
public void setUp() throws SystemException {
    MockitoAnnotations.initMocks(this);
    ClassAttributeAssignmentModel classAttributeAssignmentModel = new ClassAttributeAssignmentModel();
    ClassificationAttributeModel classificationAttributeModel = new ClassificationAttributeModel();
    classificationAttributeModel.setCode("Procedure"); …
Run Code Online (Sandbox Code Playgroud)

mocking mockito powermock hybris powermockito

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