HashSets and hash tables
- Lists allow for ordered elements, but searching them is very slow.
- Can speed up search tremendously if you dont care about ordering.
- Hash tables let you do this. Drawback is that you have no control over how elements are ordered.
- hashCode() computes integer (quickly) which corresponds to position in hash table.
- Independent of other objects in table.
HashSet Class
- Hashing can be used to implement several important data structures.
- Simplest of these is HashSet
- add elements with add(Object) method
- contains(Object) is redefined to first look for duplicates.
- if duplicate exists, Object is not added
- What determines a duplicate?
- careful here, must redefine both hashCode() and equals(Object)!
HashSet
- Look HashSet Example
- Play around with some additional methods.
/********************************************************************** * HashSet in Java Example * **********************************************************************/ import java.util.*; public class app { public static void main(String[] args) { HashSet<String> name = new HashSet<String>(); name.add("Mark"); name.add("tom"); name.add("jack"); name.add("july"); name.add("patrik"); Iterator<String> itr = name.iterator(); while(itr.hasNext()) System.out.println(itr.next()); } } /* Output: tom Mark july patrik jack */
- Java Simple Programs And Examples
- Java Example – Java Hello World Example
- Java Example – Math and Arithmetic Operators in Java
- Java Example – Variables and Types in Java
- Java Example – Scanner class and Getting User Input using Java
- Java Example – The If-Else If Statement, Nested If Statements, Logical Operators
- Java Example – Arrays in Java Example
- Java Example – ListIterator in Java Example
- Java Example – How to get current timestamp using Java
- Java Example – HashSet in Java with Example
- Java Example – How to read file in Java using BufferedReader
- Java Example – How to get Current Directory in Linux/Windows
- Java Example – Program to reverse an array or string
- Java Example – Sort String Array
- Java Example – Comparing two strings
- Java Example – String concatenation (join strings)
- Java Example – Java String Contains example
- Java Conversion
- Java Example – Convert String to int
- Java Example – Convert Date to String
- Java Example – Convert String to Character Array
- Java Example – Convert into string
- Java Example – Convert ArrayList to String Array
- Java Example – Convert Char Array To String
- Java Example – String Array To List
- Java Sorting algorithms & Techniques
- Java Example – Bubble Sort Algorithm
- Java Example – Insertion Sort Algorithm
- Java Example – Selection Sort Algorithm
- Java Example – Quick Sort Algorithm
- Java Example – Merge Sort Algorithm
- Java Handling Files
- Java I/O – Check If File Path Absolute or not
- Java I/O – Get file name and file path
- Java I/O – Get parent directory of the file or directory
- Java I/O – How to write to a file using BufferedWriter
- Java I/O – How to write to a file using FileOutputStream
- Java I/O – Check file permission and Set file permission
- Java I/O – Read File Using Java BufferedInputStream
- Java I/O – How to create a file in Java
:::java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have java installed, and more are created every day.
java was developed by Sun microsystem as an Object oriented language for general purpose business applications. The target of java is to write a program once and then run this program on multiple operating systems.The first publicly available version of java ( java 1.0) was released in ‘95. Sun Microsystems was acquired by the Oracle Corporation in 2k10. Oracle has now the steermanship for java . In 2k06 Sun started to make java accessible under the GNU General Public License (GPL). Oracle continued this plan called Open JDK.
Over time new improved versions of java have been released. The present version of java is java 1.9 which is also known as java 9.
java is defined by a specification and consists of a programming language, a compiler, core libraries and a runtime ( java virtual machine) The java runtime permit software developers to write program code in other languages than the java programming language which still runs on the java virtual machine. The java platform is usually associated with the java virtual machine and the java core libraries.
The java syntax is similar to C++. java is case-sensitive, e.g., variables called myValue andmyvalue are treated as different variables.
Basic topics Covered in java is
INDEX
Introduction of java
JDK vs JVM vs JVM
Java Data Types
Java Operators
Java Loop Control
Java Decision Making
Java Array
Java String
Class And Object
This Keyword in Java
Static Keyword in Java
Constructor in Java
Overloading in Java
Overriding in Java