java-8

Sorting a List of Pairs java [duplicate]

◇◆丶佛笑我妖孽 提交于 2020-01-07 03:18:27
问题 This question already has answers here : Very confused by Java 8 Comparator type inference (4 answers) Closed 5 months ago . I've List with Pairs in it. I want to sort them according to the keys first and if keys are same then sort according to the values. I tried following code but throws an exception incompatible types: cannot infer type-variable(s) T List<Pair<Integer,Integer>> list = new ArrayList<>(); list.sort(Comparator.comparingInt(Pair::getKey).thenComparingInt(Pair::getValue); Error

convert method to java 8 lambda

扶醉桌前 提交于 2020-01-07 02:15:11
问题 How to write this method in functional programming by using Java 8 lambda / stream() in a short more intuitive way? int animation = R.style.DialogAnimationSlideHorizontal; String body; String subject = mSubjectView.getText().toString(); BaseDialog dialog = dialogFactory.getType(DialogTypes.DIALOG_FULL); if (!checkFields()) { // one of the recipients is invalid. body = getString(R.string.bad_address); dialog.showDialog(body, animation, new DialogBuilder.Positive() { @Override public void

Explicitly calling a default method in Java

馋奶兔 提交于 2020-01-06 15:20:51
问题 Java 8 introduces default methods to provide the ability to extend interfaces without the need to modify existing implementations. I wonder if it's possible to explicitly invoke the default implementation of a method when that method has been overridden or is not available because of conflicting default implementations in different interfaces. interface A { default void foo() { System.out.println("A.foo"); } } class B implements A { @Override public void foo() { System.out.println("B.foo"); }

How transform anonymous java class to lambda expression?

你说的曾经没有我的故事 提交于 2020-01-06 14:45:45
问题 I have an application on JavaFX. In this application, I need to implement, the editor of the column. In the old version of the code worked perfectly: myColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<MyRowDataObject, String>>() { @Override public void handle(TableColumn.CellEditEvent<MyRowDataObject, String> t) { ((MyRowDataObject) t.getTableView().getItems().get( t.getTablePosition().getRow()) ).setFirstName(t.getNewValue()); } } ); but when I tried to rewrite the code

Parse time of day string

五迷三道 提交于 2020-01-06 14:07:10
问题 Input is a string such as "23:55" or "09:20", i.e. in the "HH:mm" format. I want the string to specify the time of day in UTC. I want to use java.time to parse the string and the output to be an Instant with the specified time of day today. If needed I can append a time zone to the string, i.e. "23:55 UTC". I want to use the resulting instant to calculate the number of nanos between now and the mentioned instant. I.e. Duration.between(Instant.now(), resultingInstant).toNanos(); Edit: Might

Efficient way to group by a given list based on a key and collect in same list java 8

无人久伴 提交于 2020-01-06 12:39:07
问题 I have the below class: class A{ String property1; String property2; Double property3; Double property4; } So the property1 and property2 is the key. class Key{ String property1; String property2; } I already have a list of A like below: List<A> list=new ArrayList<>(); I want to group by using the key and add to another list of A in order to avoid having multiple items with same key in the list: Function<A, Key> keyFunction= r-> Key.valueOf(r.getProperty1(), r.getProperty2()); But then while

Efficient way to group by a given list based on a key and collect in same list java 8

放肆的年华 提交于 2020-01-06 12:38:18
问题 I have the below class: class A{ String property1; String property2; Double property3; Double property4; } So the property1 and property2 is the key. class Key{ String property1; String property2; } I already have a list of A like below: List<A> list=new ArrayList<>(); I want to group by using the key and add to another list of A in order to avoid having multiple items with same key in the list: Function<A, Key> keyFunction= r-> Key.valueOf(r.getProperty1(), r.getProperty2()); But then while

Configuration HikariCP + Spring4 + Hibernate

╄→гoц情女王★ 提交于 2020-01-06 09:06:05
问题 I want to configure HinkariCP data source with Spring4 Java config. My config looks like: @Configuration @EnableJpaRepositories("com.app.dao.repository") @EnableTransactionManagement public class DataAccessConfig { private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect"; private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql"; private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql"; private static final

Configuration HikariCP + Spring4 + Hibernate

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 09:05:20
问题 I want to configure HinkariCP data source with Spring4 Java config. My config looks like: @Configuration @EnableJpaRepositories("com.app.dao.repository") @EnableTransactionManagement public class DataAccessConfig { private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect"; private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql"; private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql"; private static final

How to collect result of a stream into an array of custom object in Java 8 [duplicate]

泄露秘密 提交于 2020-01-06 08:38:10
问题 This question already has answers here : How to convert a Java 8 Stream to an Array? (9 answers) Java stream toArray() convert to a specific type of array (2 answers) Closed 8 months ago . I have an List<TestBuilder> testBuilders; Test has a function build of type Test I did testBuilders.stream().map(Test::build()).collect() I want to collect above in array of Test i.e Test[] I am not sure what would go in collect function 回答1: Use the terminal operation Stream::toArray which packs the