class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a List of Integers
List<Integer>
myList = Arrays.asList(1, 2, 3, 4, 5);
// Using Ints.toArray() method to convert
// a List or Set of Integer to an array
// of Int
int[] arr = Ints.toArray(myList);
// Displaying an array containing each
// value of collection,
// converted to a int value
System.out.println("Array from given List: "
+ Arrays.toString(arr));
}
}
private void testInts() {
int[] intArray = {1,2,3,4,5,6,7,8,9};
//convert array of primitives to array of objects
List<Integer> objectArray = Ints.asList(intArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
intArray = Ints.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< intArray.length ; i++) {
System.out.print(intArray[i] + " ");
}
System.out.println("]");
//check if element is present in the list of primitives or not
System.out.println("5 is in list? " + Ints.contains(intArray, 5));
//Returns the minimum
System.out.println("Min: " + Ints.min(intArray));
//Returns the maximum
System.out.println("Max: " + Ints.max(intArray));
//get the byte array from an integer
byte[] byteArray = Ints.toByteArray(20000);
for(int i = 0; i< byteArray.length ; i++) {
System.out.print(byteArray[i] + " ");
}
}
class ArrayUtils
{
// Java program to demonstrate Guava's Ints.toArray() method
public static void main(String args[])
{
List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5);
int[] primitive = Ints.toArray(ints);
System.out.println(Arrays.toString(primitive));
}
}
public static void main(String[] args) {
List<Integer> list = ...;
int[] values = Ints.toArray(list);
}
public void givenUsingGuava_whenListConvertedToArray_thenCorrect() {
List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
int[] targetArray = Ints.toArray(sourceList);
}
private <T> ValueExtractor provideExtractorForValue(Class<T> clazz, int target, List<String> chainOfProperties) {
Class<?> propertyClass = clazz;
List<Integer> indices = Lists.newArrayList();
for (String property : chainOfProperties) {
Field field;
try {
field = clazz.getDeclaredField(property);
} catch (NoSuchFieldException e) {
throw new InvalidQueryException(e);
}
PortableProperty portablePropertyAnnotation = field.getAnnotation(PortableProperty.class);
if (portablePropertyAnnotation == null) {
throw new InvalidQueryException("");
}
// TODO add support for customs codecs some day ;)
int index = portablePropertyAnnotation.value();
indices.add(index);
propertyClass = field.getDeclaringClass();
}
return new PofExtractor<>(propertyClass, new SimplePofPath(Ints.toArray(indices)), target);
}
private Object toArray(Class<?> componentType, List<Object> values) {
if (componentType == boolean.class) {
return Booleans.toArray((Collection) values);
} else if (componentType == byte.class) {
return Bytes.toArray((Collection) values);
} else if (componentType == short.class) {
return Shorts.toArray((Collection) values);
} else if (componentType == int.class) {
return Ints.toArray((Collection) values);
} else if (componentType == long.class) {
return Longs.toArray((Collection) values);
} else if (componentType == float.class) {
return Floats.toArray((Collection) values);
} else if (componentType == double.class) {
return Doubles.toArray((Collection) values);
} else if (componentType == char.class) {
return Chars.toArray((Collection) values);
}
return values.toArray((Object[]) Array.newInstance(componentType, values.size()));
}
private int[] getZoomLevels() {
final List<Integer> zoomLevels = new ArrayList<>();
final File[] files = directory.listFiles();
if (files == null) {
return new int[]{};
}
final Pattern pattern = Pattern.compile("^([0-9]|1[0-9]|2[0-2])$");
for (final File file : files) {
final String fileName = file.getName();
final Matcher matcher = pattern.matcher(fileName);
if (matcher.matches()) {
final int value = Integer.parseInt(matcher.group());
zoomLevels.add(value);
}
}
final int[] result = Ints.toArray(zoomLevels);
Arrays.sort(result);
return result;
}
public void testScale_index_compute_intVarargs() {
int[] dataset = Ints.toArray(SIXTEEN_SQUARES_INTEGERS);
assertThat(Quantiles.scale(10).index(1).compute(dataset))
.isWithin(ALLOWED_ERROR)
.of(SIXTEEN_SQUARES_DECILE_1);
assertThat(dataset).asList().isEqualTo(SIXTEEN_SQUARES_INTEGERS);
}
public void testScale_indexes_varargs_compute_intVarargs() {
int[] dataset = Ints.toArray(SIXTEEN_SQUARES_INTEGERS);
assertThat(Quantiles.scale(10).indexes(0, 10, 5, 1, 8, 1).compute(dataset))
.comparingValuesUsing(QUANTILE_CORRESPONDENCE)
.containsExactly(
0, SIXTEEN_SQUARES_MIN,
10, SIXTEEN_SQUARES_MAX,
5, SIXTEEN_SQUARES_MEDIAN,
1, SIXTEEN_SQUARES_DECILE_1,
8, SIXTEEN_SQUARES_DECILE_8);
assertThat(dataset).asList().isEqualTo(SIXTEEN_SQUARES_INTEGERS);
}