CPS 100 2.1 APTs and structuring data/information l Is an element in an array, Where is an element in an array?  DIY: use a loop  Use Collections, several.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Generics, Lists, Interfaces
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
12-Jul-15 Lists in Java Part of the Collections Framework.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java's Collection Framework
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
CPS 108 : Spring From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea  Not really a standard language like C++  Arguably.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
CompSci 100 Prog Design and Analysis II Sept 7, 2010 Prof. Rodger 1CompSci 100 Fall 2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
(c) University of Washington14-1 CSC 143 Java Collections.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Compsci 06/101, Fall What will we do today? l Practice solving problems  Solving problems without a computer  Is this different than solving.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Collections. The Plan ● Why use collections? ● What collections are available? ● How are the collections different? ● Examples ● Practice.
Compsci 100, Fall Data and Information How and why do we organize data? Differences between data and information? What about knowledge?
Chapter 18 Java Collections Framework
Data structures Abstract data types Java classes for Data structures and ADTs.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Genome Revolution: COMPSCI 006G 2.1 Control in Java and Programming l The computer executes your program one statement at a time, from top to bottom within.
Compsci 06/101, Fall Vocabulary l What's the Spanish word for 'boy'? 'eat'?  How do you know? l What about the Turkish equivalents?  How do.
1 Interfaces in Java’s Collection Framework Rick Mercer.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
CompSci 100e 3.1 Plan for the week l Classes and Objects l Model-View-Controller l Maps and Hashing l Upcoming  Towards Algorithm analysis l APTs:  IsomorphicWords,
CPS 100, Spring Data and Information How and why do we organize data? Differences between data and information? What about knowledge?
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Written by: Dr. JJ Shepherd
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
CPS What's in Compsci 100? l Understanding tradeoffs: reasoning, analyzing, describing…  Algorithms  Data Structures  Programming  Design l.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
CompSci 100e 7.1 Plan for the week l Review:  Boggle  Coding standards and inheritance l Understand linked lists from the bottom up and top-down  As.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Building Java Programs Generics, hashing reading: 18.1.
CompSci Toward understanding data structures  What can be put in a TreeSet?  What can be sorted?  Where do we find this information?  How do.
Java: Base Types All information has a type or class designation
Data and Information How and why do we organize data? Differences between data and information? What about knowledge?
Java: Base Types All information has a type or class designation
Chapter 19 Java Data Structures
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CS2013 Lecture 7 John Hurley Cal State LA.
Java String Class String is a class
Introduction to Java Collection
Presentation transcript:

CPS APTs and structuring data/information l Is an element in an array, Where is an element in an array?  DIY: use a loop  Use Collections, several options  Tradeoffs? public boolean contains(String[] list, String target){ for(String s : list){ if (s.equals(target)) return true; } return false; } public boolean contains(String[] list, String target){ return Arrays.asList(list).contains(target); } public boolean contains(String[] list, String target){ return new HashSet (Arrays.asList(list)).contains(target); }

CPS APTs and Class/OO/Java tradeoffs l If you search a list of elements once, you must “touch” every element, so why worry about doing anything else?  Can you skip an element when searching? Why?  What about a sorted list of elements? l If you search repeatedly, it makes sense to organize data  Leverage or amortize the cost of the organization over several searches  Where do we store the organized data so that we can access it repeatedly?

CPS Class State and helper methods import java.util.*; public class SearchForStuff { private HashSet mySet; private boolean contains(String target){ return mySet.contains(target); } public int findMePlease(String[] data, String[] query){ mySet = new HashSet (Arrays.asList(data)); for(int k=0; k < query.length; k++){ String[] all = query[k].split(" "); l Instance variables: initialized once and repeatedly accessed  Where to call new?  How to use helper methods --- private or otherwise

CPS Data and Information How and why do we organize data? Differences between data and information?

CPS Where is ? traceroute traceroute to katahdin.cs.dartmouth.edu ( ), 64 hops max, 1 lou ( ) ms ( ) ms 3 tel1sp-roti.netcom.duke.edu ( ) ms 4 rlgh7600-gw-to-duke7600-gw.ncren.net ( ) ms 5 rlgh1-gw-to-rlgh7600-gw.ncren.net ( ) ms 6 rtp11-gw-to-rpop-oc48.ncren.net ( ) ms 7 rtp7600-gw-to-rtp11-gw-sec.ncren.net ( ) ms 8 dep7600-gw2-to-rtp7600-gw.ncren.net ( ) ms 9 internet2-to-dep7600-gw2.ncren.net ( ) ms 10 ge nycmng.abilene.ucaid.edu ( ) ms 11 so rtr.newy.net.internet2.edu ( ) ms 12 nox300gw1-vl-110-nox-internet2.nox.org ( ) ms 13 … 14 … 15 border.ropeferry1-crt.dartmouth.edu ( ) ms 16 katahdin.cs.dartmouth.edu ( ) ms

CPS John von Neumann “Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin.” “There's no sense in being precise when you don't even know what you're talking about. “ “There are two kinds of people in the world: Johnny von Neumann and the rest of us.” Eugene Wigner, Noble Physicist

CPS From Google to Maps l If we wanted to write a search engine we’d need to access lots of pages and keep lots of data  Given a word, on what pages does it appear?  This is a map of words->web pages l In general a map associates a key with a value  Look up the key in the map, get the value  Google: key is word/words, value is list of web pages  DNS: Domain name is key, IP address is a value l Interface issues  What if the key isn’t in the map, what value returned?  What if there’s already a value associated with a key?

CPS Interface at work: MapDemo.java l Key is a string, Value is # occurrences  Code below shows how Map interface/classes work What clues are there for prototype of map.get and map.put ?  What if a key is not in map, what value returned?  What kind of objects can be put in a map? for(String s : list) { s = s.toLowerCase(); Integer count = map.get(s); if (count == null){ map.put(s,1); } else{ map.put(s,count+1); }

CPS What can an Object do (to itself)? l  Look at java.lang.Object  What is this class? What is its purpose? l toString()  Used to print ( System.out.println ) an object  overriding toString() useful in new classes  String concatenation: String s = "value "+x;  Default is basically a pointer-value

CPS What else can you do to an Object? l equals(Object o)  Determines if guts of two objects are the same, must override, e.g., for using a.indexOf(o) in ArrayList a  Default is ==, pointer equality l hashCode()  Hashes object (guts) to value for efficient lookup l If you're implementing a new class, to play nice with others you must  Override equals and hashCode  Ensure that equal objects return same hashCode value

CPS Objects and values l Primitive variables are boxes  think memory location with value l Object variables are labels that are put on boxes String s = new String("genome"); String t = new String("genome"); if (s == t) {they label the same box} if (s.equals(t)) {contents of boxes the same} s t What's in the boxes? "genome" is in the boxes

CPS Objects, values, classes l For primitive types: int, char, double, boolean  Variables have names and are themselves boxes (metaphorically)  Two int variables assigned 17 are equal with == l For object types: String, ArrayList, others  Variables have names and are labels for boxes  If no box assigned, created, then label applied to null  Can assign label to existing box (via another label)  Can create new box using built-in new l Object types are references or pointers or labels to storage

CPS Anatomy of a class public class Foo { private int mySize; private String myName; public Foo(){ // what’s needed? } public int getSize(){ return mySize; } public double getArea(){ double x; x = Math.sqrt(mySize); return x; } l What values for vars (variables) and ivars (instance variables)?

CPS David Parnas "For much of my life, I have been a software voyeur, peeking furtively at other people's dirty code. Occasionally, I find a real jewel, a well-structured program written in a consistent style, free of kludges, developed so that each component is simple and organized, and designed so that the product is easy to change. "

CPS Parnas on re-invention "We must not forget that the wheel is reinvented so often because it is a very good idea; I've learned to worry more about the soundness of ideas that were invented only once. "

CPS David Parnas (entry in Wikipedia)Wikipedia l Module Design: Parnas wrote about the criteria for designing modules, in other words, the criteria for grouping functions together. This was a key predecessor to designing objects, and today's object-oriented design. l Social Responsibility: Parnas also took a key stand against the Strategic Defense Initiative (SDI) in the mid 1980s, arguing that it would be impossible to write an application that was free enough from errors to be safely deployed. Strategic Defense Initiative l Professionalism: Parnas became one of the first software engineers to earn a professional engineering license in Canada. He believes that software engineering is a branch of traditional engineering.

CPS Tomato and Tomato, how to code java.util.Collection and java.util.Collections  one is an interface add(), addAll(), remove(), removeAll(), clear() toArray(), size(), iterator()  one is a collection of static methods sort(), shuffle(), reverse(), max() frequency(), indexOfSubList () java.util.Arrays  Also a collection of static methods sort(), fill(), binarySearch(), asList()

CPS Kinds of maps, sets, lists, … l We’ll study performance of maps and sets (and lists)  Difference between array and ArrayList (LinkedList) size, performance, operations  Difference between TreeSet and HashSet (and Map) Performance of get and put Comparable v. Hashable, issues? l How to convert between array and List?  Arrays.asList(…)  list.toArray(…) Which is static method, what are parameters?