Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swissdict
julia
communitycreator

How to create and use SwissDict in Julia

Programming Bytes

Overview

The SwissDict is based on the SwissTable developed by Google. The SwissDict implements all the functionality of the AbstractDict protocol. The SwissDict performs better than the standard dictionary when the load factor is high.

Example

The below code demonstrates how to create and use the SwissDict in Julia.

using DataStructures

# Create a new SwissDict object which will return 10 as default va{String,Int}l 
dd = SwissDict{String,Int}()

dd["a"] = 1;
dd["b"] = 2;
dd["c"] = 3;
dd["d"] = 4;

# print the Dictionary
println(dd);

## print keys of dict
println("Keys = ", keys(dd))

## print values of dict
println("Values = ", values(dd))

## print values of dict
println("Value of the key a is  = ", get(dd, "a", 10));

## print values of dict
println("Value of the key e is  = ", get(dd, "e", 99));
Using the SwissDict

Explanation

  • Line 4: We create a new SwissDict object with the name dd.
  • Line 6–9: We'll add four key-value pairs to the dd object.
  • Line 15: We'll use the keys method to print all the keys present in the dd object.
  • Line 18: We'll use the values method to print all the values of the dd object.
  • Line 21: We'll use the get method to get the value for the key a. In the case that there is no entry for the key a, then the default value passed (third argument) to the get method is returned. The dd object has an entry for the key a, so the respective value 1 is returned.
  • Line 24: We'll use the get method to get the value for the key e . The dd object doesn't have an entry for the key e so the default value 99, which is passed as the third argument is returned.

RELATED TAGS

swissdict
julia
communitycreator
RELATED COURSES

View all Courses

Keep Exploring