Set & Map

Maps and sets.

C++

Rust

Java


map<char, int> m {{'a', 5}, {'b', 3}, {'c', 1}};
set<int> s = {9, 8, 7, 6};

// inserts a new value for d
m['d'] = 7;

// prints the value for d
cout << m['d'] << endl;

// checks to see if 2 is in the set
cout << (s.find(2) == s.end()) << endl;


let mut m = HashMap::new();
let mut s = HashSet::new();

  // all initialization is done the same way
  m.insert('a', 5);
  m.insert('b', 3);
  m.insert('c', 1);
  m.insert('d', 7);

  s.insert(9);
  s.insert(8);
  s.insert(7);
  s.insert(6);

  // prints the value for d
  println!("{}", m.get(&'d').unwrap());

  // checks to see if 2 is in the set
  println!("{}", s.contains(&2));


Map<Character, Integer> m = new HashMap<>();
Set<Integer> s =
    new HashSet<>(Arrays.asList(9, 8, 7, 6));

// all initialization is done the same way
m.put('a', 5);
m.put('b', 3);
m.put('c', 1);
m.put('d', 7);

// prints the value for d
System.out.println(m.get('d'));

// checks to see if 2 is in the set
System.out.println(s.contains(2));



C++11 added brace-initialization to the language and greatly simplified the initialization of conainters. Rust does not supporty any type of initialization for HashMap or HashSet. Java does not provide any type of initialization for Map, but Arrays.asList() can be used for Set.