Java Program to Perform Searching Based on Locality of Reference

This is a java program to perform searching based on locality of refernce. The Locality of Reference principle says that if an element of a list is accessed it might also be accessed in near future. So we store it to the front of the list.

Here is the source code of the Java Program to Perform Searching Based on Locality of Reference. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Follows recently accessed elements should be referred more so kept on top of
 * the list
 */
package com.maixuanviet.combinatorial;
 
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
 
public class LocalityBasedSearching
{
    public static void main(String[] args)
    {
        List<Integer> items = new LinkedList<Integer>();
        Integer n = 10;
        Scanner sc = new Scanner(System.in);
        Random rand = new Random();
        while (n > 0)
        {
            items.add(rand.nextInt(100));
            n--;
        }
        System.out.println(items.toString());
        boolean flag = true;
        boolean found = false;
        Integer numberofInstance;
        while (flag == true)
        {
            numberofInstance = 0;
            System.out.println("Enter the element to find: ");
            Integer search = sc.nextInt();
            for (int i = 0; i < items.size(); i++)
            {
                if (items.get(i).equals(search))
                {
                    found = true;
                    System.out.println("Element found at index " + i
                            + "\nReordering list...");
                    // Integer temp = items.get(numberofInstance);
                    // items.set(numberofInstance, search);
                    items.add(numberofInstance, search);
                    items.remove(i + 1);
                    // items.set(i, temp);
                    System.out.println("Reordered list: " + items.toString());
                    numberofInstance++;
                    // break;
                }
            }
            if (found == false)
            {
                System.out.println("No such element found.");
            }
            System.out.println("Do you want to continue? <true>/<false>");
            flag = sc.nextBoolean();
        }
        sc.close();
    }
}

Output:

$ javac LocalityBasedSearching.java
$ java LocalityBasedSearching
 
[52, 94, 58, 8, 78, 0, 30, 81, 16, 58]
Enter the element to find: 
8
Element found at index 3
Reordering list...
Reordered list: [8, 52, 94, 58, 78, 0, 30, 81, 16, 58]
Do you want to continue? <true>/<false>
true
Enter the element to find: 
58
Element found at index 3
Reordering list...
Reordered list: [58, 8, 52, 94, 78, 0, 30, 81, 16, 58]
Element found at index 9
Reordering list...
Reordered list: [58, 58, 8, 52, 94, 78, 0, 30, 81, 16]
Do you want to continue? <true>/<false>
true
Enter the element to find: 
94
Element found at index 4
Reordering list...
Reordered list: [94, 58, 58, 8, 52, 78, 0, 30, 81, 16]
Do you want to continue? <true>/<false>
false

Related posts:

Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
The Modulo Operator in Java
Java Program to Implement RoleList API
Java Byte Array to InputStream
Giới thiệu HATEOAS
HttpClient Timeout
Jackson Annotation Examples
A Guide to the Java LinkedList
Java Program for Topological Sorting in Graphs
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Using JWT with Spring Security OAuth (legacy stack)
Java Program to Implement IdentityHashMap API
Create a Custom Auto-Configuration with Spring Boot
Java Program to Implement Disjoint Sets
ETags for REST with Spring
What is Thread-Safety and How to Achieve it?
So sánh HashMap và HashSet trong Java
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Merging Two Maps with Java 8
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
How to Round a Number to N Decimal Places in Java
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Implement Kosaraju Algorithm
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Guide to the Java Clock Class
Consuming RESTful Web Services
Java Program to Implement DelayQueue API
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Hướng dẫn Java Design Pattern – Composite
Guide to Spring Cloud Kubernetes
Spring Data JPA Delete and Relationships