16 Ekim 2017 Pazartesi

LinkedBlockingQueue Sınıfı

Giriş
BlockingQueue arayüzünden kalıtır.

constructor
Metodun için şöyledir
public LinkedBlockingQueue() {
    this(Integer.MAX_VALUE);
}
constructor - int
Metodun imzası şöyledir
public LinkedBlockingQueue(int capacity)
clear metodu
Örnek ver

drainTo metodu
Örnek ver

put metodu
Örnek ver

take metodu
Örnek ver

10 Ekim 2017 Salı

Spliterators Sınıfı - SplitIterator Döndürür

Giriş
Bu sınıf SplitIterator döndürür. SplitIterator nesnesi StreamSupport nesnesine geçilerek, Iterator'den stream oluşturulabilir.

splitIterator metodu - long[]
Açıklaması şöyle.
The returned spliterator always reports the characteristics SIZED and SUBSIZED. The caller may provide additional characteristics for the spliterator to report. (For example, if it is known the array will not be further modified, specify IMMUTABLE; if the array data is considered to have an encounter order, specify ORDERED). The method Arrays.spliterator(long[], int, int) can often be used instead, which returns a spliterator that reports SIZED, SUBSIZED, IMMUTABLE, and ORDERED.
spliteratorUnknownSize metodu - iterator + characteristics
Örnek - iterator + ORDERED
Iterator'dan Stream elde etmek için şöyle yaparız.
Stream<E> stream = StreamSupport.stream(
  Spliterators.spliteratorUnknownSize(sourceIterator, Spliterator.ORDERED), false);
Örnek - iterator + ORDERED
Bir sürü Iterator'dan Stream elde etmek için şöyle yaparız
public static <E> Iterator<E>
  chainedIterator(Collection<? extends Iterator<? extends E>> iterators) {

    if(iterators.isEmpty()) return Collections.emptyIterator();
    return iterators.stream()
      .flatMap(it -> StreamSupport.stream(
         Spliterators.<E>spliteratorUnknownSize(it, Spliterator.ORDERED), false))
      .iterator();
}

8 Ekim 2017 Pazar

BiConsumer Arayüzü

Giriş
Şu satırı dahil ederiz.
import java.util.function.BiConsumer;
Açıklaması şöyle
Represents an operation that accepts two input arguments and returns no result.
T — the type of the first input argument
U — the type of the second input argument

Örnek
BiConsumer A nesnesinin B metodunu çağır şeklinde de kullanılabilir. Elimizde A arayüzü olsun. Ve bu arayüzün setMyField() metodu olsun.
Iterator<A> iteratorKey = ...;
Iterator<B> iteratorValue = ...;
BiConsumer<? super A, ? super B> consumer = A::setMyField;


while(iteratorKey.hasNext() && iteratorValue.hasNext()){
  consumer.accept(iteratorKey.next(),iteratorValue.next());
}

BiFunction Arayüzü - İki Collection Girdi İle Çok Kullanılır

Giriş
Şu satırı dahil ederiz.
import java.util.function.BiFunction;
Açıklaması şöyle
BiFunction represents a function that accepts two arguments and produces a result.
T — the type of the first argument to the function
U — the type of the second argument to the function
R — the type of the result of the function

Örnek
Şöyle yaparız
public class DistinctBiFunction implements 
  BiFunction<List<Integer>,List<Integer>,List<Integer>>{

  @Override
  public List<Integer> apply(List<Integer> list1, List<Integer> list2) {
    return Stream.of(list1, list2)
      .flatMap(List::stream)
        .distinct()
        .collect(Collectors.toList());
   }
}

BiFunction biFunction = new DistinctBiFunction();
List<Integer> list1 = ...
List<Integer> list2 = ...
System.out.println("Output for BiFunction : " + biFunction.apply(list1, list2));


Map.of metodu

of metodu
Java 9 ile geliyor.  

1. Immutable Map döndürür. 

2. Map.of() metodu en fazlan 10 elemanlık bir Map yaratabilir. Daha büyük bir şey yaratmak istersek Map.ofEntries() kullanılır

3. null key değer içeremez. 
Açıklaması şöyle. Bu metod ile Collections.emptyMap() benzer iş yaparlar. Fark olarak bu metod null key alamazken, diğeri alabilir.
The Map.of() and Map.ofEntries() static factory methods provide a convenient way to create immutable maps. The Map instances created by these methods have the following characteristics:
  • ...
  • They disallow null keys and values. Attempts to create them with null keys or values result in NullPointerException.
4. aynı key değerini içermez, yoksa IllegalArgumentException fırlatılır. Açıklaması şöyle
They reject duplicate keys at creation time. Duplicate keys passed to a static factory method result in IllegalArgumentException.
Şu kod hatalıdır.
Map<String, Integer> tempMap = Map.of(
        "London",    13,
        "London",    13, // !
);
Örnek
Immutable map istemiyorsak şöyle yaparız
Map<Integer, String> map = new HashMap<>( Map.of(1,"a", 2,"b", 3,"c") );
Örnek

Immutable map istemiyorsak şöyle yaparız.
Map<Integer, String> map = new HashMap<Integer, String>(
  Map.of(1, "value1", 2, "value2", 3, "value3"));
Örnek
Şöyle yaparız.
Map<Integer, String> map = Map.of(1, "value1", 2, "value2", 3, "value3", 4 );
Örnek
Şöyle yaparız.
 Map<String, String> srcMap = Map.of("A", "a", "B", "b", "C", "c");

4 Ekim 2017 Çarşamba

Java 9 Flow.Publisher Arayüzü

Giriş
Açıklaması şöyle.
Produces items for subscribers to consume. The only method is subscribe(Subscriber), whose purpose should be obvious.
Akış şöyle.
- Create a Publisher and a Subscriber.
- Subscribe the subscriber with Publisher::subscribe.
- The publisher creates a Subscription and calls Subscriber::onSubscription with it so the subscriber can store the subscription.
- At some point the subscriber calls Subscription::request to request a number of items.
- The publisher starts handing items to the subscriber by calling Subscriber::onNext. It will never publish more than the requested number of items.
- The publisher might at some point be depleted or run into trouble and call Subscriber::onComplete or Subscriber::onError, respectively.
- The subscriber might either continue to request more items every now and then or cut the connection by calling Subscription::cancel.

Map.Entry Arayüzü

comparingByValue metodu - Comparator
Java 8 ile geliyor. Verilen comparator'u kullanan bir comparator döner. Şöyle yaparız.
Map.Entry.comparingByValue(Comparator.comparing(Test::getNumber,
  Comparator.reverseOrder());