L5. Necessary Java Programming Techniques

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
L2. Necessary Java Programming Techniques (Vector) Packages  Java.util import Java.util.*; Classes  Vector Interfaces  Enumeration Java API.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
Using Maps. A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable,
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1 The Collection Interface public interface Collection extends Iterable { boolean add(E e); boolean addAll(Collection c); void clear(); boolean contains(Object.
Dictionaries and Hash Tables Cmput Lecture 24 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Using Maps. 2 A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable,
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary A Java Interface Iterators Using the ADT Dictionary A Directory of Telephone.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Hashing CS 105. Hashing Slide 2 Hashing - Introduction In a dictionary, if it can be arranged such that the key is also the index to the array that stores.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
The Java Collections Framework Based on
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Set and Map IS 313, Skeletons  Useful coding patterns  Should understand how and why they work how to use  possible quiz material.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Maps Nick Mouriski.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Fundamentals Design Patterns Delegation Interface Immutable Marker Interface Proxy 10/14/2001 8:35 PM.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
Collections Dwight Deugo Nesa Matic
C19: Collection Classes (don’t forget to look at all the online code examples)
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Hashing By Emily Nelson. The Official Definition Using a hash function to turn some kind of data in relatively small integers or Strings The “hash code”
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Tables and Priority Queues
Fundamental of Java Programming
Efficiency of in Binary Trees
Using Maps.
Using Maps.
Introduction to Collections
Containers ב Java אליהו חלסצ'י תכנות מתקדם תרגול מספר 3
"He's off the map!" Topic 9 Maps
Programming in Java Lecture 11: ArrayList
Hashing as a Dictionary Implementation
Introduction to Collections
Part of the Collections Framework
Using Maps.
Maps "He's off the map!" -Stan (Mark Ruffalo) Eternal Sunshine of the Spotless Mind.
L2. Necessary Java Programming Techniques
"He's off the map!" - Eternal Sunshine of the Spotless Mind
Using Maps.
L2. Necessary Java Programming Techniques
Introduction to Collections
Collections Framework
Introduction to Collections
Hashing in java.util
Data Structures II AP Computer Science
Web Design & Development Lecture 6
Set and Map IS 313,
Presentation transcript:

L5. Necessary Java Programming Techniques (Hashtable) Continue …… Java API

Hierarchy Java API java.util Class Hashtable java.lang.Object java.util.Hashtable This class implements a hashtable, which maps keys to values. Any non-null object can be used as a key or as a value.

Constructor Methods Hashtable() Constructs a new, empty hashtable with a default initial capacity (11) and load factor, which is 0.75. Hashtable(int initialCapacity) Constructs a new, empty hashtable with the specified initial capacity and default load factor, which is 0.75. Hashtable(int initialCapacity, float loadFactor) Constructs a new, empty hashtable with the specified initial capacity and the specified load factor. Hashtable(Map t) Constructs a new hashtable with the same mappings as the given Map. public interface Map An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

A simple example To retrieve a number, use the following code: This example creates a hashtable of numbers. It uses the names of the numbers as keys: Hashtable numbers = new Hashtable(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3)); To retrieve a number, use the following code: Integer n = (Integer)numbers.get("two"); if (n != null) { System.out.println("two = " + n); }

Methods void clear() Clears this hashtable so that it contains no keys. boolean contains(Object value)    Tests if some key maps into the specified value in this hashtable. boolean isEmpty()    Tests if this hashtable maps no keys to values. boolean containsKey(Object key)    Tests if the specified object is a key in this hashtable.  boolean containsValue(Object value)    Returns true if this Hashtable maps one or more keys to this value.  Enumeration elements()    Returns an enumeration of the values in this hashtable. Enumeration keys()           Returns an enumeration of the keys in this hashtable.

Methods public interface Set extends Collection Object get(Object key)    Returns the value to which the specified key is mapped in this hashtable. Object put(Object key, Object value)     Maps the specified key to the specified value in this hashtable. Objec remove(Object key)     Removes the key (and its corresponding value) from this hashtable Set keySet()     Returns a Set view of the keys contained in this Hashtable public interface Set extends Collection A collection that contains no duplicate elements.

Demo

Exercise Make a java program that can store employees’ data (name and id) to a hash table and do the operations shown in the display below. HashTableApplet.java (optional) After finish Ex 1. please continue to make a java application program to the same problem above.