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:
New Features in Java 11
Spring Boot - Quick Start
Java Program to Implement Min Heap
Java Program to Implement Interval Tree
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
A Guide to Queries in Spring Data MongoDB
Giới thiệu thư viện Apache Commons Chain
Write/Read cookies using HTTP and Read a file from the internet
Check If Two Lists are Equal in Java
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Spring Boot - Cloud Configuration Client
Deploy a Spring Boot App to Azure
Spring Data Reactive Repositories with MongoDB
Java Program to Find Transpose of a Graph Matrix
Service Registration with Eureka
Java Program to Find Path Between Two Nodes in a Graph
Default Password Encoder in Spring Security 5
Java Program to Check Cycle in a Graph using Graph traversal
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java 9 Stream API Improvements
Java Program to Implement Coppersmith Freivald’s Algorithm
Java Program to Perform Partial Key Search in a K-D Tree
Build a REST API with Spring and Java Config
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
The HttpMediaTypeNotAcceptableException in Spring MVC
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Hướng dẫn Java Design Pattern – Service Locator
Jackson Exceptions – Problems and Solutions
Spring MVC and the @ModelAttribute Annotation
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
StringBuilder vs StringBuffer in Java