java-8

Parsing a year String to a LocalDate with Java8

*爱你&永不变心* 提交于 2019-12-30 06:20:29
问题 With Joda library, you can do DateTimeFormat.forPattern("yyyy").parseLocalDate("2008") that creates a LocalDate at Jan 1st, 2008 With Java8, you can try to do LocalDate.parse("2008",DateTimeFormatter.ofPattern("yyyy")) but that fails to parse: Text '2008' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2008},ISO of type java.time.format.Parsed Is there any alternative, instead of specifically writing sth like LocalDate.ofYearDay(Integer.valueOf("2008"), 1) ? 回答1:

Hadoop IOException failure to login

孤街浪徒 提交于 2019-12-30 06:01:14
问题 I'm pretty new to Hadoop. However, I've been able to successfully setup hadoop 2.7.3 with Java 7 in the cluster mode on my servers. Everything works totally fine. But then, when I try to switch to Java 8 and start dfs, there is an error: Exception in thread "main" java.io.IOException: failure to login at org.apache.hadoop.security.UserGroupInformation.loginUserFromSubject(UserGroupInformation.java:824) at org.apache.hadoop.security.UserGroupInformation.getLoginUser(UserGroupInformation.java

Java 8 lambdas execution

守給你的承諾、 提交于 2019-12-30 05:59:48
问题 How can I do something like this in Java 8? boolean x = ((boolean p)->{return p;}).apply(true); Right now I get the following error: The target type of this expression must be a functional interface 回答1: As per the JLS section 15.27: It is a compile-time error if a lambda expression occurs in a program in someplace other than an assignment context (§5.2), an invocation context (§5.3), or a casting context (§5.5). It is also possible to use a lambda expression in a return statement. We can

Using streams to manipulate a String

大兔子大兔子 提交于 2019-12-30 05:43:48
问题 Let's say that I want to remove all the non-letters from my String . String s = "abc-de3-2fg"; I can use an IntStream in order to do that: s.stream().filter(ch -> Character.isLetter(ch)). // But then what? What can I do in order to convert this stream back to a String instance? On a different note, why can't I treat a String as a stream of objects of type Character ? String s = "abc-de3-2fg"; // Yields a Stream of char[], therefore doesn't compile Stream<Character> stream = Stream.of(s

Count non null fields in an object

故事扮演 提交于 2019-12-30 04:50:10
问题 I have a UserProfile class which contains user's data as shown below: class UserProfile { private String userId; private String displayName; private String loginId; private String role; private String orgId; private String email; private String contactNumber; private Integer age; private String address; // few more fields ... // getter and setter } I need to count non null fields to show how much percentage of the profile has been filled by the user. Also there are few fields which I do not

What is equivalent to C#'s Select clause in JAVA's streams API

北慕城南 提交于 2019-12-30 04:41:09
问题 I wanted to filter list of Person class and finally map to some anonymous class in Java using Streams. I am able to do the same thing very easily in C#. Person class class Person { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } } Code to map the result in desire format. List<Person> lst = new List<Person>(); lst.Add(new Person() { Name = "Pava", Address = "India", Id = 1 }); lst.Add(new Person() { Name = "tiwari", Address = "USA", Id = 2 });

How does Java 8 know which String::compareTo method reference to use when sorting?

↘锁芯ラ 提交于 2019-12-30 04:31:04
问题 How does Java know which String::compareTo method reference to use when calling Collections.sort(someListOfStrings, String::compareTo); ? compareTo is not static and it needs to know the value of the "left hand side" of the comparison. 回答1: Suppose that you use method reference for Comparator interface: Comparator<String> cmp = String::compareTo; When you call the cmp.compare(left, right) (which is "single abstract method" or "SAM" of Comparator interface), the magic occurs: int result = cmp

How to initialize a map using a lambda?

时间秒杀一切 提交于 2019-12-30 04:24:11
问题 I want to declare a fully populated map field in a single statement, (which may contain several nested statements,) like this: private static final Map<Integer,Boolean> map = something-returning-an-unmodifiable-fully-populated-HashMap; Anonymous initializers won't do, for the same reason that invoking a function which returns a new populated map won't do: they require two top-level statements: one for the variable declaration, and one for the method or initializer. The double curly bracket (

Why does Collections.sort(List) work in Java 8 with CopyOnWriteArrayList but not in Java 7?

邮差的信 提交于 2019-12-30 04:06:33
问题 I can sort a list of users without any problems using the following code and Java 8: CopyOnWriteArrayList<User> allCurrentLoginnedUsersList = new CopyOnWriteArrayList<>(); Collections.sort(allCurrentLoginnedUsersList); Now, I changed to Java 7 and I saw no errors on eclipse. But now, when running under Java 7 I got this error: java.lang.UnsupportedOperationException at java.util.concurrent.CopyOnWriteArrayList$COWIterator.set(CopyOnWriteArrayList.java:1049) at java.util.Collections.sort

Cannot convert void to java.lang.Void

血红的双手。 提交于 2019-12-30 03:43:08
问题 I'm trying to do the following interface Updater { void update(String value); } void update(Collection<String> values, Updater updater) { update(values, updater::update, 0); } void update(Collection<String> values, Function<String, Void> fn, int ignored) { // some code } but I get this compiler error: "Cannot convert void to java.lang.Void" That means updater::update cannot be used as Function<String, Void> . Of course I can't write Function <String, void> and I don't want to change return