Compare operation with collections - Create collections


Compare operation with collections - Create collections

1. Create List

Description JDK Guava GS Collections
Create empty list new ArrayList<>() Lists.newArrayList() FastList.newList()
Create list from values Arrays.asList("1", "2", "3") Lists.newArrayList("1", "2", "3") FastList.newListWith("1", "2", "3")
Create list with capacity = 100 new ArrayList<>(100) Lists.newArrayListWithCapacity(100) FastList.newList(100)
Create list from any collection new ArrayList<>(collection) Lists.newArrayList(collection) FastList.newList(collection)
Create list from any Iterable - Lists.newArrayList(iterable) FastList.newList(iterable)
Create list from Iterator - Lists.newArrayList(iterator) -
Create list from array Arrays.asList(array) Lists.newArrayList(array) FastList.newListWith(array)
Create list using factory - - FastList.newWithNValues(10, () -> "1")

Examples:

System.out.println("createList start");
// Create empty lists
List<String> emptyGuavaList = Lists.newArrayList(); // using guava
List<String> emptyJDKList = new ArrayList<>(); // using JDK
MutableList<String> emptyGSList = FastList.newList(); // using gs
 
// Create lists with 100 elements
List<String> exactly100GuavaList = Lists.newArrayListWithCapacity(100); // using guava
List<String> exactly100JDKList = new ArrayList<>(100); // using JDK
MutableList<String> exactly100GSList = FastList.newList(100); // using gs
 
// Create lists with about 100 elements
List<String> approx100GuavaList = Lists.newArrayListWithExpectedSize(100); // using guava
List<String> approx100JDKList = new ArrayList<>(115); // using JDK
MutableList<String> approx100GSList = FastList.newList(115); // using gs
 
// Create lists with some elements
List<String> withElementsGuavaList = Lists.newArrayList("alpha", "beta", "gamma"); // using guava
List<String> withElementsJDKList = Arrays.asList("alpha", "beta", "gamma"); // using JDK
MutableList<String> withElementsGSList = FastList.newListWith("alpha", "beta", "gamma"); // using gs
 
System.out.println(withElementsGuavaList);
System.out.println(withElementsJDKList);
System.out.println(withElementsGSList);
 
// Create lists from any Iterable interface (any collection)
Collection<String> collection = new HashSet<>(3);
collection.add("1");
collection.add("2");
collection.add("3");
 
List<String> fromIterableGuavaList = Lists.newArrayList(collection); // using guava
List<String> fromIterableJDKList = new ArrayList<>(collection); // using JDK
MutableList<String> fromIterableGSList = FastList.newList(collection); // using gs
 
System.out.println(fromIterableGuavaList);
System.out.println(fromIterableJDKList);
System.out.println(fromIterableGSList);
 
// Create lists from any Iterator
Iterator<String> iterator = collection.iterator();
List<String> fromIteratorGuavaList = Lists.newArrayList(iterator); // using guava
 
System.out.println(fromIteratorGuavaList);
 
// Create lists from any array
String[] array = {"4", "5", "6"};
List<String> fromArrayGuavaList = Lists.newArrayList(array); // using guava
List<String> fromArrayJDKList = Arrays.asList(array); // using JDK
MutableList<String> fromArrayGSList = FastList.newListWith(array); // using gs
 
System.out.println(fromArrayGuavaList);
System.out.println(fromArrayJDKList);
System.out.println(fromArrayGSList);
 
// Create lists using fabric
MutableList<String> fromFabricGSList = FastList.newWithNValues(10, () ->
    String.valueOf(Math.random())); // using gs
 
System.out.println(fromFabricGSList);
System.out.println("createList end");

2. Create Set

Description JDK Guava GS Collections
Create empty set new HashSet<>() Sets.newHashSet() UnifiedSet.newSet()
Create set from values new HashSet<>(Arrays.asList("alpha", "beta", "gamma")) Sets.newHashSet("alpha", "beta", "gamma") UnifiedSet.newSetWith("alpha", "beta", "gamma")
Create set from any collections new HashSet<>(collection) Sets.newHashSet(collection) UnifiedSet.newSet(collection)
Create set from any Iterable - Sets.newHashSet(iterable) UnifiedSet.newSet(iterable)
Create set from any Iterator - Sets.newHashSet(iterator) -
Create set from Array new HashSet<>(Arrays.asList(array)) Sets.newHashSet(array) UnifiedSet.newSetWith(array)

Examples:

import com.google.common.collect.Sets;
import com.gs.collections.impl.set.mutable.UnifiedSet;
 
import java.util.*;
 
public class HashSetCreation {
    public static void main(String[] args) {
        System.out.println("createHashSet start");
 
        // Create empty set
        Set<String> emptyGuavaSet = Sets.newHashSet(); // using guava
        Set<String> emptyJDKSet = new HashSet<>(); // using JDK
        Set<String> emptyGSSet = UnifiedSet.newSet(); // using gs
 
        // Create set with 100 elements
        Set<String> approx100GuavaSet = Sets.newHashSetWithExpectedSize(100); // using guava
        Set<String> approx100JDKSet = new HashSet<>(130); // using JDK
        Set<String> approx100GSSet = UnifiedSet.newSet(130); // using gs
 
        // Create set from some elements
        Set<String> withElementsGuavaSet = Sets.newHashSet("alpha", "beta", "gamma"); // using guava
        Set<String> withElementsJDKSet = new HashSet<>(Arrays.asList("alpha", "beta", "gamma")); // using JDK
        Set<String> withElementsGSSet = UnifiedSet.newSetWith("alpha", "beta", "gamma"); // using gs
 
        System.out.println(withElementsGuavaSet);
        System.out.println(withElementsJDKSet);
        System.out.println(withElementsGSSet);
 
        // Create set from any Iterable interface (any collection)
        Collection<String> iterableCollection = new ArrayList<>(3);
        iterableCollection.add("1");
        iterableCollection.add("2");
        iterableCollection.add("3");
 
        Set<String> fromIterableGuavaSet = Sets.newHashSet(iterableCollection); // using guava
        Set<String> fromIterableJDKSet = new HashSet<>(iterableCollection); // using JDK
        Set<String> fromIterableGSSet = UnifiedSet.newSet(iterableCollection); // using gs
 
        System.out.println(fromIterableGuavaSet);
        System.out.println(fromIterableJDKSet);
        System.out.println(fromIterableGSSet);
 
        /* Attention: JDK creates a set only from Collection, but guava and gs can create a set from Iterable and Collection */
 
        // Create set from any Iterator
        Iterator<String> iterator = iterableCollection.iterator();
        Set<String> fromIteratorGuavaSet = Sets.newHashSet(iterator); // using guava
        System.out.println(fromIteratorGuavaSet);
 
        // Create set from any array
        String[] array = {"4", "5", "6"};
        Set<String> fromArrayGuavaSet = Sets.newHashSet(array); // using guava
        Set<String> fromArrayJDKSet = new HashSet<>(Arrays.asList(array)); // using JDK
        Set<String> fromArrayGSSet = UnifiedSet.newSetWith(array); // using gs
 
        System.out.println(fromArrayGuavaSet);
        System.out.println(fromArrayJDKSet);
        System.out.println(fromArrayGSSet);
 
        System.out.println("createHashSet end");
    }
}

3. Create Map

Description JDK Guava GS Collections
Create empty map new HashMap<>() Maps.newHashMap() UnifiedMap.newMap()
Create map with capacity = 130 new HashMap<>(130) Maps.newHashMapWithExpectedSize(100) UnifiedMap.newMap(130)
Create map from other map new HashMap<>(map) Maps.newHashMap(map) UnifiedMap.newMap(map)
Create map from keys - - UnifiedMap.newWithKeysValues("1", "a", "2", "b")

Examples:

System.out.println("createHashMap start");
 
// Create empty maps
Map<String, String> emptyGuavaMap = Maps.newHashMap(); // using guava
Map<String, String> emptyJDKMap = new HashMap<>(); // using JDK
Map<String, String> emptyGSMap = UnifiedMap.newMap(); // using gs
 
// Create maps with about 100 elements
Map<String, String> approx100GuavaMap = Maps.newHashMapWithExpectedSize(100); // using guava
Map<String, String> approx100JDKMap = new HashMap<>(130); // using JDK
Map<String, String> approx100GSMap = UnifiedMap.newMap(130); // using gs
 
// Create maps from another map
Map<String, String> sourceMap = new HashMap<>(3);
sourceMap.put("k1", "v1");
sourceMap.put("k2", "v2");
Map<String, String> withMapGuava = Maps.newHashMap(sourceMap); // using guava
Map<String, String> withMapJDK = new HashMap<>(sourceMap); // using JDK
Map<String, String> withMapGS = UnifiedMap.newMap(sourceMap); // using gs
 
System.out.println(withMapGuava);
System.out.println(withMapJDK);
System.out.println(withMapGS);
 
// Create maps from keys
Map<String, String> withKeys = UnifiedMap.newWithKeysValues("1", "a", "2", "b");
System.out.println(withKeys);
 
System.out.println("createHashMap end");

Basic Programs