#how to convert string to int in java example
Explore tagged Tumblr posts
tpointtechblog · 1 year ago
Text
Java Convert String to int | TpointTech
In Java, you can convert a Java String to an int using the Integer.parseInt() or Integer.valueOf() method.
Example:
String str = "123"; int num = Integer.parseInt(str); // Converts String to int System.out.println(num); //
Output:
123
int num = Integer.valueOf(str); // Also converts String to int
Both methods work similarly, but valueOf() returns an Integer object, while parseInt() returns a primitive int.
1 note · View note
izicodes · 2 years ago
Text
Dynamically vs Statically-Typed Programming Languages
Tumblr media
Hiya!🌍�� I know I haven't done one of these posts in a while but now I came up with a new topic to talk about!
Today, we're going to dive into the world of programming languages and explore the differences between dynamically-typed and statically-typed ones. I actually got the idea from explaining the whole difference between languages such as C# and Java to Lua and Python! Also just wanted to talk about how various languages handle data types~! So, buckle up, and let's get started~! 🚀
Tumblr media
The Main Difference
It all lies in how they handle data types:
In a dynamically-typed language, the type of a variable is determined at runtime, which means you don't have to specify the type explicitly when declaring a variable.
In a statically-typed language, the type of a variable is determined at compile-time, and you must declare the type explicitly when defining a variable.
Tumblr media
Example Code
Not getting the picture of what I'm talking about? No worries, let's take a look at some code examples to illustrate the difference. I'll use my beloved Lua (a dynamically-typed language) and C# (a statically-typed language)~!
Lua
Tumblr media
C#
Tumblr media
In the Lua example, we can see that we don't need to specify the data type of the variable x. We can even change its type later in the code and it would still work!
In the C# example, we must specify the data type of x when declaring it, and attempting to change its type later will result in a compile-time error. Remember though, you can convert an int to string in C# via 'Convert.ToString()'!
Tumblr media
Recap!
In dynamically-typed language, the type of a variable is determined at runtime.
Lua, Python, and JavaScript are programming languages that are dynamically typed.
In a statically-typed language, the type of a variable is determined at compile-time.
C#, Java, and Go are programming languages that are statically typed.
Obviously, there is more to know about each type as dynamically-typed and statically-typed languages each have their advantages and disadvantages - but I wanted to focus more on the data type declaration part~!
Here are some further reading pages:
Dynamic Typing vs Static Typing - LINK
Advantages and Disadvantages of Dynamic and Static Typing - LINK
Tumblr media
That's all, thanks for reading, and hope you learned something new! Happy coding, folks~! 🌟💻🚀
Tumblr media
83 notes · View notes
nectoy7 · 7 months ago
Text
Understanding Java Data Types: A Comprehensive Guide
Java, one of the most widely used programming languages, is known for its portability, security, and rich set of features. At the core of Java programming are data types, which define the nature of data that can be stored and manipulated within a program. Understanding data types is crucial for effective programming, as they determine how data is stored, how much memory it occupies, and the operations that can be performed on that data.
What are Data Types?
In programming, data types specify the type of data that a variable can hold. They provide a way to classify data into different categories based on their characteristics and operations. Java categorizes data types into two main groups:
1. Primitive Data Types
2. Reference Data Types
Why Use Data Types?
1. Memory Management: Different data types require different amounts of memory. By choosing the appropriate data type, you can optimize memory usage, which is particularly important in resource-constrained environments.
2. Type Safety: Using data types helps catch errors at compile time, reducing runtime errors. Java is a statically typed language, meaning that type checks are performed during compilation.
3. Code Clarity: Specifying data types makes the code more readable and understandable. It allows other developers (or your future self) to quickly grasp the intended use of variables.
4. Performance Optimization: Certain data types can enhance performance, especially when dealing with large datasets or intensive calculations. For example, using int instead of long can speed up operations when the range of int is sufficient.
5. Defining Operations: Different data types support different operations. For example, you cannot perform mathematical operations on a String data type without converting it to a numeric type.
When and Where to Use Data Types?
1. Choosing Primitive Data Types:
Use int when you need a whole number without a decimal, such as counting items.
Use double for fractional numbers where precision is essential, like financial calculations.
Use char when you need to store a single character, such as a letter or symbol.
Use boolean when you need to represent true/false conditions, like in conditional statements.
2. Choosing Reference Data Types:
Use String for any textual data, such as names, messages, or file paths.
Use Arrays when you need to store multiple values of the same type, such as a list of scores or names.
Use Custom Classes to represent complex data structures that include multiple properties and behaviors. For example, a Car class can encapsulate attributes like model, year, and methods for actions like starting or stopping the car.
1. Primitive Data Types
Primitive data types are the most basic data types built into the Java language. They serve as the building blocks for data manipulation in Java. There are eight primitive data types:
Examples of Primitive Data Types
1. Byte Example
byte age = 25; System.out.println(“Age: ” + age);
2. Short Example
short temperature = -5; System.out.println(“Temperature: ” + temperature);
3. Int Example
int population = 1000000; System.out.println(“Population: ” + population);
4. Long Example
long distanceToMoon = 384400000L; // in meters System.out.println(“Distance to Moon: ” + distanceToMoon);
5. Float Example
float pi = 3.14f; System.out.println(“Value of Pi: ” + pi);
6. Double Example
double gravitationalConstant = 9.81; // m/s^2 System.out.println(“Gravitational Constant: ” + gravitationalConstant);
7. Char Example
char initial = ‘J’; System.out.println(“Initial: ” + initial);
8. Boolean Example
boolean isJavaFun = true; System.out.println(“Is Java Fun? ” + isJavaFun);
2. Reference Data Types
Reference data types, unlike primitive data types, refer to objects and are created using classes. Reference data types are not defined by a fixed size; they can store complex data structures such as arrays, strings, and user-defined classes. The most common reference data types include:
Strings: A sequence of characters.
Arrays: A collection of similar data types.
Classes: User-defined data types.
Examples of Reference Data Types
1. String Example
String greeting = “Hello, World!”; System.out.println(greeting);
2. Array Example
int[] numbers = {1, 2, 3, 4, 5}; System.out.println(“First Number: ” + numbers[0]);
3. Class Example
class Car {     String model;     int year;
    Car(String m, int y) {         model = m;         year = y;     } }
public class Main {     public static void main(String[] args) {         Car car1 = new Car(“Toyota”, 2020);         System.out.println(“Car Model: ” + car1.model + “, Year: ” + car1.year);     } }
Type Conversion
In Java, type conversion refers to converting a variable from one data type to another. This can happen in two ways:
1. Widening Conversion: Automatically converting a smaller data type to a larger data type (e.g., int to long). This is done implicitly by the Java compiler.
int num = 100; long longNum = num; // Widening conversion
2. Narrowing Conversion: Manually converting a larger data type to a smaller data type (e.g., double to int). This requires explicit casting.
double decimalNum = 9.99; int intNum = (int) decimalNum; // Narrowing conversion
Conclusion
Understanding data types in Java is fundamental for effective programming. It not only helps in managing memory but also enables programmers to manipulate data efficiently. Java’s robust type system, consisting of both primitive and reference data types, provides flexibility and efficiency in application development. By carefully selecting data types, developers can optimize performance, ensure type safety, and maintain code clarity.
By mastering data types, you’ll greatly enhance your ability to write efficient, reliable, and maintainable Java programs, setting a strong foundation for your journey as a Java developer.
3 notes · View notes
nel-world · 3 days ago
Text
j
Swing is not thread-safe. Updating UI components from background threads (not the Event Dispatch Thread) causes race conditions, freezes, or crashes.
Use SwingUtilities.invokeLater() or SwingWorker to handle background tasks safely.
Component Overlap or Z-order Issues Components might overlap or not render correctly if layout and repainting aren’t managed properly.
revalidate() and repaint() are often needed after dynamic UI changes.
Scaling and DPI Conflicts On high-DPI displays, Swing apps can look blurry or improperly scaled if not configured.
Java 9+ supports HiDPI better, but older setups require workarounds.
Architecture Conflicts Mixing UI logic with business logic leads to spaghetti code and maintenance problems.
Not following patterns like MVC or separating concerns can make the design fragile.
Event Handling Conflicts Multiple listeners acting on the same component or event can cause logic errors.
Improper handling of key bindings or focus can result in unresponsive controls. // Updating a JTable in Java Swing can be done in a few different ways Using a DefaultTableModel (most common way)
Access the model:DefaultTableModel model = (DefaultTableModel) table.getModel(); Refreshing the UI If you're updating the model directly, the JTable usually updates automatically. But if needed:
java model.fireTableDataChanged();
// If you update the JTable (or any Swing component) from a non-EDT thread, you risk:
UI glitches
Random exceptions
Unpredictable behavior
The Fix: Use SwingUtilities.invokeLater() // Always wrap the JTable in a JScrollPane to handle large datasets.
Use BorderLayout.CENTER to make it fill the frame.
This design makes JTable the main UI element—perfect for apps like:
Inventory systems
Admin dashboards
// Custom Cell Rendering (How Data is Displayed) To change how a cell looks, implement a custom TableCellRenderer.
// Make Only Certain Columns Editable Override isCellEditable() in your model:
java Copy Edit DefaultTableModel model = new DefaultTableModel(data, columnNames) { @Override public boolean isCellEditable(int row, int column) {
//
Custom Cell Editors (How Data is Edited) To control how a user edits a cell, use a TableCellEditor.
Example: Use a combo box editor for a column java
String[] roles = {"Developer", "Designer", "Manager"}; JComboBox comboBox = new JComboBox<>(roles);
table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor // Format Displayed Values You can convert raw data (like timestamps, enums, booleans) into human-readable text using renderers or by overriding getValueAt() in a custom TableModel.
//
GridLayout Divides space into equal-sized rows and columns.
java
BoxLayout Aligns components vertically or horizontally.
GridBagLayout Most flexible, but also the most complex.
Allows fine-grained control over row/column span, alignment, padding. //
Optimized event-driven programming for efficient user interactions and system performance.
Implemented MVC architecture to ensure scalability and maintainability of Java Swing applications.
Enhanced multithreading in Swing applications to improve responsiveness using SwingWorker.
Debugged and resolved UI rendering issues, ensuring cross-platform compatibility.
Worked with Look and Feel (LAF) customization for a modern and branded UI experience.
//
ava Swing Application Works JFrame (Main Window) – The base container that holds all UI components.
JPanel (Layout Container) – Used to organize components inside the frame.
Swing Components – Buttons (JButton), labels (JLabel), text fields (JTextField), tables (JTable), etc.
Event Handling – Listeners (like ActionListener) handle user interactions.
Threading (SwingWorker) – Ensures UI remains responsive during background tasks.
Example Use Cases Point of Sale (POS) Systems – Cashier interfaces for processing transactions.
Inventory Management – Applications for tracking stock levels.
Data Entry Forms – GUI forms for database input and management.
Media Players – Applications for playing audio/video with Swing UI.\
JFrame Main application window JPanel Container for organizing UI elements JButton Clickable button JLabel Display text or images JTextField Single-line input field JTextArea Multi-line text input JTable Displays tabular data JMenuBar Menu bar with dropdown menus JList List of selectable items
.. //
Use of Modern Look and Feel (LAF) FlatLaf – A modern, flat UI theme for Swing that provides a better-looking UI.
Improved Concurrency with CompletableFuture Handles long-running tasks without freezing the UI.
Example:
java
CompletableFuture.supplyAsync(() -> fetchData()) .thenAccept(data -> SwingUtilities.invokeLater(() -> label.setText(data)));
// Use a Layout Manager Java Swing provides various layout managers like:
BorderLayout – Divides the window into 5 regions (North, South, East, West, Center).
GridBagLayout – Flexible and customizable grid-based layout.
BoxLayout – Arranges components in a single row or column.
GroupLayout – Best for complex resizable designs (used in NetBeans GUI Builder).
Use JScrollPane to make JTable scrollable ✔ Use DefaultTableModel for editing rows ✔ Add event listeners to detect row selection ✔ Integrate with a database using JDBC
Performance Issues in JTable & How to Optimize When dealing with large datasets in JTable, performance can degrade due to factors like slow rendering, inefficient data models, excessive event handling, Large Dataset Causes UI Lag Issue: If the table has thousands of rows, JTable may slow down because it loads all rows at once.
Solution: Use pagination or lazy loading instead of loading everything upfront.
✅ Example: Paginated JTable (Loading 100 Rows at a Time)
java Copy Edit int pageSize = 100; // Load 100 rows at a time int currentPage = 0; List data = fetchDataFromDatabase(currentPage * pageSize, pageSize); // Load only a subset
DefaultTableModel model = (DefaultTableModel) table.getModel(); for (Object[] row : data) {
//
Slow Rendering Due to Default Renderer Issue: The default cell renderer calls Component.setOpaque(false), causing unnecessary painting.
Solution: Use a custom renderer with setOpaque(true).
✅ Example: Custom Fast Renderer
java Copy Edit class FastRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); label.setOpaque(true); // Prevents repainting issues
;;
Frequent Repainting & Event Listeners Cause Overhead Issue: JTable repaints everything after every update, even when unnecessary.
Solution: Temporarily disable auto updates, batch updates, then re-enable.
✅ Example: Batch Update with Table Locking
java Copy Edit DefaultTableModel model = (DefaultTableModel) table.getModel(); model.setRowCount(0); // Clear table without repainting table.setAutoCreateColumnsFromModel(false); // Avoid unnecessary updates
// Batch insert rows for (int i = 0; i < 1000; i++) { model.addRow(new Object[]{"ID " + i, "Name " + i, i + 20}); }
table.setAutoCreateColumnsFromModel(true); //
Using DefaultTableModel for Large Data Handling Issue: DefaultTableModel is inefficient for large datasets because it stores all data in memory.
Solution: Use a custom TableModel that fetches data dynamically.
✅ Example: Custom Lazy Loading Table Model
java Copy Edit class CustomTableModel extends AbstractTableModel { private final int rowCount = 1000000; // Simulating large dataset@Override public int getRowCount() { return rowCount;
Slow Sorting & Filtering Issue: Default sorting can be slow for large datasets.
Solution: Use RowSorter with custom sorting instead of sorting all rows at once.
✅ Example: Optimized Sorting
java Copy Edit TableRowSorter sorter = new TableRowSorter<>(table.getModel()); table.setRowSorter(sorter);
Use pagination or lazy loading for large datasets. ✅ Optimize cell rendering with setOpaque(true). ✅ Batch updates & disable auto updates temporarily. ✅ Use a custom TableModel instead of DefaultTableModel. ✅ Implement RowSorter for efficient sorting.
0 notes
learnwithcadl123 · 6 months ago
Text
Tumblr media
The Basics of Java: Understanding Variables and Data Types
Java, one of the most widely-used programming languages, is the foundation for a range of applications from web development to mobile applications, especially on the Android platform. For those new to Java, understanding its core concepts like variables and data types is essential to grasp how the language operates. These elements form the building blocks of Java programming and will set you up for success as you learn more advanced topics.
To gain a deeper understanding and hands-on experience, consider joining CADL’s Java Programming Course. Our course offers a structured approach to Java, covering everything from the basics to advanced topics.
1. What Are Variables?
In Java, a variable is a named location in memory used to store data. Variables act as containers for storing information that can be referenced and manipulated throughout a program. Whenever you need to work with data (numbers, text, etc.), you will need to assign it to a variable.
Declaring Variables in Java
Declaring a variable in Java involves specifying its data type, followed by the variable name, and optionally initializing it with a value. Here’s the basic syntax:
java
Copy code
dataType variableName = value;
For example:
java
Copy code
int age = 25;
String name = "John";
In the first line, an integer (int) variable called age is declared and initialized with the value 25. In the second line, a String variable named name is declared and initialized with the text "John".
2. Types of Variables in Java
Java has three primary types of variables:
Local Variables: Defined within methods or blocks and accessible only within that method or block.
Instance Variables: Also known as non-static fields, they are defined within a class but outside any method. They are unique to each instance of a class.
Static Variables: Also known as class variables, they are shared among all instances of a class. Defined with the static keyword.
3. Understanding Data Types in Java
Data types specify the type of data a variable can hold. Java has two main categories of data types: Primitive Data Types and Non-Primitive Data Types.
Primitive Data Types
Primitive data types are the most basic data types and are predefined by Java. They include:
int: Stores integer values (e.g., int age = 30;).
double: Stores decimal numbers (e.g., double price = 9.99;).
char: Stores single characters (e.g., char grade = 'A';).
boolean: Stores true or false values (e.g., boolean isJavaFun = true;).
Java also includes byte, short, long, and float data types, each used for specific types of numeric values.
Examples:
java
Copy code
int count = 10;      // integer type
double height = 5.9; // double (floating-point) type
char letter = 'A';   // character type
boolean isActive = true; // boolean type
Each primitive type has a specific range and size in memory. For instance, int values range from -2,147,483,648 to 2,147,483,647, while double values allow for larger decimal numbers.
Non-Primitive Data Types
Non-primitive data types are created by the programmer and can include multiple values and methods. The most common non-primitive data types are Strings, Arrays, and Classes.
String: A sequence of characters (e.g., String message = "Hello, World!";).
Array: Used to store multiple values of the same type in a single variable (e.g., int[] numbers = {1, 2, 3, 4};).
Class: Used to define custom data types in Java, which can hold both variables and methods.
4. Variable Naming Conventions
Naming conventions help make code more readable and maintainable. In Java:
Variable names should be meaningful and descriptive.
Use camelCase for variable names (e.g., userName, itemCount).
Avoid starting variable names with numbers or using special characters except _.
Following these conventions ensures your code is more understandable, especially as projects grow.
5. Java Type Casting
Type casting is the process of converting one data type to another. Java allows two types of type casting: Implicit Casting and Explicit Casting.
Implicit Casting (Widening Casting)
Java automatically converts a smaller data type to a larger one without data loss. For example, converting an int to a double:
java
Copy code
int num = 10;
double doubleNum = num; // Implicit casting from int to double
Explicit Casting (Narrowing Casting)
When converting a larger data type to a smaller one, you must perform explicit casting. This process may result in data loss, so proceed with caution.
java
Copy code
double price = 19.99;
int discountedPrice = (int) price; // Explicit casting from double to int
6. Common Data Type Examples in Java
Let’s look at some examples to see how variables and data types work together in Java:
Example 1: Working with Different Data Types
java
Copy code
public class Main {
    public static void main(String[] args) {
        int quantity = 5;              // integer variable
        double pricePerItem = 15.50;   // double variable
        String itemName = "Pen";       // String variable
        boolean isInStock = true;      // boolean variable
        double totalPrice = quantity * pricePerItem;
        System.out.println("Item: " + itemName);
        System.out.println("Total Price: " + totalPrice);
        System.out.println("In Stock: " + isInStock);
    }
}
Example 2: Using Type Casting
java
Copy code
public class Main {
    public static void main(String[] args) {
        double num = 9.78;
        int data = (int) num;  // Explicit casting: double to int
        System.out.println("Original Number (double): " + num);
        System.out.println("Converted Number (int): " + data);
    }
}
In the second example, the decimal part of num is lost because int can only store whole numbers. Type casting helps control data representation but requires careful consideration.
7. Final Thoughts on Variables and Data Types in Java
Understanding variables and data types in Java is crucial for writing efficient, error-free code. Java’s versatility in handling data types allows developers to manage various data with ease, whether you're dealing with text, numbers, or more complex data structures. Starting with these basic concepts will give you the foundation needed to explore more advanced programming topics, such as control flow, object-oriented programming, and data structures.
Mastering the fundamentals of Java is easier with structured guidance, so why not join CADL’s Java Programming Course? This course provides hands-on lessons, practical examples, and insights into core Java concepts, setting you on the right path to becoming a skilled Java developer.
0 notes
vatt-world · 8 months ago
Text
hi
fizzbuzz reverse string implement stack
convert integer to roman numeral longest palindrome substring
design hashset
Java group by sort – multiple comparators example https://leetcode.com/discuss/interview-question/848202/employee-implementation-online-assessment-hackerrank-how-to-solve
SELECT SUBQUERY1.name FROM (SELECT ID,name, RIGHT(name, 3) AS ExtractString FROM students where marks > 75 ) SUBQUERY1 order by SUBQUERY1.ExtractString ,SUBQUERY1.ID asc ;
SELECT *
FROM CITY
WHERECOUNTRYCODE = 'USA' AND POPULATION > 100000;
Regards
Write a simple lambda in Java to transpose a list of strings long value to a list of long reversed. Input: [“1”,”2”,”3”,”4”,”5”] output: [5,4,3,2,1]    2. Write a Java Program to count the number of words in a string using HashMap.
Sample String str = "Am I A Doing the the coding exercise Am"   Data model for the next 3 questions:
Write a simple lambda in Java to transpose a list of strings long value to a list of long reversed. Input: [“1”,”2”,”3”,”4”,”5”] output: [5,4,3,2,1]    2. Write a Java Program to count the number of words in a string using HashMap.
Sample String str = "Am I A Doing the the coding exercise Am"   Data model for the next 3 questions:
Customer :
CustomerId : int Name : varchar(255)
Account :
AccountId : int CustomerId : int AccountNumber : varchar(255) Balance : int
Transactions :  Transactionid : int AccountId: int TransTimestamp : numeric(19,0) Description : varchar(255) Amount(numeric(19,4))   3. Write a select query to find the most recent 10 transactions.  4. Write a select query, which, given an customer id, returns all the transactions of that customer.  5. What indexes should be created for the above to run efficiently? CustomerId, AccountId  6. Write a program to sort and ArrayList.
Regards
0 notes
fullstackme · 5 years ago
Text
How To Read Lined JSON files with Java 8
Came across this, seeming trivial at a glance, task of parsing a relatively well-formatted data feed just recently. Sure, you may say, what could be easier than parsing a JSON format given that there are plenty of tools for that, especially for Java? Well, sorry, not exactly JSON... In effect, compared to other unstructured data sources I previously worked with, this feed used a lined JSON format (i.e. IJSON). Example:
{“id”: “us-cia-world-leaders.bc0...”, “type”: “individual”, ...} {“id”: “us-cia-world-leaders.924...”, “type”: “entity”, ...} {...}
Even though this format is widely used, mainstream JSON parsers such as Jackson cannot handle this structure since it’s not a valid JSON file. Looks like we have a little problem here?
Tackling IJSON with Java
A quick solution is to simply read the lined JSON file line by line and transform each line to a POJO entry. Combined with streamed input readers, the lined JSON format appeared to be more efficacious than the “classic” JSON, merely because we no longer need to preload the entire structure in memory and then transform it. With 30Mb+ files, the performance benefits are evidently noticeable.
The below code snippet illustrates how this can be achieved:
import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.stream.Stream; /** * Simple streamed reader to go through Lined JSON files, convert each line to POJO entry * and perform a specified action on every row. * @author Vladimir Salin */ public class LineBasedJsonReader {    private static final Logger log = LoggerFactory.getLogger(LineBasedJsonReader.class);    private ObjectMapper objectMapper;    public LineBasedJsonReader(ObjectMapper objectMapper) {        this.objectMapper = objectMapper;    }    /**     * Parses a provided input in a streamed way. Converts each line in it     * (which is supposed to be a JSON) to a specified POJO class     * and performs an action provided as a Java 8 Consumer.     *     * @param stream lined JSON input     * @param entryClass POJO class to convert JSON to     * @param consumer action to perform on each entry     * @return number of rows read     */    public int parseAsStream(final InputStream stream, final Class entryClass, final Consumer<? super Object> consumer) {        long start = System.currentTimeMillis();        final AtomicInteger total = new AtomicInteger(0);        final AtomicInteger failed = new AtomicInteger(0);        try (Stream<String> lines = new BufferedReader(new InputStreamReader(stream)).lines()) {            lines                    .map(line -> {                        try {                            total.incrementAndGet();                            return objectMapper.readerFor(entryClass).readValue(line);                        } catch (IOException e) {                            log.error("Failed to parse a line {}. Reason: {}", total.get()-1, e.getMessage());                            log.debug("Stacktrace: ", e);                            failed.incrementAndGet();                            return null;                        }                    })                    .filter(Objects::nonNull)                    .forEach(consumer);        }        long took = System.currentTimeMillis() - start;        log.info("Parsed {} lines with {} failures. Took {}ms", total.get(), failed.get(), took);        return total.get() - failed.get();    } }
As you can see, we simply need to pass a source as an InputStream, a POJO class for the JSON we want to parse to, a Java 8 Consumer to act on each parsed row, and that’s it. The above is just a simple snippet for illustrative purposes. In a production environment, one should add more robust error handling.
So why Lined JSON?
Indeed, with these numerous JSON parsing tools, why the heck someone decided to go Lined JSON? Is it any fancy writing every single line in this JSON-y object format?
Actually, yes, it is fancy. Just think of it for a second -- you read the line and get a valid JSON object. Let me put it this way: you load just one line into memory and get a valid JSON object you can work with in your code. Another line -- another object. Worked with it, released from memory, going next. And this is how you proceed through the entire file, no matter how long it is.
Just imagine a huge JSON array weighting a good couple of huundred MBs. Going straightforward and reading in full would take quite a bunch of memory. Going lined JSON approach would allow you iterating through each line and spending just a little of your precious memory. For sure, in some cases we need the whole thing loaded, but for others it's just fine to go one by one. So, lessons learned, another convenient data structure to use and to handle!
Originally posted in Reading Lined JSON files with Java 8
1 note · View note
bestwallartdesign · 2 years ago
Text
The JSON Data Type in MySQL: Pluses and Minuses
Tumblr media
Optimizing data architecture is an important part of your application development process. Data in MySQL is generally stored in the record format, and when the data is called from the database by UI/UX or any other function, Java converts the data format to JSON before sending it to the client.
Related articles
InfluxDB: A modern approach to monitoring IoT & System
Here’s how DevSecOps is taking over from DevOps to help businesses gain an edge
Data saved in record format
Data saved in JSON format
Retrieve data faster by storing it as JSON
Conversion from row data to JSON is an extra layer of effort, and it takes more time and processing power to complete the action.
Exporting MySQL data to JSON can be done using any of the following methods:
Using the CONCAT() and GROUP_CONCAT() functions
Using JSON_OBJECT and JSON_ARRAYAGG functions
Using MySQL Shell and JSON Format Output
Using third-party software such as ApexSQL Database Power Tools for VS Code
Since version 5.7.8, MySQL has supported the JSON data type and allowed users to save in JSON format directly. However, since record format is the default, many users prefer to continue with this more traditional data format. By investing in the right data architecture services, you can optimize your data formats and data types to get the most out of your data architecture.
What is the JSON data format?
JSON, or JavaScript Object Notation, is a lightweight data-interchange format that’s similar to other data types. The storage size of a JSON document (also known as a NoSQL database) is around the same as that of LONGBLOB or LONGTEXT data.
Now that MySQL can store JSON data in binary format, the server can efficiently store, search and retrieve JSON data from the MySQL database.
When do you use it?
JSON data stores configuration data of multiple attributes. For example, let’s say you have the attributes of a customizable character in a game. One player’s character may have a hat, while another may have shoes a particular shade of red. All these data points can be captured in JSON tabular data.
How do you use it?
The syntax for a JSON column is column_name JSON. Data can be stored as JSON in MySQL by using the JSON data type.
Why should you use it?
The JSON data type provides certain advantages over storing JSON-format strings in a string column:
Automatic content validation: When you’re adding JSON data to MySQL, the database automatically confirms that the data format fits the data and doesn’t allow you to save it if it doesn’t match.
Faster data transfer: All calls from other clients require data conversion from record to JSON. Saving data directly in JSON makes data transfer more efficient. In addition, JSON can have a substantially lower character count, reducing the overhead in data transfers.
Readability: Since JSON data is saved in text format, not XML, it’s more easily readable by the human eye.
Easy exchange: JSON is helpful when it comes to data exchange between heterogeneous systems since you can also use the JSON format with other programming languages.
Potential to combine and store: JSON documents with multiple keys and value attributes can be saved in the same document.
What are the disadvantages of using JSON?
Indexing: MySQL doesn’t support indexing of JSON columns, which means that if you want to search through your JSON documents, you could trigger a full table scan. A JSON column cannot be indexed directly. However, if you wish to perform more efficient searches, you can generate a column with values extracted from the JSON column, on which you can create an index.
Limited storage: JSON documents stored in MySQL can only reach a theoretical maximum size of 1GB.
Inefficient storage: JSON could be more storage efficient. If your priority is optimizing data architecture by prioritizing storage efficiency in your database schema, you may be better off with more traditional data types such as INT, CHAR, VARCHAR, and the like.
At CloudNow, we understand that using the correct data types and formats is key to optimizing your data architecture. That’s why we stay up-to-date with the functionalities available on MySQL and the benefits of using different data types for different requirements, to provide top quality data architecture services.
0 notes
pythonprogram2 · 3 years ago
Text
Lexicographical Order
 The term Lexicographical order is a mathematical term known by names: lexical order, lexicographic(al) product, alphabetical order, or dictionary order.
This section will cover the topic lexicographical order, its definition, and other detailed information. After that, we will learn how to use the concept of lexicographical order in the Java programming language.
Tumblr media
Defining Lexicographical Order
Lexicographical order or lexicographic in mathematics is a generalization of the alphabetical sequence of the dictionaries to the sequences of the ordered symbols or elements of a totally ordered list. The term lexicographical order is motivated by the word 'lexicon'. Lexicon is the set of words used in some of the other languages and has a conventional ordering. Thus, lexicographical order is a way for formalizing word order where the order of the underlying symbols is given.
In programming, lexicographical order is popularly known as Dictionary order and is used to sort a string array, compare two strings, or sorting array elements. It becomes quite easy to sort elements lexically. It is because lexicographical order has several variants and generalizations in which:
One variant is applicable to the sequences of different lengths as before considering the particular elements, lengths of the sequences are compared.
The second variant is used in order subsets of a given finite set. It does so by assigning a total order to the finite set. Then it is converting subsets into increasing sequences to which the lexicographical order is applied.
The generalization refers to the Cartesian product sequence of partially ordered sets, and such sequence is a total order, if and only if each factor of the Cartesian product are ordered totally.
Understanding the Formal Notion of Lexicographical Order
In order to understand the formal notion of the lexicographical order:
It begins with a finite set A, which is known as the alphabet and is completely sequenced. It further means that for a and b (any two symbols which are different and not the same) in A, either a < b or b < a.
Here, the words of A are the finite sequence of symbols from A and including words of length 1 holding a single symbol, words of length 2 with two symbols, and for words of length three, it's 3, and so on. With regards, it also includes the empty sequence ? holding no symbols at all. Thus the lexicographical order for the finite set A can be described as:
Suppose, for the two different worlds of the same length, a=a1a2…ak and b=b1b2…bk is given. Here, two words order depends on the alphabetic order of the symbols in the first place i where two words vary when counting from the beginning of the words, i.e., satisfying the condition a < b if and only if ai < bi within the order of the alphabet A.
If two words have varied in length, the usual lexicographical order pads the word with shorter length with blanks at the end until both words become the same in length, and then the words are compared.
Implementing Lexicographical in Java
As discussed above that lexicographical order can be used either for comparing two strings or sorting the elements. Here, we will discuss both the methods and will Implement each.
Sorting Elements in Lexicographical Order
Arranging words in order is known as lexicographical order or also known as Dictionary order. It means that on applying lexicographical order, the words are ordered alphabetically as per their component alphabets. For sorting a string array in lexicographical order, we have the following two methods:
Method 1: Applying any sorting method
Below is the example code given that will let us understand that how we can perform sorting on elements in Lexicographical order:
public class Main {  
   public static void main(String[] args) {  
      String[] name = { "John","Remo","Mixy","Julie","Ronny"};  
      int n = 5;  
      System.out.println("Before Sorting");  
      for(int i = 0; i < n; i++) {  
         System.out.println(name[i]);  
      }  
      for(int i = 0; i < n-1; ++i) {  
         for (int j = i + 1; j < n; ++j) {  
            if (name[i].compareTo(name[j]) > 0) {  
               String temp = name[i];  
               name[i] = name[j];  
               name[j] = temp;  
            }  
         }  
      }  
      System.out.println("\nAfter performing lexicographical order: ");  
      for(int i = 0; i < n; i++) {  
         System.out.println(name[i]);  
      }  
   }  
}  
Code Explanation:
In the above code, we have created a class Main within which the main () method is created.
A string has been initialized, holding some values to it, and each word will get printed as per for loop.
Then, we have implemented the main logic within another for loop with the help of which we can form the lexicographical order of the words given.
Finally, via for loop, the arranged words are printed on the screen.
Tumblr media
From the output, we can analyze that the given sequence of the words was not in alphabetical order but after applying the lexicographical order code, we can see that every word is sequenced now in alphabetical order.
Method 2: Applying sort () function
The sort () method is available in the Arrays class within the util package.
Below is the example code given that will let us understand that how we can perform sorting on elements in Lexicographical order:
import java.io.*;  
import java.util.Arrays;  
    
class Main {  
    public static void printArray(String str[])  
    {  
        for (String string : str)  
            System.out.print(string + " ");  
        System.out.println();  
    }  
    public static void main(String[] args)  
    {  
        String arr[]  
            = {"John","Harry","Emlie","Ronny","Julie","Mary" };  
  
        Arrays.sort(arr,String.CASE_INSENSITIVE_ORDER);  
        printArray(arr);  
    }  
}  
On executing the above output, we got the below-shown output:
Tumblr media
Comparing two strings using Lexicographical order in Java
For comparing two strings using Lexicographical order, we have the following two methods:
Using compareTo () method
Let's begin one by  one:
Using compareTo () method
Below is an example implementation by which we can compare to strings lexicographically:
import java.lang.*;  
public class StringExample {  
   public static void main(String[] args) {  
      String str1 = "String", str2 = "Comparison";  
       int get_val = str1.compareTo(str2);  
  
      if (get_val < 0) {  
         System.out.println("str1 is greater than str2");  
      } else if (get_val == 0) {  
         System.out.println("str1 is equal to str2");  
      } else {  
         System.out.println("str1 is less than str2");  
      }  
   }  
}  
Code Explanation:
We have created a class StringExample where we have implemented the main () method.
We have initialized two strings, i.e., str1 and str2.
Next, using the compareTo () method, we have compared the strings str1 and str2.
After it, if the get_val value is found less than 0, it means str1 is greater than str2.
Else if the get_val value is equal to 0, it means both str1 and str2 strings are equal.
Else, both the strings str1 is less than str2.
Tumblr media
By creating a user-defined function
Below we have created a user-defined function using which we can compare two strings lexicographically. The code is as follows:
public class StringExample {  
  public static void main(String[] args) {  
      
    String firstString = "Red";  
    String secondString = "Red";  
    String thirdString = "Green";  
    String fourthString = "Yellow";  
    String fifthString = "REdGreen";  
      
    System.out.println("Comparing two strings lexicographically by user defined function");  
      
    System.out.print("\nCompairing firstString ("+firstString+") to the secondString ("+secondString+") returns: ");  
    System.out.println(compareString(firstString, secondString));  
      
    System.out.print("\nCompairing secondString ("+secondString+") to the thirdString ("+thirdString+") returns: ");  
    System.out.println(compareString(secondString, thirdString));  
      
    System.out.print("\nCompairing thirdString ("+thirdString+") to the fourthString ("+fourthString+") returns: ");  
    System.out.println(compareString(thirdString, fourthString));  
      
    System.out.print("\nCompairing fourthString ("+fourthString+") to the firstString ("+firstString+") returns: ");  
    System.out.println(compareString(fourthString, firstString));  
   
    System.out.print("\nCompairing firstString ("+firstString+") to the fifthString ("+fifthString+") returns: ");  
    System.out.println(compareString(firstString, fifthString));  
  }  
  
  public static int compareString(String str, String argString) {  
      
    int lim= Math.min(str.length(), argString.length());  
      
    int k=0;  
    while(k
      if(str.charAt(k)!= argString.charAt(k)) {  
        return (int) str.charAt(k)- argString.charAt(k);  
      }  
      k++;  
    }  
    return str.length() - argString.length();  
  }  
}  
Tumblr media
Code Explanation:
We have created a Java class where we have initialized five strings.
Next, we have compared the first string with the second string, the second to the third-string, and so on..
For making the comparison, we have created a user-defined function compareString () whereby comparing the length and each character of the strings, and we got the results.
Therefore, in this way, we can make use of the lexicographical order in Java for performing such tasks.
1 note · View note
tpointtechblog · 1 year ago
Text
Java String to Int: A Comprehensive Guide for Developers
Introduction to Java String to Int: In the world of Java programming, converting strings to integers is a common task that developers encounter frequently.
Whether you're parsing user input, manipulating data from external sources, or performing calculations, understanding how to convert strings to integers is essential.
Tumblr media
In this comprehensive guide, we'll explore the various techniques, best practices, and considerations for converting strings to integers in Java.
Understanding String to Int Conversion:
Before diving into the conversion process, it's important to understand the difference between strings and integers in Java.
Strings are sequences of characters, while integers are numeric data types used to represent whole numbers. The process of converting a string to an integer involves parsing the string and extracting the numerical value it represents.
Using parseInt() Method:
One of the most common methods for converting strings to integers in Java is the parseInt() method, which is part of the Integer class. This method takes a string as input and returns the corresponding integer value. It's important to note that parseInt() can throw a NumberFormatException if the string cannot be parsed as an integer, so error handling is essential.
Example:
String str = "123"; int num = Integer.parseInt(str); System.out.println("Integer value: " + num);
Handling Exceptions:
As mentioned earlier, the parseInt() method can throw a NumberFormatException if the string is not a valid integer.
To handle this exception gracefully, developers should use try-catch blocks to catch and handle the exception appropriately. This ensures that the application doesn't crash unexpectedly if invalid input is provided.
Using valueOf() Method:
In addition to parseInt(), Java also provides the valueOf() method for converting strings to integers. While value Of() performs a similar function to parseInt(), it returns an Integer object rather than a primitive int. This can be useful in certain situations where an Integer object is required instead of a primitive int.
Example:
String str = "456"; Integer num = Integer.valueOf(str); System.out.println("Integer value: " + num);
Considerations and Best Practices:
When converting strings to integers in Java, there are several considerations and best practices to keep in mind:
Always validate input strings to ensure they represent valid integers before attempting conversion.
Handle exceptions gracefully to prevent application crashes and improve error handling.
Use parseInt() or valueOf() depending on your specific requirements and whether you need a primitive int or Integer object.
Consider performance implications, especially when dealing with large volumes of data or performance-critical applications.
Conclusion:
Converting strings to integers is a fundamental task in Java programming Language, and understanding the various techniques and best practices is essential for developers.
By following the guidelines outlined in this comprehensive guide, you'll be well-equipped to handle string to int conversion efficiently and effectively in your Java projects.
Happy coding!
1 note · View note
pradtutorials · 4 years ago
Link
How to Convert String to Int Java with Examples
0 notes
vespaengine · 7 years ago
Text
Blog search application in Vespa
Introduction
This is the first of a series of blog posts where data from WordPress.com (WP) is used to highlight how Vespa can be used to store, search and recommend blog posts. The data was made available during a Kaggle challenge to predict which blog posts someone would like based on their past behavior. It contains many ingredients that are necessary to showcase needs, challenges and possible solutions that are useful for those interested in building and deploying such applications in production.
The end goal is to build an application where:
Users will be able to search and manipulate the pool of blog posts available.
Users will get blog post recommendations from the content pool based on their interest.
This part addresses:
How to describe the dataset used as well as any information connected to the data.
How to set up a basic blog post search engine using Vespa.
The next parts show how to extend this basic search engine application with machine learned models to create a blog recommendation engine.
Dataset
The dataset contains blog posts written by WP bloggers and actions, in this case ‘likes’, performed by WP readers in blog posts they have interacted with. The dataset is publicly available at Kaggle and was released during a challenge to develop algorithms to help predict which blog posts users would most likely ‘like’ if they were exposed to them. The data includes these fields per blog post:
post_id - unique numerical id identifying the blog post
date_gmt - string representing date of blog post creation in GMT format yyyy-mm-dd hh:mm:ss
author - unique numerical id identifying the author of the blog post
url - blog post URL
title - blog post title
blog - unique numerical id identifying the blog that the blog post belongs to
tags - array of strings representing the tags of the blog posts
content - body text of the blog post, in html format
categories - array of strings representing the categories the blog post was assigned to
For the user actions:
post_id - unique numerical id identifying the blog post
uid - unique numerical id identifying the user that liked post_id
dt - date of the interaction in GMT format yyyy-mm-dd hh:mm:ss
Downloading raw data
For the purposes of this post, it is sufficient to use the first release of training data that consists of 5 weeks of posts as well as all the ‘like’ actions that occurred during those 5 weeks.
This first release of training data is available here - once downloaded, unzip it. The 1,196,111 line trainPosts.json will be our practice document data. This file is around 5GB in size.
Requirements
Indexing the full data set requires 23GB disk space. We have tested with a Docker container with 10GB RAM. We used similar settings as described in the vespa quick start guide. As in the guide we assume that the $VESPA_SAMPLE_APPS env variable points to the directory with your local clone of the vespa sample apps:
$ docker run -m 10G --detach --name vespa --hostname vespa --privileged --volume $VESPA_SAMPLE_APPS:/vespa-sample-apps --publish 8080:8080 vespaengine/vespa
Searching blog posts
Functional specification:
Blog post title, content, tags and categories must all be searchable
Allow blog posts to be sorted by both relevance and date
Allow grouping of search results by tag or category
In terms of data, Vespa operates with the notion of documents. A document represents a single, searchable item in your system, e.g., a blog post, a photo, or a news article. Each document type must be defined in the Vespa configuration through a search definition. Think of a search definition as being similar to a table definition in a relational database; it consists of a set of fields, each with a given name, a specific type, and some optional properties.
As an example, for this simple blog post search application, we could create the document type blog_post with the following fields:
url - of type uri
title - of type string
content - of type string (string fields can be of any length)
date_gmt - of type string (to store the creation date in GMT format)
The data fed into Vespa must match the structure of the search definition, and the hits returned when searching will be on this format as well.
Application Packages
A Vespa application package is the set of configuration files and Java plugins that together define the behavior of a Vespa system: what functionality to use, the available document types, how ranking will be done and how data will be processed during feeding and indexing. The search definition, e.g., blog_post.sd, is a required part of an application package — the other required files are services.xml and hosts.xml.
The sample application blog search creates a simple but functional blog post search engine. The application package is found in src/main/application.
Services Specification
services.xml defines the services that make up the Vespa application — which services to run and how many nodes per service:
<?xml version='1.0' encoding='UTF-8'?> <services version='1.0'>  <container id='default' version='1.0'>    <search/>    <document-api/>    <nodes>      <node hostalias='node1'/>    </nodes>  </container>  <content id='blog_post' version='1.0'>    <search>      <visibility-delay>1.0</visibility-delay>    </search>    <redundancy>1</redundancy>    <documents>      <document mode='index' type='blog_post'/>    </documents>    <nodes>      <node hostalias='node1'/>    </nodes>    <engine>      <proton>        <searchable-copies>1</searchable-copies>      </proton>    </engine>  </content> </services>
<container> defines the container cluster for document, query and result processing
<search> sets up the search endpoint for Vespa queries. The default port is 8080.
<document-api> sets up the document endpoint for feeding.
<nodes> defines the nodes required per service. (See the reference for more on container cluster setup.)
<content> defines how documents are stored and searched
<redundancy> denotes how many copies to keep of each document.
<documents> assigns the document types in the search definition — the content cluster capacity can be increased by adding node elements — see elastic Vespa. (See also the reference for more on content cluster setup.)
<nodes> defines the hosts for the content cluster.
Deployment Specification
hosts.xml contains a list of all the hosts/nodes that is part of the application, with an alias for each of them. Here we use a single node:
<?xml version="1.0" encoding="utf-8" ?> <hosts>  <host name="localhost">    <alias>node1</alias>  </host> </hosts>
Search Definition
The blog_post document type mentioned in src/main/application/service.xml is defined in the search definition. src/main/application/searchdefinitions/blog_post.sd contains the search definition for a document of type blog_post:
search blog_post {    document blog_post {        field date_gmt type string {            indexing: summary        }        field language type string {            indexing: summary        }        field author type string {            indexing: summary        }        field url type string {            indexing: summary        }        field title type string {            indexing: summary | index        }        field blog type string {            indexing: summary        }        field post_id type string {            indexing: summary        }        field tags type array<string> {            indexing: summary        }        field blogname type string {            indexing: summary        }        field content type string {            indexing: summary | index        }        field categories type array<string> {            indexing: summary        }        field date type int {            indexing: summary | attribute        }    }    fieldset default {        fields: title, content    }    rank-profile post inherits default {        first-phase {            expression:nativeRank(title, content)        }    } }
document is wrapped inside another element called search. The name following these elements, here blog_post, must be exactly the same for both.
The field property indexing configures the indexing pipeline for a field, which defines how Vespa will treat input during indexing — see indexing language. Each part of the indexing pipeline is separated by the pipe character ‘|’:
index: Create a search index for this field
attribute: Store this field in memory as an attribute — for sorting, searching and grouping
summary: Let this field be part of the document summary in the result set
Deploy the Application Package
Once done with the application package, deploy the Vespa application — build and start Vespa as in the quick start. Deploy the application:
$ cd /vespa-sample-apps/blog-search $ vespa-deploy prepare src/main/application && vespa-deploy activate
This prints that the application was activated successfully and also the checksum, timestamp and generation for this deployment (more on that later). Pointing a browser to http://localhost:8080/ApplicationStatus returns JSON-formatted information about the active application, including its checksum, timestamp and generation (and should be the same as the values when vespa-deploy activate was run). The generation will increase by 1 each time a new application is successfully deployed, and is the easiest way to verify that the correct version is active.
The Vespa node is now configured and ready for use.
Feeding Data
The data fed to Vespa must match the search definition for the document type. The data downloaded from Kaggle, contained in trainPosts.json, must be converted to a valid Vespa document format before it can be fed to Vespa. Find a parser in the utility repository. Since the full data set is unnecessarily large for the purposes of this first part of this post, we use only the first 10,000 lines of it, but feel free to load all 1,1M entries:
$ head -10000 trainPosts.json > trainPostsSmall.json $ python parse.py trainPostsSmall.json > feed.json
Send this to Vespa using one of the tools Vespa provides for feeding. Here we will use the Java feeding API:
$ java -jar $VESPA_HOME/lib/jars/vespa-http-client-jar-with-dependencies.jar --verbose --file feed.json --host localhost --port 8080
Note that in the sample-apps/blog-search directory, there is a file with sample data. You may also feed this file using this method.
Track feeding progress
Use the Metrics API to track number of documents indexed:
$ curl -s 'http://localhost:19112/state/v1/metrics' | tr ',' '\n' | grep -A 2 proton.doctypes.blog_post.numdocs
You can also inspect the search node state by
$ vespa-proton-cmd --local getState  
Fetch documents
Fetch documents by document id using the Document API:
$ curl -s 'http://localhost:8080/document/v1/blog-search/blog_post/docid/1750271' | python -m json.tool
The first query
Searching with Vespa is done using a HTTP GET requests, like:
<host:port>/<search>?<yql=value1>&<param2=value2>...
The only mandatory parameter is the query, using yql=<yql query>. More details can be found in the Search API.
Given the above search definition, where the fields title and content are part of the fieldset default, any document containing the word “music” in one or more of these two fields matches our query below:
$ curl -s 'http://localhost:8080/search/?yql=select+*+from+sources+*+where+default+contains+%22music%22%3B' | python -m json.tool
Looking at the output, please note:
The field documentid in the output and how it matches the value we assigned to each put operation when feeding data to Vespa.
Each hit has a property named relevance, which indicates how well the given document matches our query, using a pre-defined default ranking function. You have full control over ranking — more about ranking and ordering later. The hits are sorted by this value.
When multiple hits have the same relevance score their internal ordering is undefined. However, their internal ordering will not change unless the documents are re-indexed.
Add &tracelevel=9 to dump query parsing details
Other examples
yql=select+title+from+sources+*+where+title+contains+%22music%22%3B
Once more a search for the single term “music”, but this time with the explicit field title. This means that we only want to match documents that contain the word “music” in the field title. As expected, you will see fewer hits for this query, than for the previous one.
yql=select+*+from+sources+*+where+default+contains+%22music%22+AND+default+contains+%22festival%22%3B
This is a query for the two terms “music” and “festival”, combined with an AND operation; it finds documents that match both terms — but not just one of them.
yql=select+*+from+sources+*+where+sddocname+contains+%22blog_post%22%3B
This is a single-term query in the special field sddocname for the value “blog_post”. This is a common and useful Vespa trick to get the number of indexed documents for a certain document type (search definition): sddocname is a special and reserved field which is always set to the name of the document type for a given document. The documents are all of type blog_post, and will therefore automatically have the field sddocname set to that value.
This means that the query above really means “Return all documents of type blog_post”, and as such all documents in the index are returned.
Relevance and Ranking
Ranking and relevance were briefly mentioned above; what is really the relevance of a hit, and how can one change the relevance calculations? It is time to introduce rank profiles and rank expressions — simple, yet powerful methods for tuning the relevance.
Relevance is a measure of how well a given document matches a query. The default relevance is calculated by a formula that takes several factors into consideration, but it computes, in essence, how well the document matches the terms in the query. Sample use cases for tweaking the relevance calculations:
Personalize search results based on some property; age, nationality, language, friends and friends of friends.
Rank fresh (age) documents higher, while still considering other relevance measures.
Rank documents by geographical location, searching for relevant resources nearby.
Vespa allows creating any number of rank profiles: named collections of ranking and relevance calculations that one can choose from at query time. A number of built-in functions and expressions are available to create highly specialized rank expressions.
Blog popularity signal
It is time to include the notion of blog popularity into the ranking function. Do this by including the post_popularity rank profile below at the bottom of src/main/application/searchdefinitions/blog_post.sd, just below the post rank profile.
   rank-profile post_popularity inherits default {        first-phase {            expression: nativeRank(title, content) + 10 * if(isNan(attribute(popularity)), 0, attribute(popularity))        }    }
Also, add a popularity field at the end of the document definition:
       field popularity type double {            indexing: summary | attribute        }
Notes (more information can be found in the search definition reference):
rank-profile post_popularity inherits default This configures Vespa to create a new rank profile named post_popularity, which inherits all the properties of the default rank-profile; only properties that are explicitly defined, or overridden, will differ from those of the default rank-profile.
first-phase Relevance calculations in Vespa are two-phased. The calculations done in the first phase are performed on every single document matching your query, while the second phase calculations are only done on the top n documents as determined by the calculations done in the first phase.
expression: nativeRank(title, content) + 10 * if(isNan(attribute(popularity)), 0, attribute(popularity)) Still using the basic search relevance for title and content, boosting documents based on some document level popularity signal. This expression is used to rank documents. Here, the default ranking expression — the nativeRank of the default field set — is included to make the query relevant, while the custom, second term includes the document value attribute(popularity), if this is set. The weighted sum of these two terms is the final relevance for each document.
Deploy the configuration:
$ vespa-deploy prepare src/main/application && vespa-deploy activate
Use parse.py — which has a -p option to calculate and add a popularity field — and then feed the parsed data:
$ python parse.py -p trainPostsSmall.json > feed_with_popularity.json $ java -jar $VESPA_HOME/lib/jars/vespa-http-client-jar-with-dependencies.jar --file feed_with_popularity.json --host localhost --port 8080
After feeding, query
$ curl -s 'http://localhost:8080/search/?yql=select+*+from+sources+*+where+default+contains+%22music%22%3B&ranking=post_popularity' | python -m json.tool
and find documents with high popularity values at the top.
Sorting and Grouping
What is an attribute?
An attribute is an in-memory field - this is different from index fields, which may be moved to a disk-based index as more documents are added and the index grows. Since attributes are kept in memory, they are excellent for fields which require fast access, e.g., fields used for sorting or grouping query results. The downside is higher memory usage. By default, no index is generated for attributes, and search over these defaults to a linear scan - to build an index for an attribute field, include attribute:fast-search in the field definition.
Defining an attribute field
An example is found in blog_post.sd:
field date type int {    indexing: summary | attribute }
The data has format YYYYMMDD. And since the field is an int, it can be used for range searches.
Example queries using attribute field
yql=select+*+from+sources+*+where+default+contains+%2220120426%22%3B
This is a single-term query for the term 20120426 in the default field set. (The strings %22 and %3B are URL encodings for " and ;.) In the search definition, the field date is not included in the default field set. Nevertheless, the string “20120426” is found in the content of many posts, which are returned then as results.
yql=select+*+from+sources+*+where+date+contains+%2220120426%22%3B
To get documents that were created 26 April 2012, and whose date field is 20120426, replace default with date in the YQL query string. Note that since date has not been defined with attribute:fast-search, searching will be done by scanning all documents.
yql=select+*+from+sources+*+where+default+contains+%22recipe%22+AND+date+contains+%2220120426%22%3B
A query with two terms; a search in the default field set for the term “recipe” combined with a search in the date field for the value 20120426. This search will be faster than the previous example, as the term “recipe” is for a field for which there is an index, and for which the search core will evaluate the query first. (This speedup is only noticeable with the full data set!)
Range searches
The examples above searched over date just as any other field, and requested documents where the value was exactly 20120426. Since the field is of type int, however, we can use it for range searches as well, using the “less than” and “greater than” operators (< and >, or %3C and %3E URL encoded). The query
yql=select+*+from+sources+*+where+date+%3C+20120401%3B
finds all documents where the value of date is less than 20120401, i.e., all documents from before April 2012, while
yql=select+*+from+sources+*+where+date+%3C+20120401+AND+date+%3E+20120229%3B
finds all documents exactly from March 2012.
Sorting
The first feature we will look at is how an attribute can be used to change the hit order. By now, you have probably noticed that hits are returned in order of descending relevance, i.e., how well the document matches the query — if not, take a moment to verify this.
Now try to send the following query to Vespa, and look at the order of the hits:
$ curl -s 'http://localhost:8080/search/?yql=select+*+from+sources+*+where+default+contains+%22music%22+AND+default+contains+%22festival%22+order+by+date%3B' | python -m json.tool
By default, sorting is done in ascending order. This can also be specified by appending asc after the sort attribute name. Use desc to sort the in descending order:
$ curl -s 'http://localhost:8080/search/?yql=select+*+from+sources+*+where+default+contains+%22music%22+AND+default+contains+%22festival%22+order+by+date+desc%3B' | python -m json.tool
Query time data grouping
Grouping is the concept of looking through all matching documents at query-time and then performing operations with specified fields across all the documents — some common use cases include:
Find all the unique values for a given field, make one group per unique value, and return the count of documents per group.
Group documents by time and date in fixed-width or custom-width buckets. An example of fixed-width buckets could be to group all documents by year, while an example of custom buckets could be to sort bug tickets by date of creation into the buckets Today, Past Week, Past Month, Past Year, and Everything else.
Calculate the minimum/maximum/average value for a given field.
Displaying such groups and their sizes (in terms of matching documents per group) on a search result page, with a link to each such group, is a common way to let users refine searches. For now we will only do a very simple grouping query to get a list of unique values for date ordered by the number of documents they occur in and top 3 is shown:
$ curl -s 'http://localhost:8080/search/?yql=select%20*%20from%20sources%20*%20where%20sddocname%20contains%20%22blog_post%22%20limit%200%20%7C%20all(group(date)%20max(3)%20order(-count())each(output(count())))%3B' | python -m json.tool
With the full data set, you will get the following output:
{    "root": {        "children": [            {                "children": [                    {                        "children": [                            {                                "fields": {                                    "count()": 43                                },                                "id": "group:long:20120419",                                "relevance": 1.0,                                "value": "20120419"                            },                            {                                "fields": {                                    "count()": 40                                },                                "id": "group:long:20120424",                                "relevance": 0.6666666666666666,                                "value": "20120424"                            },                            {                                "fields": {                                    "count()": 39                                },                                "id": "group:long:20120417",                                "relevance": 0.3333333333333333,                                "value": "20120417"                            }                        ],                        "continuation": {                            "next": "BGAAABEBGBC"                        },                        "id": "grouplist:date",                        "label": "date",                        "relevance": 1.0                    }                ],                "continuation": {                    "this": ""                },                "id": "group:root:0",                "relevance": 1.0            }        ],        "coverage": {            "coverage": 100,            "documents": 1000,            "full": true,            "nodes": 0,            "results": 1,            "resultsFull": 1        },        "fields": {            "totalCount": 1000        },        "id": "toplevel",        "relevance": 1.0    } }
The three most common unique values of date are listed, along with their respective counts.
Try to change the filter part of the YQL+ expression — the where clause — to a text match of “recipe”, or restrict date to be less than 20120401, and see how the list of unique values changes as the set of matching documents for your query changes. Try to search for the single term “Verizon” as well — a word we know is not present in the document set, and as such will match no documents — and you will see that the list of groups is empty.
Attribute limitations
Memory usage
Attributes are kept in memory at all time, as opposed to normal indexes where the data is mostly kept on disk. Even with large search nodes, one will notice that it is not practical to define all the search definition fields as attributes, as it will heavily restrict the number of documents per search node. Some Vespa installations have more than 1 billion documents per node — having megabytes of text in memory per document is not an option.
Matching
Another limitation is the way matching is done for attributes. Consider the field blogname from our search definition, and the document for the blog called “Thinking about museums”. In the original input, the value for blogname is a string built of up the three words “Thinking”, “about”, and “museums”, with a single whitespace character between them. How should we be able to search this field?
For normal index fields, Vespa does something called tokenization on the string. In our case this means that the string above is split into the three tokens “Thinking”, “about” and “museums”, enabling Vespa to match this document both for the single-term queries “Thinking”, “about” and “museums”, the exact phrase query “Thinking about museums”, and a query with two or more tokens in either order (e.g. “museums thinking”). This is how we all have come to expect normal free text search to work.
However, there is a limitation in Vespa when it comes to attribute fields and matching; attributes do not support normal token-based matching — only exact matching or prefix matching. Exact matching is the default, and, as the name implies, it requires you to search for the exact contents of the field in order to get a match.
When to use attributes
There are both advantages and drawbacks of using attributes — it enables sorting and grouping, but requires more memory and gives limited matching capabilities. When to use attributes depends on the application; in general, use attributes for:
fields used for sorting, e.g., a last-update timestamp,
fields used for grouping, e.g., problem severity, and
fields that are not long string fields.
Finally, all numeric fields should always be attributes.
Clean environment by removing all documents
vespa-remove-index removes all documents:
$ vespa-stop-services $ vespa-remove-index $ vespa-start-services
Conclusion
You should now have a basic understanding of how Vespa can help build your application. In the next blog post we will proceed to show how can we use Statistics and Machine Learning to extend a basic search application into a recommendation system.
2 notes · View notes
siva3155 · 5 years ago
Text
300+ TOP ARDUINO Interview Questions and Answers
Arduino Interview Questions for freshers experienced :-
1. What is Arduino? Arduino is an open source electronic platform. It is based on easy-to-use hardware and software. It able to read input signal. It is used to write and upload the computer code to the physical board by using Arduino. 2. What is the stable version of Arduino software? The stable version of Arduino software is 1.8.3 and released on 31 may 2017. 3. Who is the developer of Arduino? Arduino is the developer of Arduino. 4. Why we should use Arduino? We should use Arduino because of its features: It is easy to use. It runs on Cross platform. Low cost It is open source. 5. In which language Arduino software was written? Arduino software was written in Java, C++ and c language. 6. What are the advantages of Arduino? There are following features of Arduino: It able to read analog or digital input signals. We can control our functions. It uses c and c++ programming language. 7. What are the IDE toolbar of Arduino? There are following IDE toolbar of Arduino: It is used to check if there is any compilation error. It is used to upload a program to the Arduino board. Shortcut used to create a new sketch. It is used to directly open one of the example sketch. It is used to save your sketch. Serial monitor used to receive serial data from the board and send the serial data to the board. 8. What is sketch in Arduino? In Arduino, the first terminology is the program called sketch. 9. What are the three important parts of Arduino? Arduino has three important parts: structure , values( variables and constants) and function. 10. What are the software structure functions? There are two main software structure functions: Setup( ) function Loop( ) function
Tumblr media
ARDUINO Interview Questions 11. What is the use of operator in Arduino? Operator is used to compile mathematical or logical functions. 12. How can we declare the functions? We can declare the function by using given code: int sum_func (int x, int y) // function declaration { int z = 0; z = x+y ; return z; // return the value } 13. Which function is used to find the length of string? Strlen() functions is used to find the length of string. 14. How to convert string to upper case? The my_str.toUpperCase() function is used to convert string to upper case. 15. What are the functions of time in Arduino? In Arduino, there are four functions of time: delay() delayMicroseconds() : millis() micros() 16. What are Libraries in Arduino? In Arduino, Libraries are collection of code that makes it easy to connect to a sensor, display, module etc. Arduino Questions and Answers Pdf Download Read the full article
0 notes
felord · 6 years ago
Text
CSE205 Object Oriented Programming and Data Structures Programming Project 4 Solved
Tumblr media
1  Submission Instructions
Create a folder named asuriteid-p04 where asuriteid is your ASURITE user id (for example, if your ASURITE user id is jsmith6 then your folder would be named jsmith6-p04) and copy your .java source code files to this folder. Do not copy the .class files or any other files. Next, compress the asuriteid-p04 folder creating a zip archive file named asuriteid-p04.zip (e.g., jsmith6-p04.zip). Upload asuriteid-p04.zip to the Blackboard Project 3 submission link by the project deadline. Please see the Course Schedule section in the Syllabus for the deadline. Consult the Syllabus for the late and academic integrity policies. Please note that the deadline for submitting Project 4 is a hard deadline, i.e., there is no 24 hour late penalty submission period. This is necessary so we can quickly grade Project 4 quickly before final letter grades are due.
2  Learning Objectives
Complete all of the learning objects of the previous projects. To implement a GUI interface and respond to action events. To use the linked list, stack, and queue classes.
3  Background
In the lecture notes and video lectures for Stacks and Queues : Sections 3 – 7 we discussed an application that evaluates an arithmetic expression written in infix notation such as: (-1 - -2) * -(3 / 5) Infix notation is the usual algebraic notation that we are all familiar with where a binary operator (a binary operator is an operator that has two operands) is written between the left-hand and right-hand operands. For example, in the expression above, the left-hand operand of the subtraction operator is -1 and the right-hand operand is -2. Some operators, such as the negation operator, are unary operators meaning there is only one operand (uni = one). For negation, the operand is written to the right of the negation operator. Therefore, this expression contains six operators: negation, subtraction, negation, multiplication, negation, and division. In the algorithm that evaluates the expression, we also treat a left parenthesis as an operator, which it is not, but during the evaluation we have to push left parentheses onto the operator stack. In an infix arithmetic expression, each operator has a precedence level, which for a left parenthesis, is different when it is on the operator stack as opposed to when it is not (this will become clear when you read the trace of the algorithm for the above expression; see page 3): Operator          Normal Precedence Level                 Stack Precedence Level (                                     5                                                         0 –                                    4                                                         4 *  /                                3                                                         3 +  –                               2                                                         2 )                                     1                                                         1 Right parentheses really don't have precedence because they are not pushed on the operator stack, but we assign them a precedence level of 1 for consistency. The algorithm discussed in the notes did not handle the negation operator so I have modified it to handle negation. Here is the revised algorithm: Method evaluate(In: pExpr as an infix expression) Returns Double Create operatorStack -- Stores Operators Create operandStack  -- Stores Operands While end of pExpr has not been reached Do Scan next token in pExpr -- The type of token is Token If token is an operand Then Convert token to Operand object named number operandStack.push(number) ElseIf token is an InstanceOf LeftParen Then Convert token to LeftParen object named paren operatorStack.push(paren) ElseIf token is an InstanceOf RightParen Then While not operatorStack.peek() is an InstanceOf LeftParen Do topEval() operatorStack.pop() -- Pops the LeftParen ElseIf token is Negation, Addition, Subtraction, Multiplication, or Division Then Convert token to Operator object named operator While keepEvaluating() returns True Do topEval() operatorStack.push(op) End While While not operatorStack.isEmpty() Do topEval() Return operandStack.pop()  -- the result of evaluating the expression End Method evaluate Method keepEvaluating() Returns True or False If operatorStack.isEmpty() Then Return False Else Return stackPrecedence(operatorStack.peek())  ≥ precedence(operator) End Method keepEvaluating Method topEval() Returns Nothing right ← operandStack.pop() operator ← operatorStack.pop() If operator is Negation Then operandStack.push(-right) Else left ← operandStack.pop() If operator is Addition Then operandStack.push(left + right) ElseIf operator is Subtraction Then operandStack.push(left - right) ElseIf operator is Multiplication Then  operandStack.push(left * right) Else operandStack.push(left / right) End If End Method topEval Method precedence(In: Operator pOperator) Returns Int If pOperator is LeftParen Then Return 5 ElseIf pOperator is Negation Then Return 4 ElseIf pOperator is Multiplication or Division Then Return 3 ElseIf pOperator is Addition or Subtraction Then Return 2 Else Return 1 End Method precedence Method stackPrecedence(In: Operator pOperator) Returns Int If pOperator is LeftParen Then Return 0 ElseIf pOperator is Negation Then Return 4 ElseIf pOperator is Multiplication or Division Then Return 3 ElseIf pOperator is Addition or Subtraction Then Return 2 Else Return 1 End Method stackPrecedence It would be worthwhile to trace the algorithm using the above expression to make sure you understand how it works: Create the operand and operator stacks. Both are empty at the beginning. Scan the first token ( and push it onto the operator stack. Scan the next token – (negation) and push it onto the operator stack. Scan the next token 1 and push it onto the operand stack. Scan the next token – (subtraction). Since the operator on top of the operator stack (negation) has higher precedence than subtraction, evaluate the top (note: negation is a unary operator so there is only one operand to be popped from the operand stack): Pop the top number from the operand stack. Call this right = 1. Pop the top operator from the operator stack. Call this operator = – (negation). Evaluate operator and push the result (-1) onto the operand stack. Now push the subtraction operator onto the operator stack. Scan the next token – (negation). Since the operator on top of the stack (subtraction) has precedence less than negation, push the negation operator onto the operator stack. Scan the next token 2 and push it onto the operand stack. Scan the next token ). Pop and evaluate operators from the operator stack until the matching ( is reached. a. The top operator is a unary operator (negation): Pop the top number from the operand stack. Call this right = 2. Pop the top operator from the operator stack. Call this operator = – (negation). Evaluate operator and push the result (-2) onto the operand stack. The top operator is a binary operator (subtraction): Pop the top number from the operand stack. Call this right = -2. Pop the top number from the operand stack. Call this left = -1. Pop the top operator from the operator stack. Call this operator = – (subtraction). Evaluate operator and push the result (1) onto the operand stack. The top operator is ( so pop it. Scan the next token * (multiplication). The operator stack is empty so push *. Scan the next token – (negation). Since negation has higher precedence than the operator on top of the operator stack (multiplication) push the negation operator onto the operator stack. Scan the next token ( and push it onto the operator stack. Scan the next token 3 and push it onto the operand stack. Scan the next token / (division). Since the operator on top of the stack (left parenthesis) has higher precedence than division push / onto the operator stack. Now do you see why the precedence of ( changes when it is on the operator stack? Scan the next token 5 and push it onto the operand stack. Scan the next token ). Pop and evaluate operators from the operator stack until the matching ( is reached. a. The top operator is binary operator (division): Pop the top number from the operand stack. Call this right = 5. Pop the top number from the operand stack. Call this left = 3. Pop the top operator from the operator stack. Call this operator = /. Evaluate operator and push the result (0.6) onto the operand stack. The top operator is ( so pop it. The end of the infix expression string has been reached. Pop and evaluate operators from the operator stack until the operator stack is empty. The top operator is a unary operator (negation): Pop the top number from the operand stack. Call this right = 0.6. Pop the top operator from the operator stack. Call this operator = – (negation). Evaluate operator and push the result (-0.6) onto the operand stack. The top operator is a binary operator (multiplication): Pop the top number from the operand stack. Call this right = -0.6. Pop the top number from the operand stack. Call this left = 1. Pop the top operator from the operator stack. Call this operator = *. Evaluate operator and push the result (-0.6) onto the operand stack. The operator stack is empty. Pop the result from the operand stack (-0.6) and return it.
4  Software Requirements
The project shall implement a GUI calculator which accepts as input a syntactically correct arithmetic expression written in infix notation and displays the result of evaluating the expression. The program shall meet these requirements. The program shall implement a GUI which permits the user to interact with the calculator. Watch the Project 4 video lecture for a demonstration of how the application works. When the Clear button is clicked, the input text field and the result label shall be configured to display nothing. When a syntactically correct infix arithmetic expression is entered in the input text field and the Evaluate button is clicked, the program shall evaluate the expression and display the result in the label of the GUI. When the input text field is empty, clicking the Evaluate button does nothing. When the Exit button is clicked, the application shall terminate. Note: you do not have to be concerned with syntactically incorrect infix expressions. We will not test your program with such expressions.
5  Software Design
Refer to the UML class diagram in Section 5.21. Your program shall implement this design.
5.1  Main Class
The Main class shall contain the main() method which shall instantiate an object of the Main class and call run() on that object. Main is completed for you.
5.2  AddOperator
Implements the addition operator, which is a binary operator. AddOperator is completed for you. Use AddOperator as a guide when completing DivOperator, MultOperator, and SubOperator.
5.3  BinaryOperator
The abstract superclass of all binary operators. BinaryOperator is completed for you. Note that BinaryOperator implements one abstract method evaluate() which all subclasses must implement. The subclasses are AddOperator, DivOperator, MultOperator, and SubOperator.
5.4  DivOperator
Implements the division operator, which is a binary operator. Complete the code in this file by using the AddOperator class as an example.
5.5  DList
This is the DList class from the Week 7 Source Code zip archive. It implements a generic doubly linked list where the data type of each list element is E. DList is completed for you. For example, to create a DList which stores elements of the type Token you would write DList list = new DList(); much in the same way that we can create an ArrayList of Doubles by writing ArrayList list = new ArrayList(); 5.6  Expression Represents an infix expression to be evaluated. Use the provided pseudocode as a guide in completing this class.
5.7  LeftParen
Represents a left parenthesis in the expression. LeftParen is completed for you. Note that LeftParen is a subclass of the abstract class Parenthesis.
5.8  MultOperator
Implements the multiplication operator, which is a binary operator. Complete the code in this file by using the AddOperator class as an example.
5.9  NegOperator
Implements the negation operator, which is a unary operator. Complete the code in this file by using the AddOperator class as an example. Note, however, that negation is a unary operator so it only has one operand. 5.10  Operand An operand is a numeric value represented as a Double. Implement the class using the UML class diagram as a guide.
5.11  Operator
Operator is the abstract superclass of all binary and unary operators, i.e., it is the superclass of BinaryOperator and UnaryOperator. Implement the class using the UML class diagram as a guide. Note that all of the non-constructor methods are abstract, i.e., none of them are implemented in Operator.
5.12  Parenthesis
Parenthesis is the superclass of LeftParen and RightParen. These are treated as a weird sort of Operator because we need to be able to push LeftParens on the operator stack when evaluating the expression. Parenthesis is completed for you.
5.13  Queue
Implements a generic queue data structure using a DList list to store the elements. This is the same class that was provided in the Week 7 Source zip archive. Queue is completed for you. 5.14  RightParen Represents a right parenthesis in the expression. RightParen is completed for you.
5.15  Stack
Implements a generic stack data structure using a DList list to store the elements. This is the same class that was provided in the Week 7 Source zip archive. Stack is completed for you.
5.16  SubOperator
Implements the subtraction operator, which is a binary operator. Complete the code in this file by using the AddOperator class as an example.
5.17  Token
Token is the abstract superclass of the different types of tokens (i.e., symbols) that can appear in an infix expression. Token is completed for you.
5.18  Tokenizer
The Tokenizer class scans a String containing an infix expression and breaks it into tokens. For this project, a token will be either an Operand (a double value), a LeftParen or RightParen, or an arithmetic UnaryOperator (subclass NegOperator) or BinaryOperator (one of the AddOperator, SubOperator, MultOperator, or DivOperator subclasses). Tokenizer is completed for you. It is implemented as a finite state machine (FSM) which are commonly used in computing, especially when breaking a "sentence" of words or symbols into its component parts. If you have the time, you should study the code to learn how FSM's work and are used. 5.19  UnaryOperator UnaryOperator is the superclass of all unary operators. UnaryOperator is completed for you. 5.20  View The View implements the GUI. Read the comments and implement the pseudocode.
5.21  UML Class Diagram
The UML class diagram is provided in the zip archive /uml folder as two UMLet files. Because the images in this document are small, there are PNG images of the two class diagrams in the /image folder. Your program shall implement this design.
5.21  UML Class Diagram (continued)
6  Additional Project Requirements
Format your code neatly. Use proper indentation and spacing. Study the examples in the book and the examples the instructor presents in the lectures and posts on the course website. Put a comment header block at the top of each method formatted thusly: /** * A brief description of what the method does.  */ Put a comment header block at the top of each source code file formatted thusly (or use /** ... */ comments if you wish): //******************************************************************************************************** // CLASS: classname (classname.java) // // COURSE AND PROJECT INFO // CSE205 Object Oriented Programming and Data Structures, semester and year // Project Number: project-number // // AUTHOR: your-name, your-asurite-id, your-email-addr //******************************************************************************************************** Read the full article
0 notes
mlbors · 8 years ago
Text
Introduction to Hash Tables
Through this article, we are going to take a look at the data structure called Hash Table.
Hash Table
A Hash Table is a data structure that stores data in an associative manner. It is made up of two parts: an array, where the data is stored, and a Hash Function which is a mapping function. Basically, a Hash Function is a function that takes things from one space and maps them to a space for indexing.
A Hash Table is used to implement structures such as dictionary, map, or associative array and it is a data structure in which insertion and search operations are very fast.
The idea behind a Hash Table is that for each element we want to store, we calculate a unique address and we put the value at this index in the array. When we need to find a value, we once again calculate its index and then return the value. In other words, a Hash Table allows us to store and retrieve objects by key.
A Hash Table is an array and initially, this array is empty. When we want to put a value into the Hash Table under a certain key, that key will be used to compute an index in the array. We can see it like so:
hashTable["firstName"] = "John" hashTable array: +--------------+ | 0: | +--------------+ | 1: | +--------------+ | 2: firstName | ---> John +--------------+ | 3: | +--------------+ | 4: | +--------------+
Here, the key is "firstName" and it maps to index 2. It means that our Hash Table asked for the index of the key "firstName" and the Hash Function returned 2. We can see it like so:
key ---> HASH FUNCTION --> array_index
Hash Function
Hashing is a technique that lets us convert a range of key values into a range of indexes of an array. The values returned by a Hash Function are called Hash Values. There are multiple ways for constructing a Hash Function but it is important that the function has some basic requirements:
The Hash Value is fully determined by the data being hashed and it should be easy to compute
The Hash Function uses all the input data
The Hash Function should provide a uniform distribution across the Hash Table
The Hash Function generates very different Hash Values for similar strings
Solving collisions
Let's say that our Hash Function will compute the index of a specific string by summing the ASCII values of the characters modulo 373 (that is a prime number). Our set of string will be {"abcdef", "bcdefa", "cdefab" , "defabc"}. Knowing that the ASCII values of a, b, c, d, e, and f are 97, 98, 99, 100, 101, and 102, our addition will always give us the same result (597) just like the operation 597%373.
When different elements get the same Hash Value, it is a called a collision. So, how can we handle such a situation? The two most popular ways to solve this are called Chaining and Open Addressing.
Chaining
When we use this method, we just use a Linked List and put all the values with the same Hash Value into it and so the Hash Table becomes an array of Linked Lists.
Open Addressing
Also called Linear Probing, when we use this method and we want to add a new entry, the Hash Index of the Hashed Value is computed and then the array is examined. If the slot is empty, then the value is inserted. If not, we calculate another Hash Value and check that slot and we continue until a place for the value is found. The probe sequence will be like so:
index = index % hashTableSize index = (index + 1) % hashTableSize index = (index + 2) % hashTableSize index = (index + 3) % hashTableSize ...
Quadratic Probing
Quadratic Probing is similar to Linear Probing but the difference is the interval between successive probes. With Quadratic Probing, the sequence would be something like this:
index = index % hashTableSize index = (index + 1^2) % hashTableSize index = (index + 2^2) % hashTableSize index = (index + 3^2) % hashTableSize ...
Double Hashing
Double Hashing works on a similar idea to Linear and Quadratic Probing. The difference is that a second Hash Function is used to determine the location of the next empty slot. The sequence would look like this:
index = (h1(key) + 1 * h2(key)) % hashTableSize; index = (h1(key) + 2 * h2(key)) % hashTableSize; index = (h1(key) + 3 * h2(key)) % hashTableSize; ...
Basic Examples
Knowing what we know, it is time to make really simple examples using Java.
Linear Probing
First, let's define a class for the Entry:
public class HashEntry { private int key; private int value; HashEntry(int key, int value) { this.key = key; this.value = value; } public int getKey() { return key; } public int getValue() { return value; } }
And now, let's create our Hash Table:
public class HashTable { private final static int TABLE_SIZE = 64; HashEntry[] table; HashTable() { table = new HashEntry[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; i++) { table[i] = null; } } public int get(int key) { int hash = (key % TABLE_SIZE); while (table[hash] != null && table[hash].getKey() != key) { hash = (hash + 1) % TABLE_SIZE; } if (table[hash] == null) { return -1; } else { return table[hash].getValue(); } } public void put(int key, int value) { int hash = (key % TABLE_SIZE); while (table[hash] != null && table[hash].getKey() != key) { hash = (hash + 1) % TABLE_SIZE; } table[hash] = new HashEntry(key, value); } }
Chaining
First, we define the Nodes of our Linked List.
public class HashNode { private int key; private int value; private HashNode next; HashNode(int key, int value) { this.key = key; this.value = value; this.next = null; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getKey() { return key; } public HashNode getNext() { return next; } public void setNext(HashNode next) { this.next = next; } }
Now, let's create our Hash Table:
public class HashTable { private final static int TABLE_SIZE = 64; HashNode[] table; HashTable() { table = new HashNode[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; i++) { table[i] = null; } } public int get(int key) { int hash = (key % TABLE_SIZE); if (table[hash] == null) { return -1; } else { HashNode entry = table[hash]; while (entry != null && entry.getKey() != key) { entry = entry.getNext(); } if (entry == null) { return -1; } else { return entry.getValue(); } } } public void put(int key, int value) { int hash = (key % TABLE_SIZE); if (table[hash] == null) { table[hash] = new HashNode(key, value); } else { HashNode entry = table[hash]; while (entry.getNext() != null && entry.getKey() != key) { entry = entry.getNext(); } if (entry.getKey() == key) { entry.setValue(value); } else { entry.setNext(new HashNode(key, value)); } } } }
Conclusion
Through this article, we saw what are Hash Tables, how they work and what problem we could have when we use them. It is also good to know that there are many existing well-tested Hash Functions that we can rely on.
1 note · View note
mycodetips · 4 years ago
Text
How to Convert 'int' to 'String' in JAVA
How to Convert ‘int’ to ‘String’ in JAVA
Typecasting, or type conversion, is a method of changing an entity from one data type to another. It is used in computer programming to ensure variables are correctly processed by a function. An example of typecasting is converting an integer to a string. It is important to ensure that the casting is in line with run-time rules as well as compile-time rules in Java. Reference type casting is…
Tumblr media
View On WordPress
0 notes