



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture was delivered by Dr. Ameet Shashank at B R Ambedkar National Institute of Technology. Its relate to Data Representation and Algorithm Design course. Its main points are: Elementary, Symbol, tables, DNS, URL, IP, Frequency, Counter, API, Associative, Array, Abstraction
Typology: Slides
1 / 6
This page cannot be seen from the preview
Don't miss anything!




2
key (^) value www.cs.princeton.edu URL IP address 128.112.136. www.princeton.edu 128.112.128. www.yale.edu 130.132.143. www.harvard.edu 128.103.060. www.simpsons.com 209.052.165. 3
Application Purpose Key Value Phone book Look up phone number Name Phone number Bank Process transaction Account number Transaction details File share Find song to download Name of song Computer ID Dictionary Look up word Word Definition Web search Find relevant documents Keyword List of documents Genomics Find markers DNA string Known positions DNS Find IP address given URL URL IP address Reverse DNS Find URL given IP address IP address URL Book index Find relevant pages Keyword List of pages Web cache Download Filename File contents Compiler Find properties of variable Variable name Value and type File system Find file on disk Filename Location on disk Routing table Route Internet packets Destination Best route 4
public static void main(String[] args) { ST<String, String> st = new ST<String, String>(); st.put("www.cs.princeton.edu", " 128. 112. 136. 11 "); st.put("www.princeton.edu", " 128. 112. 128. 15 "); st.put("www.yale.edu", " 130. 132. 143. 21 "); st.put("www.simpsons.com", " 209. 052. 165. 60 "); System.out.println(st.get("www.cs.princeton.edu")); System.out.println(st.get("www.harvardsucks.com")); System.out.println(st.get("www.simpsons.com")); } 128.112.136. null 209.052.165. st["www.simpsons.com"] = "209.052.165.60" st["www.simpsons.com"]
5
public static void main(String[] args) { ST<String, Integer> st = new ST<String, Integer>(); while (!StdIn.isEmpty()) { String key = StdIn.readString(); if (st.contains(key)) st.put(key, st.get(key) + 1 ); else st.put(key, 1 ); } for (String s : st) System.out.println(st.get(s) + " " + s); } calculate frequencies print results 6
13
14 public final class WrongPhoneNumber { private final int area; // 609 private final int exch; // 867 private final int ext; // 5309 public WrongPhoneNumber(int area, int exch, int ext) { this.area = area; this.exch = exch; this.ext = ext; } public boolean equals(WrongPhoneNumber b) { WrongPhoneNumber a = this; return (a.ext == b.ext) && (a.exch == b.exch) && (a.area == b.area); } }
broken implementation of equals (won't get called by java.util.HashMap) helps enforce immutability 15 public final class PhoneNumber { private final int area, exch, ext; public PhoneNumber(int area, int exch, int ext) { this.area = area; this.exch = exch; this.ext = ext; } public boolean equals(Object y) { if (y == this) return true; if (y == null) return false; if (y.getClass() != this.getClass()) return false; PhoneNumber a = this; PhoneNumber b = (PhoneNumber) y; return (a.area == b.area) && (a.exch == b.exch) return (a.area == b.area) && (a.ext == b.ext); } }
can't throw an exception must be Object, not PhoneNumber 16
stay tuned
18
www.princeton.edu 128.112.128. www.yale.edu 130.132.143. www.harvard.edu 128.103.060. null
key value next first 19
public class ListST<Key, Val> implements Iterable
public Val get(Key key) { for (Node x = first; x != null; x = x.next) if (key.equals(x.key)) return x.val; return null; } public void put(Key key, Val val) { for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) { x.val = val; return; } } first = new Node(key, val, first); } } if key is already present, replace value