13

シナリオ:

3 つの要素を持つリストの場合:

[A, B, C]

何度でも循環アクセスできます。

また、各要素のアクセス回数を記録する追加のカウント機能があります。

たとえば、7 回アクセスすると、次のように返されます。

[A, B, C, A, B, C, A]

そして、各要素のアクセス数を次のようにします。

+–––––––––––+–––––––––––––––+
|  Element  |  Access count |
+–––––––––––––––––––––––––––+
|     A     |       3       |
+–––––––––––––––––––––––––––+
|     B     |       2       |
+–––––––––––––––––––––––––––+
|     C     |       2       |
+–––––––––––+–––––––––––––––+

フィルタリングする必要がある要素リストを呼び出し元が指定できるようにする別の追加関数を追加します。例として 7 回のアクセスを引き続き使用し、フィルタリングします[C]

[A, B, A, B, A, B, A]
+–––––––––––+–––––––––––––––+
|  Element  |  Access count |
+–––––––––––––––––––––––––––+
|     A     |       4       |
+–––––––––––––––––––––––––––+
|     B     |       3       |
+–––––––––––––––––––––––––––+
|     C     |       0       |
+–––––––––––+–––––––––––––––+

そして、その後の呼び出しでgetNextOne()は、アクセス数が少ないものを常にフェッチする必要があります。負荷分散されたアクセス カウントの実装をシミュレートします。したがって、2 番目の呼び出し元が 10 回アクセスしようとすると、次のように返されます。

[C, C, C, B, C, A, B, C, A, B, C, A]
+–––––––––––+–––––––––––––––+
|  Element  |  Access count |
+–––––––––––––––––––––––––––+
|     A     |       7       |
+–––––––––––––––––––––––––––+
|     B     |       6       |
+–––––––––––––––––––––––––––+
|     C     |       6       |
+–––––––––––+–––––––––––––––+
4

3 に答える 3

27

Guava は、Iterables.cycle()と for count を組み合わせて提供しMultisetます。これで完了です。

package com.stackoverflow.so22869350;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;

import java.util.Iterator;
import java.util.List;

public class Circular<T> {
    private final Multiset<T> counter;
    private final Iterator<T> elements;

    public Circular(final List<T> elements) {
        this.counter = HashMultiset.create();
        this.elements = Iterables.cycle(elements).iterator();
    }

    public T getOne() {
        final T element = this.elements.next();
        this.counter.add(element);
        return element;
    }

    public int getCount(final T element) {
        return this.counter.count(element);
    }

    public static void main(final String[] args) {
        final Circular<String> circular =
                new Circular<>(Lists.newArrayList("A", "B", "C"));
        for (int i = 0; i < 7; i++) {
            System.out.println(circular.getOne());
        }
        System.out.println("Count for A: " + circular.getCount("A"));
    }
}

出力:

A
B
C
A
B
C
A
Count for A: 3

注意:適切なequals/ hashCodefor typeを持つように注意してくださいT

于 2014-04-04T17:48:03.270 に答える
2

単一の に基づいて循環アクセス リストを実装できますTreeMap

ツリーマップ
Integerアクセス要求数
価値 List<T>アクセスが要求されたオブジェクトのリスト

クラスCircularList<T>

メソッド このコードは、ライブラリを追加しなくても Java 7 で動作します
getOne()
Tアクセス要求の数が最も少ない最初の要素を返す
getOne(List<T> フィルター)
Tフィルタに含まれていないアクセス要求の数が最も少ない最初の要素を返します
getOne(T filterIn)
Tフィルタリングされた要素を返す
getCount(T要素)
int検索要素のアクセス要求の数を返す、または-1そのような要素がない場合
状態()
Stringマップの現在のステータスを返す

オンラインでお試しください!

public class CircularList<T> {
    private final TreeMap<Integer, List<T>> elements = new TreeMap<>();

    /**
     * @param list required.
     */
    public CircularList(List<T> list) {
        if (list == null || list.size() == 0) return;
        this.elements.put(0, new ArrayList<>(list));
    }

    /**
     * @return the first element with the least number of access requests.
     */
    public synchronized T getOne() {
        // pull out the entry with the least number of access requests
        Map.Entry<Integer, List<T>> entry = this.elements.pollFirstEntry();
        Integer key = entry.getKey();
        List<T> value = entry.getValue();
        // pull out the first element from the list
        T element = value.remove(0);
        // if there is something left in the list, then put it back
        if (value.size() > 0) this.elements.put(key, value);
        // take the next list with greater number of access requests
        List<T> newValue = this.elements.get(key + 1);
        // create it if it doesn't exist
        if (newValue == null) newValue = new ArrayList<>();
        // add the current element to this list
        newValue.add(element);
        // update the map
        this.elements.put(key + 1, newValue);
        // return the first element with the least number of access requests
        return element;
    }

    /**
     * @param filter elements list that should be filtered.
     * @return the first element with the least number of
     * access requests that is not contained in the filter.
     */
    public synchronized T getOne(List<T> filter) {
        // incorrect filter is not applied
        if (filter == null || filter.size() == 0) return getOne();
        Integer key = -1;
        List<T> value;
        T element = null;
        // iterate over the entries of the map
        for (Map.Entry<Integer, List<T>> entry : this.elements.entrySet()) {
            key = entry.getKey();
            value = entry.getValue();
            element = null;
            // iterate over the elements of the list
            for (T el : value) {
                // the first element not contained in the filter
                if (!filter.contains(el)) {
                    element = el;
                    // remove this element from the list
                    value.remove(el);
                    // if there is nothing left in the list, remove the entry
                    if (value.size() == 0) this.elements.remove(key);
                    break;
                }
            }
            // if the element is found
            if (element != null) break;
        }
        // if no element is found, no filter is applied
        if (element == null) return getOne();
        // take the next list with greater number of access requests
        List<T> newValue = this.elements.get(key + 1);
        // create it if it doesn't exist
        if (newValue == null) newValue = new ArrayList<>();
        // add the current element to this list
        newValue.add(element);
        // update the map
        this.elements.put(key + 1, newValue);
        // return the first element with the least number of access requests
        return element;
    }

    /**
     * @param filterIn element that should be filtered.
     * @return the filtered element.
     */
    public synchronized T getOne(T filterIn) {
        // incorrect filter is not applied
        if (filterIn == null) return getOne();
        // iterate over the entries of the map
        for (Map.Entry<Integer, List<T>> entry : this.elements.entrySet()) {
            Integer key = entry.getKey();
            List<T> value = entry.getValue();
            // iterate over the elements of the list
            for (T element : value) {
                // if element is found
                if (filterIn.equals(element)) {
                    // remove this element from the list
                    value.remove(element);
                    // if there is nothing left in the list, remove the entry
                    if (value.size() == 0) this.elements.remove(key);
                    // take the next list with greater number of access requests
                    List<T> newValue = this.elements.get(key + 1);
                    // create it if it doesn't exist
                    if (newValue == null) newValue = new ArrayList<>();
                    // add the current element to this list
                    newValue.add(element);
                    // update the map
                    this.elements.put(key + 1, newValue);
                    // return filtered element
                    return element;
                }
            }
        }
        // if no element is found, no filter is applied
        return getOne();
    }

    /**
     * Search for the element in the lists of the map.
     *
     * @param element search element.
     * @return the number of access requests of the
     * search element, or -1 if there is no such element.
     */
    public int getCount(T element) {
        for (Map.Entry<Integer, List<T>> entry : this.elements.entrySet()) {
            if (entry.getValue().contains(element)) {
                return entry.getKey();
            }
        }
        return -1;
    }

    /**
     * @return the current status of the map.
     */
    public String status() {
        return elements.toString();
    }

    @Override
    public String toString() {
        return elements.toString();
    }
}
// Test
public static void main(String[] args) {
    CircularList<String> list =
            new CircularList<>(Arrays.asList("A", "B", "C", "D"));
    System.out.println(list); // {0=[A, B, C, D]}
    for (int i = 0; i < 10; i++) {
        System.out.print(list.getOne(Arrays.asList("A")) + " ");
        // B C D B C D B C D B
    }
    System.out.println();
    System.out.println(list.status()); // {0=[A], 3=[C, D], 4=[B]}
    for (int i = 0; i < 3; i++) {
        System.out.print(list.getOne("D") + " ");
        // D D D
    }
    System.out.println();
    System.out.println(list.status()); // {0=[A], 3=[C], 4=[B], 6=[D]}
    for (int i = 0; i < 14; i++) {
        System.out.print(list.getOne() + " ");
        // A A A C A B C A B C A D B C
    }
    System.out.println();
    System.out.println(list.status()); // {6=[A], 7=[D, B, C]}
    System.out.println(list.getCount("A")); // 6
    System.out.println(list.getCount("E")); // -1
}
于 2021-04-09T18:33:06.653 に答える