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:
How to Read a File in Java
Java Program to Implement Bloom Filter
REST Pagination in Spring
Java Program to Implement Weight Balanced Tree
The StackOverflowError in Java
Java Program to add two large numbers using Linked List
How to Find an Element in a List with Java
A Guide to TreeMap in Java
Mapping a Dynamic JSON Object with Jackson
Guide to java.util.Formatter
Java Program to Implement Quick sort
How to Return 404 with Spring WebFlux
OAuth2 Remember Me with Refresh Token
Uploading MultipartFile with Spring RestTemplate
How to Get a Name of a Method Being Executed?
Guide to WeakHashMap in Java
Spring Autowiring of Generic Types
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Creating Docker Images with Spring Boot
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
A Guide to JUnit 5 Extensions
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Lớp Arrarys trong Java (Arrays Utility Class)
Mockito and JUnit 5 – Using ExtendWith
Java Program to Implement Hash Tables Chaining with Binary Trees
Hướng dẫn Java Design Pattern – Dependency Injection
Number Formatting in Java
Java Program to Describe the Representation of Graph using Incidence List
Introduction to Spring Data JPA
Java Program to Perform Uniform Binary Search