Data strutcures

Ace your homework & exams now with Quizwiz!

A few important features of HashSet are:

Implements Set Interface. The underlying data structure for HashSet is Hashtable. As it implements the Set Interface, duplicate values are not allowed. Objects that you insert in HashSet are not guaranteed to be inserted in the same order. Objects are inserted based on their hash code .NULL elements are allowed in HashSet

Important features of HashSet

Implements set data structure

Is hashmap an ordered collection ?

No

String s = "Java String"; System.out.println(s.charAt(s.toUpperCase().length()))

Runtime Exception

String s = "Java String Quiz"; System.out.println(s.substring(5,3));

Runtime Excption

What is String Immutability?

String objects are immutable. Immutable simply means unmodifiable or unchangeable

A good hash function may not prevent the collisions completely however it can reduce the number of collisions.

True

Does Hash Function reduce collision

True

clone() method in hashset is used

Used to create a shallow copy of the set

What is Hash Collision?

When two keys hash to the same slot. We call this situation a collision

Which of the following statements are true about String in java? a) String is case sensitive in Java b) all the options c) String is immutable in Java. d) String is thread-safe in Java.

b) all the options

Design a HashSet without using any built-in hash table libraries

class MyHashSet { private int buckets = 1000; private int itemsPerBucket = 1001; private boolean[][] table; /** Initialize your data structure here. */ public MyHashSet() { table = new boolean[buckets][]; } public int hash(int key) { return key % buckets; } public int pos(int key) { return key / buckets; } public void add(int key) { int hashkey = hash(key); if (table[hashkey] == null) { table[hashkey] = new boolean[itemsPerBucket]; } table[hashkey][pos(key)] = true; } public void remove(int key) { int hashkey = hash(key); if (table[hashkey] != null) table[hashkey][pos(key)] = false; } /** Returns true if this set did not already contain the specified element */ public boolean contains(int key) { int hashkey = hash(key); return table[hashkey] != null && table[hashkey][pos(key)]; } }

Hash tables are implemented where?

constant time lookup and insertion is required

String s1 = "abc"; String s2 = "abc"; System.out.println("s1 == s2 is:" + s1 == s2)

false

String s1 = "abc"; String s2 = new String("abc"); System.out.print(s1==s2);

false

Design a HashSet without using any built-in hash table libraries

import java.util.ArrayList; import java.util.List; class MyHashSet { private final List<Integer> hash; private int key_index = 0; public MyHashSet() { hash = new ArrayList<>(); } public void add(int key) { if (!contains(key)) hash.add(key); } public void remove(int key) { if (contains(key)) { hash.remove(key_index); } } public boolean contains(int key) { if (hash.contains(key)){ int i = 0; while (i < hash.size()) { if (hash.get(i) == key) { key_index = i; return true; } i++; } } return false; } }

tring s1 = "Java"; String s2 = "Java"; StringBuilder sb1 = new StringBuilder(); sb1.append("Ja").append("va"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); System.out.println(sb1.toString() == s1); System.out.println(sb1.toString().equals(s1));

true true false true

String s1 = "Cat"; String s2 = "Cat"; String s3 = new String("Cat"); System.out.print(s1 == s2); System.out.print(s1 == s3);

truefalse

String x = "xyz"; String y = "xyz"; x.concat(y); System.out.print(x);

xyz

Does Load factor effect the performance of HashSet operations?

yes

Java implementation of Hashtable chaining using doubly linked list

// Java implementation of Hashtable chaining // using doubly linked list import java.util.*; class DoublyLinkedListNode { // Declaration of Nodes DoublyLinkedListNode next, prev; int data; // constructor DoublyLinkedListNode(int data) { this.data = data; next = null; prev = null; } } class HashTableChainingDoublyLinkedList { // Declaration of Hash Table DoublyLinkedListNode[] hashTable; // stores the size of HashTable int size; // Constructor HashTableChainingDoublyLinkedList(int hashTableSize) { // Creating an empty Hash Table hashTable = new DoublyLinkedListNode[hashTableSize]; size = 0; } // Function to check if hash table is empty public boolean isEmpty() { return size == 0; } // Function to clear/delete all elements from Hash table public void clear() { // Capacity of Hash Table int len = hashTable.length; // Creating new empty Hash Table // of same initial capacity hashTable = new DoublyLinkedListNode[len]; size = 0; } // Function that returns size of Hash Table public int getSize() { return size; } // Function to insert a value/element public void insert(int value) { size++; // gets the position/index where the value should be // stored int position = hash(value); // creates a node for storing value DoublyLinkedListNode node = new DoublyLinkedListNode(value); DoublyLinkedListNode start = hashTable[position]; if (hashTable[position] == null) hashTable[position] = node; else { node.next = start; start.prev = node; hashTable[position] = node; } } // Function to remove an element public void remove(int value) { try { // gets the position where the value to // be deleted exists int position = hash(value); DoublyLinkedListNode start = hashTable[position]; DoublyLinkedListNode end = start; // if value is found at start if (start.data == value) { size--; if (start.next == null) { // removing the value hashTable[position] = null; return; } start = start.next; start.prev = null; hashTable[position] = start; return; } // traversing the list // until the value is found while (end.next != null && end.next.data != value) end = end.next; // if reached at the end without finding the // value if (end.next == null) { System.out.println("\nElement not found\n"); return; } size--; if (end.next.next == null) { end.next = null; return; } end.next.next.prev = end; end.next = end.next.next; hashTable[position] = start; } catch (Exception e) { System.out.println("\nElement not found\n"); } } // Definition of Hash function private int hash(Integer x) { int hashValue = x.hashCode(); hashValue %= hashTable.length; if (hashValue < 0) hashValue += hashTable.length; return hashValue; } // Function to print hash table public void printHashTable() { System.out.println(); for (int i = 0; i < hashTable.length; i++) { System.out.print("At " + i + ": "); DoublyLinkedListNode start = hashTable[i]; while (start != null) { System.out.print(start.data + " "); start = start.next; } System.out.println(); } } // Driver Code public static void main(String[] args) { HashTableChainingDoublyLinkedList hashTab = new HashTableChainingDoublyLinkedList(5); hashTab.insert(99); hashTab.insert(23); hashTab.insert(36); hashTab.insert(47); hashTab.insert(80); hashTab.printHashTable(); hashTab.insert(92); hashTab.insert(49); hashTab.printHashTable(); hashTab.remove(99); hashTab.printHashTable(); hashTab.clear(); hashTab.printHashTable(); System.out.println(hashTab.isEmpty()); } }

Given a hash table T with 25 slots that stores 2000 elements, the load factor α for T is __________

80

StringBuilder b = "java data structures"; b.append(4).deleteCharAt(3).delete(3, b.length() - 1);System.out.println(b);

Compilation Error

String Builder is immutable?

False

StringBuilder sb = new StringBuilder("Good Morning"); sb.insert(1, "Friend "); System.out.println(sb);

GFriend ood Moring

When the hash function generates the same index for multiple keys, there will be a conflict ________

Hash Collision

The LinkedHashMap Class is just like ____________ with an additional feature of maintaining anorder of elements inserted into it.

HashMap

What is a Hash Map?

HashMap is a part of the Java collection framework. It uses a technique called Hashing. It implements the map interface. It stores the data in the pair of Key and Value. It uses an array and LinkedList data structure internally for storing Key and Value. Properties of Hash Set


Related study sets

Beginners Scuba Diving Final Exam

View Set

Ch. 5-1: The Systems Development Lifecycle

View Set

prepU: fluid and electrolyte balance

View Set

Apprenons les couleurs. De quelle couleur est?

View Set

All But My Life Complete Overview

View Set

ГЛАВА 38 Патофізіологія нервової системи

View Set