T
T
Tarasov Konstantin2015-03-26 01:59:59
Java
Tarasov Konstantin, 2015-03-26 01:59:59

New features of java 8, to use or not?

I know that java 8 is not so new anymore, but until this time I tried to figure it out with 7 and decided not to go any further. Today I looked at the list of new features in java 8 and ... somehow I doubted. Is it worth using them? In terms of how important it is to be able to use them. Because, for example, lambdas, although they shorten the code, are in some way "magic" that hides from us part of what is really happening.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vasily, 2015-03-26
@Applez

Everything is done for your convenience. You decide.

T
Tagir Valeev, 2015-03-26
@tagir_valeev

You can start small if you are afraid of streams. Let's say this code snippet in Java7:

Map<String, List<Item>> items = new HashMap<>();

public void addItem(String key, Item item) {
  List<Item> list = items.get(key);
  if(list == null) {
    list = new ArrayList<>();
    items.put(key, list);
  }
  list.add(item);
}

Something like this happens very often. In Java 8 it looks like this:
Map<String, List<Item>> items = new HashMap<>();

public void addItem(String key, Item item) {
  items.computeIfAbsent(key, k -> new ArrayList<>()).add(item);
}

Not only shorter, but also faster, because the key is looked up once instead of twice in the case of an insert. If you have ConcurrentMap, then the correct algorithm for calculating a non-existent key, which would minimally block the map and at the same time be executed no more than once, was generally non-trivial to write before. And now the same, in one line.

A
alk, 2015-03-26
@Alexey_Kutepov

If the boss allows you to use Java 8, then go ahead with the song =)

D
Denis Karakchiev, 2015-03-26
@Satori_Kanzo

I agree with Applez. Well, from myself ...
Firstly: about the "magic" - all programming is like that, if you do not know the principle of operation of microcontrollers and at what stage the electrical voltage develops into code.
Second, who are you to question the work of the Java language development team? =)

T
TachikomaGT, 2015-03-26
@TachikomaGT

In order for, say, the Stream API not to be “magic” for you, watch the video from Sergey Kuksenko at https://www.youtube.com/user/JUGRuVideo
For simple cases, the code can be executed a little slower, for complex ones it can be faster. In any case, you will get more expressiveness and, as a result, safety (the same applies to Optional).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question