Program for Least Recently Used ( LRU ) Page Replacement algorithm | Java program for page replacement algorithms ; Driver method ; To represent set of current pages . We use an Arraylist ; Insert it into set if not present already which represents page fault ; Check if the set can hold equal pages ; Increment page faults ; Remove the indexes page ; insert the current page
import java . util . ArrayList ; public class LRU { public static void main ( String [ ] args ) { int capacity = 4 ; int arr [ ] = { 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 } ; ArrayList < Integer > s = new ArrayList < > ( capacity ) ; int count = 0 ; int page_faults = 0 ; for ( int i : arr ) { if ( ! s . contains ( i ) ) { if ( s . size ( ) == capacity ) { s . remove ( 0 ) ; s . add ( capacity - 1 , i ) ; } else s . add ( count , i ) ; page_faults ++ ; ++ count ; } else { s . remove ( ( Object ) i ) ; s . add ( s . size ( ) , i ) ; } } System . out . println ( page_faults ) ; } }