Showing posts with label Collections. Show all posts
Showing posts with label Collections. Show all posts

Saturday, August 15, 2020

Java 8 Convert All Map Keys and Values into List

Java 8 Example to Convert All Map Keys and Values into List

Use keyset().stream() and call collect() method to convert into List.

Use values().stream() and call collect() method to convert into List.

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Java8MapToListExample {

    public static void main(String[] args) {

        Map<Integer, String> numbers = new HashMap<>();

        numbers.put(10, "ten");
        numbers.put(20, "twenty");
        numbers.put(30, "thirty");
        numbers.put(40, "forty");
        numbers.put(50, "fifty");

        //java 8 - convert all keys to map
        List<Integer> keysList = numbers.keySet().stream().collect(Collectors.toList());

        System.out.println("Map keys List :");

        for (Integer integer : keysList) {
            System.out.println(integer);
        }

        // java 8 - convert all keys to map
        List<String> valuesList = numbers.values().stream().collect(Collectors.toList());

        System.out.println("Map values list :");

        for (String s : valuesList) {
            System.out.println(s);
        }

        System.out.println("removing odd even fruit id's as list : ");

        List<Integer> evenList = numbers.keySet().stream().filter(id -> id % 2 == 0).collect(Collectors.toList());
        evenList.forEach(id -> System.out.println(id));
    }
}

Output:


Map keys List :
50
20
41
10
30
Map values list :
fifty
twenty
forty one
ten
thirty
removing odd even fruit id's as list : 
50
20
10
30


Friday, August 9, 2019

Java ConcurrentSkipListSet Examples

1. Overview


In this tutorial, We'll guide to the new Java 1.6 version ConcurrentSkipListSet. This is part of the new Collection API and package java.util.concurrent.

This class is a direct subclass of AbstractSet and implements NavigableSet.

public class ConcurrentSkipListSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable


ConcurrentSkipListSet is mainly used to keep the elements sorted according to their natural ordering. This is the default behavior and no need to provide the comparator logic to it. If we provide the Comparator at the time of constructor creation then it uses our logic to sort the set. ConcurrentSkipListSet internally built using ConcurrentSkipListMap. This is equivalent to TreeSet and works well in a concurrent environment.

Monday, August 5, 2019

Java WeakHashMap Working Examples

1. Overview


In this tutorial, We'll be talking about WeakHashMap in java and it is placed in java.util package.

WeakHashMap is implemented based on Hashtable with weak keys. Map internally is built on Entry objects. When adding a key-value pair, it will be stored in the Entry object. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. So, once the key is deleted from the map it is eligible for Garbage Collection and we can not prevent from running GC on it. This class completely behaves differently than other Map implementations such as HashMap, LinkedHashMap, etc. And also it has efficiency parameters of initial capacity and load factor as same as HashMap.

java-weakhashmap