実現したいこと
PriorityQueueのremove(Object o)の計算量を落としたい。
そのためにPackagePrivateになっている変数をリフレクションで取得したい。
最終的にリフレクションを使うのはあきらめてTreeMapを利用して自作しました。
前提
やりたいことは上記のとおりです。
ソースを覗いたらremove(Object o)で使われているindexOf()が片っ端から配列の要素を見ているみたいなので、適切に二分探索するようにオーバーライドしたらいいのではと考えました。
indexOf()の中で使われている変数(transient Object[] queue と int size)がPackagePrivateなので、これを取得して使えるようにしたいです。
発生している問題・エラーメッセージ
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field transient java.lang.Object[] java.util.PriorityQueue.queue accessible: module java.base does not "opens java.util" to unnamed module @85ede7b
下記のソースではsetAccessible(true)の箇所でエラーが出ています。
該当のソースコード
Java
1import java.lang.reflect.Field; 2import java.util.Comparator; 3import java.util.PriorityQueue; 4 5public class test2 { 6 7 public static void main(String[] args) throws Exception{ 8 MyPriorityQueue<Integer> q1=new MyPriorityQueue<>((n1,n2)->n2-n1); 9 MyPriorityQueue<Integer> q2=new MyPriorityQueue<>(); 10 q1.add(1); 11 q1.add(2); 12 q1.add(3); 13 q2.add(1); 14 q2.add(2); 15 q2.add(3); 16 q1.indexOf(1); 17 q2.indexOf(1); 18 System.out.println(q1.mySize); 19 System.out.println(q2.mySize); 20 } 21 22} 23 24class MyPriorityQueue<E> extends PriorityQueue<E> { 25 26 PriorityQueue<E> pq=new PriorityQueue<>(); 27 Class<? extends Object> clazz= pq.getClass(); 28 Object[] myQueue; 29 int mySize; 30 31 public MyPriorityQueue() { 32 super(); 33 getPrivateField(); 34 } 35 36 public MyPriorityQueue(Comparator<? super E> comparator) { 37 super(comparator); 38 getPrivateField(); 39 } 40 41 public void getPrivateField(){ 42 try { 43 Field f1 = clazz.getDeclaredField("queue"); 44 f1.setAccessible(true); 45 Field f2=clazz.getDeclaredField("size"); 46 f2.setAccessible(true); 47 } catch (NoSuchFieldException | SecurityException e) { 48 } 49 50 } 51 52 public int indexOf(Object o) { 53 try { 54 myQueue = (Object[]) clazz.getDeclaredField("queue").get(pq); 55 mySize = (int) clazz.getDeclaredField("size").get(pq); 56 System.out.println(mySize); 57 } catch (Exception e) { 58 System.out.println("Error"); 59 } 60 if (o != null) { 61 final Object[] es = myQueue; 62 for (int i = 0, n = mySize; i < n; i++) 63 if (o.equals(es[i])) 64 return i; 65 } 66 return -1; 67 } 68}
試したこと
見様見真似で色々試しましたが、リフレクションの使い方を理解できていないのでエラーの内容がわかりません。
補足情報(FW/ツールのバージョンなど)
あくまでremove(Object o)の計算量を落とすことが目的なので、もしほかに実現できる方法があればそれでも構わないのでよろしくお願いします。
TreeMapを利用してPriorityQueueもどきを自作しました。
Java
1class MyPriorityQueue<K> { 2 TreeMap<K, Integer> map = new TreeMap<>(); 3 4 public void add(K key) { 5 if (map.containsKey(key)) { 6 int value = map.get(key); 7 map.put(key, value + 1); 8 } else { 9 map.put(key, 1); 10 } 11 } 12 13 public boolean remove(K key) { 14 if (map.containsKey(key)) { 15 int value = map.get(key); 16 if (value == 1) { 17 map.remove(key); 18 } else { 19 map.put(key, value - 1); 20 } 21 return true; 22 } else { 23 return false; 24 } 25 } 26 27 public K poll() { 28 if (!map.isEmpty()) { 29 K fk = map.firstKey(); 30 remove(fk); 31 return fk; 32 } else { 33 return null; 34 } 35 } 36 37 public K peek() { 38 if (!map.isEmpty()) { 39 return map.firstKey(); 40 } else { 41 return null; 42 } 43 } 44} 45

回答3件
あなたの回答
tips
プレビュー