Cập nhật nội dung chi tiết về Java Dictionary Class Example mới nhất trên website Beiqthatgioi.com. Hy vọng thông tin trong bài viết sẽ đáp ứng được nhu cầu ngoài mong đợi của bạn, chúng tôi sẽ làm việc thường xuyên để cập nhật nội dung mới nhằm giúp bạn nhận được thông tin nhanh chóng và chính xác nhất.
key-value pair mapped in the dictionary.
2.
get()
The get() method takes the key as the argument and returns the value that is mapped to it. If no value is mapped with the given key, it simply returns null.
Syntax: public abstract V get(Object key)
Parameters: key – key whose mapped value we want
Return: value mapped with the argument key.
3.
elements
()
The elements() method is used to represent all the values present inside the Dictionary. It is usually used with loop statements as they can then represent one value at a time.
Syntax: public abstract Enumeration elements()
Return: value enumeration in the dictionary.
4.
keys
()
As the elements() method returns the enumerated values present inside the dictionary; similarly, the keys() method returns the enumerated keys present inside the dictionary.
Syntax: public abstract Enumeration keys()
Return: The key enumeration in the dictionary.
5.
isEmpty()
The isEmpty() method returns a boolean value, which is true if there are no key-value pairs present inside the Dictionary. If even any single key-value pair resides inside the dictionary, it returns false.
Syntax: public abstract boolean isEmpty()
Return: It returns true if there is no key-value relation in the dictionary; else false.
6.
remove(key
)
The remove() method takes the key as its argument, and it simply removes both the key and the value mapped with it from the dictionary.
Syntax: public abstract V remove(Object key)
Parameters: key: a key to be removed
Return: The key enumeration in the dictionary.
7.
size()
The size() method returns the total number of key-value pairs present inside the Dictionary.
Syntax: public abstract int size()
Return: It returns the no. of key-value pairs in the Dictionary.
The following program code is an example of using Dictionaries in Java.
import java.util.Dictionary; import java.util.Enumeration; import java.util.Hashtable; public class Dict { public static void main(String[] args) { Dictionary dictionary = new Hashtable(); dictionary.put("Apple", "A fruit"); dictionary.put("Ball", "A round shaped toy"); dictionary.put("Car", "A four wheeler vehicle designed to accomodate usually four people"); dictionary.put("Dog", "An animal with four legs and one tail"); System.out.println("nApple: " + dictionary.get("Apple")); System.out.println("Dog: " + dictionary.get("Dog")); System.out.println("Elephant: " + dictionary.get("Elephant")); System.out.println(); for (Enumeration i = dictionary.elements(); i.hasMoreElements();) { System.out.println("Values contained in Dictionary : " + i.nextElement()); } System.out.println(); for (Enumeration k = dictionary.keys(); k.hasMoreElements();) { System.out.println("Keys contianed in Dictionary : " + k.nextElement()); } System.out.println("nThe dictionary is empty? " + dictionary.isEmpty()); dictionary.remove("Dog"); System.out.println("nDog: " + dictionary.get("Dog")); System.out.println("nSize of Dictionary : " + dictionary.size()); } }See the output.
Finally, Java Dictionary Class Example Tutorial is over.
Recommended Posts
Nested class In Java Example
Java Type Casting Example
Java Interface Example
HashMap in Java Example
Java ArrayList Example
Java Scanner Class Example
Java Constructor Example
Beginning Java Programming With Hello World Example
The process of Java programming can be simplified in three steps:
Create the program by typing it into a text editor and saving it to a file – HelloWorld.java.
Compile it by typing “javac HelloWorld.java” in the terminal window.
Execute (or run) it by typing “java HelloWorld” in the terminal window.
Below given program is the simplest program of Java printing “Hello World” to the screen. Let us try to understand every bit of code step by step.
Output:
Hello, World
Class definition:This line uses the keyword class to declare that a new class is being defined.
class HelloWorldHelloWorld is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace { and the closing curly brace } .
main method: In Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args) public: So that JVM can execute the method from anywhere.static: Main method is to be called without object. The modifiers public and static can be written in either order.void: The main method doesn't return anything.main(): Name configured in the JVM.String[]: The main method accepts a single argument: an array of elements of type String.The next line of code is shown here. Notice that it occurs inside main( ). System.out.println("Hello, World");
This line outputs the string “Hello, World” followed by a new line on the screen. Output is actually accomplished by the built-in println( ) method. System is a predefined class that provides access to the system, and out is the variable of type output stream that is connected to the console.
/* This is a simple Java program. Call this file "HelloWorld.java". */Important Points :
The name of the class defined by the program is HelloWorld, which is same as name of file(HelloWorld.java). This is not a coincidence. In Java, all codes must reside inside a class and there is at most one public class which contain main() method.
By convention, the name of the main class(class which contain main method) should match the name of the file that holds the program.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using chúng tôi or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Chương Trình Java Đầu Tiên
Trong bài này, tôi sẽ hướng dẫn các bạn từng bước tạo một project trong Java thông qua chương trình HelloWorld. Đây là một chương trình rất phổ biến và tất cả các ngôn ngữ lập trình khác đều sử dụng trong bước đầu làm quen với ngôn ngữ đó. Chương trình này sẽ hiển thị ra màn hình Console dòng thông báo ” Hello World“. Trong bài này có sử dụng lệnh System.out.println(); để xuất thông báo ra màn hình và trong bài tiếp theo tôi sẽ nói rõ hơn về dòng lệnh này.
1. Tạo Project HelloWorld.
Các bạn thực hiện các bước sau.
Tạo Project.
Đầu tiên chúng ta mở Eclipse lên và chọnFile → New → Java Project. Hộp thoại New Java Project xuất hiện, các bạn điền vào tên Project là HelloWorld, các thông số còn lại các bạn để mặc định như hình bên dưới. Sau đó các bạn chọn Finish để kết thúc công đoạn tạo Project.
Tạo Package.
Các bạn chọn vào tên Project vừa tạo, sau đó nhấp chuột phải vào src và chọnNew → Package để tạo một Package mới. Chúng ta sẽ đặt tên cho Package này là helloworld. Nhấn Finish để kết thúc.
Tạo Class HelloWorld.
Để tạo một Class, chúng ta nhấp chuột phải vào Package helloworld vừa tạo, chọn New → Class và đặt tên Class này là HelloWorld, đồng thời bạn cũng chọn vào public static void main(String[] args) và nhấn Finish để kết thúc.
Sau khi đã hoàn thành được các bước trên, chúng ta sẽ tiến hành viết code cho chương trình HelloWorld.
2. Viết chương trình HelloWorld.
Trong Class HelloWorld, các bạn gõ vào đoạn code sau:
package helloworld; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }Lưu ý, khi các bạn lưu lại đoạn code này thì nên nhớ lưu với định dạng là UTF-8 bằng cách nhấp chuột phải vào tên Project chon Properties và thay đổi Text file encoding thành UTF-8.
Đến đây chúng ta đã hoàn tất việc tạo Project HelloWorld trong Java bằng công cụ Eclipse. Tiếp theo, chúng ta sẽ tiến hành biên dịch chương trình này và quan sát kết quả.
3. Phân tích chương trình HelloWorld.
Đầu tiên, chúng ta khai báo Class (lớp) theo cú pháp: public class TenClass (ở đây tên lớp của chúng ta là HelloWorld).
Tiếp theo, chúng ta sẽ khai báo phương thức main theo cú pháp: public static void main(String[] args). Đây là phương thức sẽ được thực hiện khi thực thi chương trình, phương thức này có tham số đầu vào (String[] args) là một mảng các chuỗi ( String).
Trong phương thức main() là câu lệnh cơ bản System.out.println("Hello World!"); dùng để xuất thông báo ” Hello World!” ra màn hình. Để gõ nhanh câu lệnh này bạn gõ chữ sysout, và nhấn tổ hợp phím Ctrl + Space chọn sysout - print to standard out và nhấn Enter để hoàn thành.
4. Biên dịch chương trình HelloWorld.
Để biên dịch chương trình, chúng ta sẽ nhấp chuột vào tên Project chọn Run As → Java Application và kết quả sẽ hiển thị trong cửa sổ Console như sau:
5. Lời kết.
Bài 3:Tự Học Java: Viết Chương Trình Đầu Tiên “Hello World” Bằng Java
Bắt đầu ngay nào!
Trước khi chúng ta đi vào chi tiết, trước tiên hãy bắt đầu với mã hóa và xem chương trình Hello World cơ bản trong Java được code như thế nào .
public class HelloWorldDemo { public static void main(String[] args) { System.out.println( "Hello World!" ); System.exit( 0 );Bây giờ hãy phân tích sâu cú pháp của chương trình.
public static void main(String[] args) public static void main(String []args)public static void main(String args[])public static void main(String... args)static public void main(String[] args) public static final void main(String[] args) final public static void main(String[] args)Dòng 3: System.out.println( “Hello World!” );
System : Đây là một lớp đã được định nghĩa trước trong gói java.lang ( nơi chứa các phương thức và biến hữu ích khác nhau)
out : Đây là một trường thành viên tĩnh của lớp PrintStream.
println: Đây là một phương thức của lớp PrintStream và được sử dụng để in đối số đã được thông qua bàn điều khiển tiêu chuẩn. Bạn cũng có thể sử dụng phương thức print () thay vì println ().
Dòng 4: chúng tôi (0);
Các phương thức java.lang. System. exit() được sử dụng để thoát khỏi chương trình hiện tại bằng cách chấm dứt Máy ảo Java (JVM) hiện đang thực thi. Phương thức này lấy mã trạng thái làm đầu vào thường là giá trị khác không. Nó biểu thị ra trong bất kỳ trường hợp nào chấm dứt bất thường xảy ra.
exit (0): Nó được sử dụng để chỉ sự chấm dứt thành công.exit(1) hoặc exit (-1) hoặc bất kỳ giá trị khác không: Nó được sử dụng để biểu thị chấm dứt không thành công.
Đó là tất cả về cú pháp chương trình. Bây giờ chúng ta hãy xem cách biên dịch Hello World trong chương trình Java.
javac HelloWorldDemo.javaLưu ý: Java phân biệt chữ hoa chữ thường. Do đó hãy đảm bảo rằng bạn nhập tên file theo đúng định dạng.
Java biên dịch chương trình như thế nào?
java HelloWorldDemoBạn đã thực hiện thành công chương trình đầu tiên của mình trong Java. Với hướng dẫn tự học java này hi vọng bạn có thể tạo ra được nhiều chương trình ứng dụng khác!
Giả sử, nếu bạn muốn biên dịch nhiều tệp Java cùng một lúc, thì bạn có thể sử dụng lệnh bên dưới:
Trong trường hợp bạn đang sử dụng IDE, bạn có thể bỏ qua tất cả rắc rối này và chỉ cần nhấn nút thực thi trong IDE của bạn để biên dịch và thực thi Hello World trong Chương trình Java.
Tác giả bài viết: Aptech Buôn Ma Thuột
Bạn đang đọc nội dung bài viết Java Dictionary Class Example trên website Beiqthatgioi.com. Hy vọng một phần nào đó những thông tin mà chúng tôi đã cung cấp là rất hữu ích với bạn. Nếu nội dung bài viết hay, ý nghĩa bạn hãy chia sẻ với bạn bè của mình và luôn theo dõi, ủng hộ chúng tôi để cập nhật những thông tin mới nhất. Chúc bạn một ngày tốt lành!