當前位置: 首頁>>代碼示例>>Java>>正文


Java RandomAccess類代碼示例

本文整理匯總了Java中java.util.RandomAccess的典型用法代碼示例。如果您正苦於以下問題:Java RandomAccess類的具體用法?Java RandomAccess怎麽用?Java RandomAccess使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


RandomAccess類屬於java.util包,在下文中一共展示了RandomAccess類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: checkedListByCopy

import java.util.RandomAccess; //導入依賴的package包/類
/**
 * Create a typesafe copy of a raw list.
 * @param rawList an unchecked list
 * @param type the desired supertype of the entries
 * @param strict true to throw a <code>ClassCastException</code> if the raw list has an invalid entry,
 *               false to skip over such entries (warnings may be logged)
 * @return a typed list guaranteed to contain only entries assignable
 *         to the named type (or they may be null)
 * @throws ClassCastException if some entry in the raw list was not well-typed, and only if <code>strict</code> was true
 */
public static <E> List<E> checkedListByCopy(List rawList, Class<E> type, boolean strict) throws ClassCastException {
    List<E> l = (rawList instanceof RandomAccess) ? new ArrayList<E>(rawList.size()) : new LinkedList<E>();
    Iterator it = rawList.iterator();
    while (it.hasNext()) {
        Object e = it.next();
        try {
            l.add(type.cast(e));
        } catch (ClassCastException x) {
            if (strict) {
                throw x;
            } else {
                LOG.log(Level.WARNING, "Element {0} not assignable to {1}", new Object[] {e, type});
            }
        }
    }
    return l;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:28,代碼來源:NbCollections.java

示例2: testNewListMultimap

import java.util.RandomAccess; //導入依賴的package包/類
public void testNewListMultimap() {
  CountingSupplier<LinkedList<Integer>> factory = new ListSupplier();
  Map<Color, Collection<Integer>> map = Maps.newTreeMap();
  ListMultimap<Color, Integer> multimap =
      Multimaps.newListMultimap(map, factory);
  assertEquals(0, factory.count);
  multimap.putAll(Color.BLUE, asList(3, 1, 4, 1));
  assertEquals(1, factory.count);
  multimap.putAll(Color.RED, asList(2, 7, 1, 8));
  assertEquals(2, factory.count);
  assertEquals("{BLUE=[3, 1, 4, 1], RED=[2, 7, 1, 8]}", multimap.toString());
  assertFalse(multimap.get(Color.BLUE) instanceof RandomAccess);

  assertTrue(multimap.keySet() instanceof SortedSet);
  assertTrue(multimap.asMap() instanceof SortedMap);
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:17,代碼來源:MultimapsTest.java

示例3: subListImpl

import java.util.RandomAccess; //導入依賴的package包/類
/**
 * An implementation of {@link List#subList(int, int)}.
 */
static <E> List<E> subListImpl(final List<E> list, int fromIndex, int toIndex) {
  List<E> wrapper;
  if (list instanceof RandomAccess) {
    wrapper =
        new RandomAccessListWrapper<E>(list) {
          @Override
          public ListIterator<E> listIterator(int index) {
            return backingList.listIterator(index);
          }

          private static final long serialVersionUID = 0;
        };
  } else {
    wrapper =
        new AbstractListWrapper<E>(list) {
          @Override
          public ListIterator<E> listIterator(int index) {
            return backingList.listIterator(index);
          }

          private static final long serialVersionUID = 0;
        };
  }
  return wrapper.subList(fromIndex, toIndex);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:29,代碼來源:Lists.java

示例4: testAsList1Small

import java.util.RandomAccess; //導入依賴的package包/類
public void testAsList1Small() {
  List<String> list = Lists.asList("foo", new String[0]);
  assertThat(list).contains("foo");
  assertEquals(1, list.size());
  assertIndexIsOutOfBounds(list, -1);
  assertEquals("foo", list.get(0));
  assertIndexIsOutOfBounds(list, 1);
  assertTrue(list instanceof RandomAccess);

  new IteratorTester<String>(3, UNMODIFIABLE, singletonList("foo"),
      IteratorTester.KnownOrder.KNOWN_ORDER) {
    @Override protected Iterator<String> newTargetIterator() {
      return Lists.asList("foo", new String[0]).iterator();
    }
  }.test();
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:17,代碼來源:ListsTest.java

示例5: testAsList2Small

import java.util.RandomAccess; //導入依賴的package包/類
@GwtIncompatible // SerializableTester
public void testAsList2Small() {
  List<String> list = Lists.asList("foo", "bar", new String[0]);
  assertThat(list).containsExactly("foo", "bar").inOrder();
  assertEquals(2, list.size());
  assertIndexIsOutOfBounds(list, -1);
  assertEquals("foo", list.get(0));
  assertEquals("bar", list.get(1));
  assertIndexIsOutOfBounds(list, 2);
  SerializableTester.reserializeAndAssert(list);
  assertTrue(list instanceof RandomAccess);

  new IteratorTester<String>(5, UNMODIFIABLE, asList("foo", "bar"),
      IteratorTester.KnownOrder.KNOWN_ORDER) {
    @Override protected Iterator<String> newTargetIterator() {
      return Lists.asList("foo", "bar", new String[0]).iterator();
    }
  }.test();
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:20,代碼來源:ListsTest.java

示例6: testPartitionRandomAccessTrue

import java.util.RandomAccess; //導入依賴的package包/類
@GwtIncompatible // ArrayList.subList doesn't implement RandomAccess in GWT.
public void testPartitionRandomAccessTrue() {
  List<Integer> source = asList(1, 2, 3);
  List<List<Integer>> partitions = Lists.partition(source, 2);

  assertTrue("partition should be RandomAccess, but not: "
      + partitions.getClass(),
      partitions instanceof RandomAccess);

  assertTrue("partition[0] should be RandomAccess, but not: "
      + partitions.get(0).getClass(),
      partitions.get(0) instanceof RandomAccess);

  assertTrue("partition[1] should be RandomAccess, but not: "
      + partitions.get(1).getClass(),
      partitions.get(1) instanceof RandomAccess);
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:18,代碼來源:ListsTest.java

示例7: subListImpl

import java.util.RandomAccess; //導入依賴的package包/類
/**
 * An implementation of {@link List#subList(int, int)}.
 */
static <E> List<E> subListImpl(
    final List<E> list, int fromIndex, int toIndex) {
  List<E> wrapper;
  if (list instanceof RandomAccess) {
    wrapper = new RandomAccessListWrapper<E>(list) {
      @Override public ListIterator<E> listIterator(int index) {
        return backingList.listIterator(index);
      }

      private static final long serialVersionUID = 0;
    };
  } else {
    wrapper = new AbstractListWrapper<E>(list) {
      @Override public ListIterator<E> listIterator(int index) {
        return backingList.listIterator(index);
      }

      private static final long serialVersionUID = 0;
    };
  }
  return wrapper.subList(fromIndex, toIndex);
}
 
開發者ID:s-store,項目名稱:s-store,代碼行數:26,代碼來源:Lists.java

示例8: equalsImpl

import java.util.RandomAccess; //導入依賴的package包/類
/**
 * An implementation of {@link List#equals(Object)}.
 */
static boolean equalsImpl(List<?> thisList, @Nullable Object other) {
  if (other == checkNotNull(thisList)) {
    return true;
  }
  if (!(other instanceof List)) {
    return false;
  }
  List<?> otherList = (List<?>) other;
  int size = thisList.size();
  if (size != otherList.size()) {
    return false;
  }
  if (thisList instanceof RandomAccess && otherList instanceof RandomAccess) {
    // avoid allocation and use the faster loop
    for (int i = 0; i < size; i++) {
      if (!Objects.equal(thisList.get(i), otherList.get(i))) {
        return false;
      }
    }
    return true;
  } else {
    return Iterators.elementsEqual(thisList.iterator(), otherList.iterator());
  }
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:28,代碼來源:Lists.java

示例9: testUnmodifiableLinkedListMultimapRandomAccess

import java.util.RandomAccess; //導入依賴的package包/類
public void testUnmodifiableLinkedListMultimapRandomAccess() {
  ListMultimap<String, Integer> delegate = LinkedListMultimap.create();
  delegate.put("foo", 1);
  delegate.put("foo", 3);
  ListMultimap<String, Integer> multimap = Multimaps.unmodifiableListMultimap(delegate);
  assertFalse(multimap.get("foo") instanceof RandomAccess);
  assertFalse(multimap.get("bar") instanceof RandomAccess);
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:9,代碼來源:MultimapsTest.java

示例10: testPartitionRandomAccessFalse

import java.util.RandomAccess; //導入依賴的package包/類
public void testPartitionRandomAccessFalse() {
  List<Integer> source = Lists.newLinkedList(asList(1, 2, 3));
  List<List<Integer>> partitions = Lists.partition(source, 2);
  assertFalse(partitions instanceof RandomAccess);
  assertFalse(partitions.get(0) instanceof RandomAccess);
  assertFalse(partitions.get(1) instanceof RandomAccess);
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:8,代碼來源:ListsTest.java

示例11: testPartitionRandomAccess

import java.util.RandomAccess; //導入依賴的package包/類
@GwtIncompatible // ?
// TODO: Figure out why this is failing in GWT.
public void testPartitionRandomAccess() {
  Iterator<Integer> source = asList(1, 2, 3).iterator();
  Iterator<List<Integer>> partitions = Iterators.partition(source, 2);
  assertTrue(partitions.next() instanceof RandomAccess);
  assertTrue(partitions.next() instanceof RandomAccess);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:9,代碼來源:IteratorsTest.java

示例12: testLeastOfIterable_simple_n_withNullElement

import java.util.RandomAccess; //導入依賴的package包/類
public void testLeastOfIterable_simple_n_withNullElement() {
  List<Integer> list = Arrays.asList(3, 4, 5, null, -1);
  List<Integer> result = Ordering.natural().nullsLast().leastOf(list, list.size());
  assertTrue(result instanceof RandomAccess);
  assertListImmutable(result);
  assertEquals(Arrays.asList(-1, 3, 4, 5, null), result);
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:8,代碼來源:OrderingTest.java

示例13: testLeastOfIterator_empty_0

import java.util.RandomAccess; //導入依賴的package包/類
public void testLeastOfIterator_empty_0() {
  List<Integer> result = numberOrdering.leastOf(
      Iterators.<Integer>emptyIterator(), 0);
  assertTrue(result instanceof RandomAccess);
  assertListImmutable(result);
  assertEquals(ImmutableList.<Integer>of(), result);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:8,代碼來源:OrderingTest.java

示例14: testLeastOfIterator_simple_0

import java.util.RandomAccess; //導入依賴的package包/類
public void testLeastOfIterator_simple_0() {
  List<Integer> result = numberOrdering.leastOf(
      Iterators.forArray(3, 4, 5, -1), 0);
  assertTrue(result instanceof RandomAccess);
  assertListImmutable(result);
  assertEquals(ImmutableList.<Integer>of(), result);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:8,代碼來源:OrderingTest.java

示例15: testLeastOfIterator_simple_1

import java.util.RandomAccess; //導入依賴的package包/類
public void testLeastOfIterator_simple_1() {
  List<Integer> result = numberOrdering.leastOf(
      Iterators.forArray(3, 4, 5, -1), 1);
  assertTrue(result instanceof RandomAccess);
  assertListImmutable(result);
  assertEquals(ImmutableList.of(-1), result);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:8,代碼來源:OrderingTest.java


注:本文中的java.util.RandomAccess類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。