小编chi*_*tiz的帖子

Java 8 Streams FlatMap方法示例

我一直在检查即将到来的Java update,即:Java 8 or JDK 8.是的,我很不耐烦,有很多新东西,但是,有一些我不明白的东西,一些简单的代码:

final Stream<Integer>stream = Stream.of(1,2,3,4,5,6,7,8,9,10);
stream.flatMap();
Run Code Online (Sandbox Code Playgroud)

javadoc是

public <R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)

返回一个流,该流包含将此流的每个元素替换为通过将提供的映射函数应用于每个元素而生成的映射流的内容的结果.每个映射的流在其内容放入此流后关闭.(如果映射的流为空,则使用空流.)这是一个中间操作.

如果有人创建了一些简单的现实生活示例flatMap,如何在以前的java版本Java[6,7]中编写代码以及如何使用相同的例程编写代码,我将不胜感激Java 8.

java java-8 java-stream flatmap

84
推荐指数
4
解决办法
11万
查看次数

Valhalla项目的价值类型是什么?

我开始阅读有关瓦尔哈拉项目的内容,而且有一些我真的不明白的事情就是这样Value Types.

这就是我的理解:

1)对象是否无法将其作为参考进行比较?

final ValueType a = new ValueType();
final ValueType b = a;
System.out.println(a==b); returns false????
Run Code Online (Sandbox Code Playgroud)

Google AutoValue 代码示例中,它说明了

if(o == this){return true;}//equals method implementation what is this? I am comparing references here right?
Run Code Online (Sandbox Code Playgroud)

2)根据维基百科,高效的小"对象"没有继承.做什么Small Objects?Without inheritance意味着什么?

这是不可能使用VT?

public final class ValueType extends Any //is this not possible??
Run Code Online (Sandbox Code Playgroud)

3)为什么使用它们?将使用哪种方案以及如何使用它.

4)Google AutoValue Library简而言之,值类型对象是没有标识的对象,即如果它们各自的内部状态相等,则认为两个值对象相等.我的问题是:他们是否有州,他们应该实施equalshashcode.什么是没有身份的对象是什么意思?

5)这个断言是否正确?

public static void …
Run Code Online (Sandbox Code Playgroud)

java value-type project-valhalla

29
推荐指数
2
解决办法
5303
查看次数

Java 8 Comparator nullsFirst naturalOrder困惑

这可能是一个简单的问题,但我想清楚地理解它......

我有这样的代码:

public final class Persona
{
   private final int id;
   private final String name
   public Persona(final int id,final String name)
   {
       this.id = id;
       this.name = name;
   }
   public int getId(){return id;}    
   public String getName(){return name;}     
   @Override
   public String toString(){return "Persona{" + "id=" + id + ", name=" + name+'}';}    
 }
Run Code Online (Sandbox Code Playgroud)

我正在测试这段代码:

import static java.util.Comparator.*;
private void nullsFirstTesting()
{               
    final Comparator<Persona>comparator = comparing(Persona::getName,nullsFirst(naturalOrder()));
    final List<Persona>persons = Arrays.asList(new Persona(1,"Cristian"),new Persona(2,"Guadalupe"),new Persona(3,"Cristina"),new Persona(4,"Chinga"),new Persona(5,null));
    persons
            .stream()
            .sorted(comparator)
            .forEach(System.out::println);                           
}
Run Code Online (Sandbox Code Playgroud)

这显示以下结果:

Persona{id=5, …
Run Code Online (Sandbox Code Playgroud)

java sorting string comparator java-8

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

Hibernate @DynamicUpdate(value = true)@SelectBeforeUpdate(value = true)性能

我开始在我的APP中使用这2个休眠注释.

@DynamicUpdate(value=true)
@SelectBeforeUpdate(value=true) 
Run Code Online (Sandbox Code Playgroud)

首先,我将尝试解释我对它的理解,以了解我是否正确.

@DynamicUpdate(value=true)
Run Code Online (Sandbox Code Playgroud)

仅更新modified values实体中的内容Hibernate needs to track those changes

@SelectBeforeUpdate(value=true)
Run Code Online (Sandbox Code Playgroud)

创建一个select之前update知道哪些属性已被更改,这在实体已在不同会话上加载和更新时很有用Hibernate is out of tracking entity changes

这2个肯定是否正确?

我主要担心的是.

DB performance哪个更好或更快地更新所有的实体领域的一次generate a select to know which columns update and update only the modified columns?

java select hibernate sql-update

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

Java Hibernate使用addEntity创建SQLQuery

我需要应用与此类似的SQL查询.

SELECT
    id as id,
    c03 as c03,
    c34 as c34 
FROM
    (SELECT
        id,
        c03,
        c34 
    FROM
        students
    where
        c34 in(
            ?, ?,?,?
        ) 
    order by
        id desc) o 
group by
    c34;
Run Code Online (Sandbox Code Playgroud)

和我的Java代码.

private final void retrieveStudents(){
    final List result = currentSession()
            .createSQLQuery("SELECT id as id,c03 as c03,c34 as c34 FROM (SELECT id,c34,c03 FROM students where c34 in(:filters) order by id desc) o group by c34;")
            .setParameterList("filters",Arrays.asList(74,1812))
            .list();
    result.forEach(this::consumer1);
}
Run Code Online (Sandbox Code Playgroud)

查询就是OK.返回一个objets数组,我可以迭代,但我想返回一个Student对象,所以我添加.

   .addEntity(Student.class)
Run Code Online (Sandbox Code Playgroud)

但是说错误就是抛出

Column 'ACTIVE' not found.
Run Code Online (Sandbox Code Playgroud)

我也试过.addScalar,但同样的事情发生了.

.addScalar("id",org.hibernate.type.IntegerType.INSTANCE)
.addScalar("c03",org.hibernate.type.DateType.INSTANCE) …
Run Code Online (Sandbox Code Playgroud)

java hibernate

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

Java Hibernate Bug错误的参数绑定

我有这个简单的Hibernate代码.

public List<Student>bug(){
    //SimpleCriteria
    final Criterion eq = and(Restrictions.eq("fdl","N"),Restrictions.eq("cid",1),Restrictions.eq("did",2));
    return currentSession().createCriteria(Student.class)                
           .createAlias("school","s",JoinType.INNER_JOIN,Restrictions.eq("zipCode",1764))               
           .createAlias("address","a",JoinType.LEFT_OUTER_JOIN,eq)
           .setProjection(addProjection("id"))
           .setResultTransformer(transformer(Student.class))                
           .list();
}      
Run Code Online (Sandbox Code Playgroud)

问题是参数和某种程度上混乱或混合或处于错误位置时每个问题出现时我创建两个createAlias都至少有这样的标准(见下面的更新)

createAlias With Some Criterions

createAlias("school","s",JoinType.INNER_JOIN,Restrictions.eq("zipCode",1764))               
createAlias("address","a",JoinType.LEFT_OUTER_JOIN,eq)
Run Code Online (Sandbox Code Playgroud)

生成的sql看起来不错..

select
    this_.ID as y0_ 
from
    student this_ 
left outer join
    address address2_ 
        on this_.C05=address2_.ID 
        and (
            (
                address2_.FDL=? 
                and address2_.CID=? 
                and address2_.DID=?
            ) 
        ) 
inner join
    school school_ 
        on this_.C03=school_.ID 
        and (
            school_.C06=? //ZIPCODE
        )
Run Code Online (Sandbox Code Playgroud)

即使我看到log4j,我也能看到错误的绑定 在此输入图像描述

您可以看到邮政编码值1764绑定到第一个参数address2_.FDL

binding parameter [1] as [INTEGER] - [1764]
Run Code Online (Sandbox Code Playgroud)

稍后,第二个参数cid被赋予fdl的正确先前值,即'N'

Message: binding parameter [2] as [VARCHAR] - [N]
Run Code Online (Sandbox Code Playgroud)

之后,为cid分配的第三个参数是1的正确的先前值

binding …
Run Code Online (Sandbox Code Playgroud)

java hibernate hibernate-5.x

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

Java 8 mapToInt和toIntFunction示例

我正在测试Java的新主要更新AKA Java 8非常有趣.我正在使用流,特别是我正在使用这个简单的代码.

private void getAvg()
{
    final ArrayList<MyPerson>persons = new ArrayList<>
    (Arrays.asList(new MyPerson("Ringo","Starr"),new MyPerson("John","Lennon"),new MyPerson("Paul","Mccartney"),new MyPerson("George","Harrison")));                
    final OptionalDouble average = persons.stream().filter(p->p.age>=40).mapToInt(p->p.age).average();
    average.ifPresent(System.out::println);        
    return;
}            
private class MyPerson
{
    private final Random random = new Random();
    private final String name,lastName;
    private int age;
    public MyPerson(String name,String lastName){this.name = name;this.lastName = lastName;this.age=random.nextInt(100);}    
    public MyPerson(String name,String lastName,final int age){this(name,lastName);this.age=age;}    
    public String getName(){return name;}
    public String getLastName(){return lastName;}                   
    public int getAge(){return age;}        
}            
Run Code Online (Sandbox Code Playgroud)

在这个例子中我非常清楚,但后来我也看到也可以用这种方式完成它.

final OptionalDouble average = persons.stream().filter(p->p.age>=40)
.mapToInt(MyPerson::getAge).average();
average.ifPresent(System.out::println);        
Run Code Online (Sandbox Code Playgroud)

我检查了方法 …

java java-8 java-stream

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

如何在Spring Expression Language中引用常量

我是Spring的新手,我有一个bean声明,如下所示.

    <bean id="mybean" class="" scope="prototype">
       <property name='typeOf' value='#{typeOfBuilder.getKeyFor("OPEN_DATE").getId()}'/>    
</bean> 
Run Code Online (Sandbox Code Playgroud)

typeOf是一种Integer类型,它是在这种情况下typeOfBuilder构建的另一个表的键.KeyOPEN_DATE

这段代码工作正常,但有一个限制.OPEN_DATE是一个NON-MANAGE Spring Bean像下面这样的常量.

public final class Constants
{
     public final static String KEY_FOR_OPEN_DATE = "OPEN_DATE";     
} 
Run Code Online (Sandbox Code Playgroud)

并强烈建议能够参考它!!

这样的事情.

<util:constant id="PATH_TO_CONSTANT" static-field="myPath"/>
<property name='typeOf' value='#{typeOfBuilder.getKeyFor(PATH_TO_CONSTANT).getId()}'/>  
Run Code Online (Sandbox Code Playgroud)

任何帮助都非常感激.

java spring spring-el

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

Java 8构造函数方法引用

我正在阅读perform message它附带的样本我重现..

@FunctionalInterface
public interface Action {
    public void perform();
}
Run Code Online (Sandbox Code Playgroud)

实施者

public final class ActionImpl implements Action {
    public ActionImpl() {
        System.out.println("constructor[ActionIMPL]");
    }

    @Override
    public void perform() {
        System.out.println("perform method is called..");
    }    
}
Run Code Online (Sandbox Code Playgroud)

来电者.

public final class MethodReferences {

    private final Action action;

    public MethodReferences(Action action) {
        this.action = action;
    }

    public void execute() {
        System.out.println("execute->called");
        action.perform();
        System.out.println("execute->exist");
    }

    public static void main(String[] args) {
        MethodReferences clazz = new MethodReferences(new ActionImpl());
        clazz.execute();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果调用此方法则将以下内容打印到输出中

constructor[ActionIMPL] …
Run Code Online (Sandbox Code Playgroud)

java java-8 method-reference constructor-reference

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

Java 8日期和时间时间字段

我浏览Java 8日期和时间新的API,我不能再进一步,我无法在网上找到任何好的资源,我希望你对这个问题点燃.

我的简单源代码是

final LocalDate date = LocalDate.now();
date.get(TemporalField field)
Run Code Online (Sandbox Code Playgroud)

我得到源代码,我明白了.

public int get(TemporalField field)
  Gets the value of the specified field from this date as an int.
Run Code Online (Sandbox Code Playgroud)

我的问题是如何获得TemporalField或哪种方法可以让这段代码正常工作......

final int value = date.get(????);
Run Code Online (Sandbox Code Playgroud)

这要归功于JB Nizet

date.get(java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH);
Run Code Online (Sandbox Code Playgroud)

API.

http://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoField.html
Run Code Online (Sandbox Code Playgroud)

java datetime java-8

7
推荐指数
2
解决办法
7542
查看次数