Java: Collection load factor or how create fixed size set or map

114 Views Asked by At

In my project i have special utilitarian methods .of() in classes MySet and MyMap to make work easier.

    public static <V> Set<V> of(V v1){
        Set<V> result = new LinkedHashSet<>();
        result.add(v1);
        return result;
    }

    public static <V> Set<V> of(V v1, V... other){
        Set<V> result = new LinkedHashSet<>();
        Collections.addAll(result, v1, other);
        return result;
    }

I want to use fixed capacity for this methods. My reason is a better performance and memory economy.

for first example:

        Set<V> result = new LinkedHashSet<>(1, 1);

for second:

        Set<V> result = new LinkedHashSet<>(other.length + 1, 1);

But as i understand 1f value of "load factor" variable means that limit to doubling buckets count = (capacity * loadfactor). In first example it would be 1 element...

I try to find some info about this question, but found nothing. What is the best way to create fixed sized Set? In my situation i always know how many elements i will need. And i don't want to be overhead, or allocate never used buckets.

0

There are 0 best solutions below