Example usage for java.util Collections max

List of usage examples for java.util Collections max

Introduction

In this page you can find the example usage for java.util Collections max.

Prototype

public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) 

Source Link

Document

Returns the maximum element of the given collection, according to the natural ordering of its elements.

Usage

From source file:br.net.fabiozumbi12.RedProtect.hooks.MojangUUIDs.java

public static String getName(String UUID) {
    try {//from w  w  w  .  jav  a  2 s.c  o  m
        URL url = new URL("https://api.mojang.com/user/profiles/" + UUID.replaceAll("-", "") + "/names");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String line = in.readLine();
        if (line == null) {
            return null;
        }
        JSONArray array = (JSONArray) new JSONParser().parse(line);
        HashMap<Long, String> names = new HashMap<Long, String>();
        String name = "";
        for (Object profile : array) {
            JSONObject jsonProfile = (JSONObject) profile;
            if (jsonProfile.containsKey("changedToAt")) {
                names.put((long) jsonProfile.get("changedToAt"), (String) jsonProfile.get("name"));
                continue;
            }
            name = (String) jsonProfile.get("name");
        }
        if (!names.isEmpty()) {
            Long key = Collections.max(names.keySet());
            return names.get(key);
        } else {
            return name;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.yimidida.shards.strategy.exit.impl.AggregateExitOperation.java

@Override
public List<Object> apply(List<Object> result) {
    if (result.size() == 0) {
        return Lists.newArrayList((Object) 0);
    }// w  w w  .jav a 2s.c om

    //      String className = result.get(0).getClass().getName();

    List<Object> nonNullResults = ExitOperationUtils.getNonNullList(result);

    switch (aggregate) {
    case MAX:
        return Collections
                .<Object>singletonList(Collections.max(ExitOperationUtils.getComparableList(nonNullResults)));
    case MIN:
        return Collections
                .<Object>singletonList(Collections.min(ExitOperationUtils.getComparableList(nonNullResults)));
    case SUM:
        return Collections.<Object>singletonList(getSum(nonNullResults, null).intValue());
    default:
        log.error("Aggregation Projection is unsupported: " + aggregate);
        throw new UnsupportedOperationException("Aggregation Projection is unsupported: " + aggregate);
    }
}

From source file:org.opensourcebank.batch.partition.HazelcastMapPartitioner.java

public Map<String, ExecutionContext> partition(int gridSize) {

    Map<Long, Object> itemsMap = Hazelcast.getMap(mapName);
    Set<Long> itemsIds = itemsMap.keySet();

    long min = 0;
    long max = 0;

    if (itemsIds.size() > 0) {
        min = Collections.min(itemsIds);
        max = Collections.max(itemsIds);
    }//from   ww  w  .jav a2  s . co m

    long targetSize = (max - min) / gridSize + 1;

    Map<String, ExecutionContext> result = new HashMap<String, ExecutionContext>();
    int number = 0;
    long start = min;
    long end = start + targetSize - 1;

    while (start <= max) {

        ExecutionContext value = new ExecutionContext();
        result.put("partition" + number, value);

        if (end >= max) {
            end = max;
        }
        value.putLong("fromId", start);
        value.putLong("toId", end);
        value.putString("mapName", mapName);
        start += targetSize;
        end += targetSize;
        number++;
    }

    return result;
}

From source file:org.duracloud.id.generator.ldap.impl.LdapImpl.java

@Override
public int maxUserId() {
    String base = "ou=people";
    String filter = "objectClass=x-idp-person";
    List<Integer> ids = getIds(base, filter);
    return ids.isEmpty() ? 0 : Collections.max(ids);
}

From source file:org.zkoss.ganttz.data.GanttDate.java

public static GanttDate max(GanttDate... dates) {
    return Collections.max(Arrays.asList(dates));
}

From source file:org.apache.hadoop.mapreduce.lib.partition.TestRehashPartitioner.java

/** test partitioner for patterns */
@Test/*from   w w w.ja  va  2  s.c om*/
public void testPatterns() {
    int results[] = new int[PARTITIONS];
    RehashPartitioner<IntWritable, NullWritable> p = new RehashPartitioner<IntWritable, NullWritable>();
    /* test sequence 4, 8, 12, ... 128 */
    for (int i = 0; i < END; i += STEP) {
        results[p.getPartition(new IntWritable(i), null, PARTITIONS)]++;
    }
    int badbuckets = 0;
    Integer min = Collections.min(Arrays.asList(ArrayUtils.toObject(results)));
    Integer max = Collections.max(Arrays.asList(ArrayUtils.toObject(results)));
    Integer avg = (int) Math.round((max + min) / 2.0);
    System.out.println("Dumping buckets distribution: min=" + min + " avg=" + avg + " max=" + max);
    for (int i = 0; i < PARTITIONS; i++) {
        double var = (results[i] - avg) / (double) (avg);
        System.out.println("bucket " + i + " " + results[i] + " items, variance " + var);
        if (Math.abs(var) > MAX_ERROR)
            badbuckets++;
    }
    System.out.println(badbuckets + " of " + PARTITIONS + " are too small or large buckets");
    assertTrue("too many overflow buckets", badbuckets < PARTITIONS * MAX_BADBUCKETS);
}

From source file:twittervis.GetEmotion.java

/**
 * Find the emotion with the largest relevance value using a created HashMap
 * /* w ww . j  a  v  a  2  s .  c om*/
 * @param e1
 * @param e2
 * @param e3
 * @param e4
 * @param e5
 * @return Final Emotion
 */
public static String getFinalEmotion(double e1, double e2, double e3, double e4, double e5) {
    // Create a map that stores the keys and values of the emotion 
    // analysis results
    HashMap<String, Double> map = new HashMap<>();
    map.put("Joy", e1);
    map.put("Anger", e2);
    map.put("Fear", e3);
    map.put("Sadness", e4);
    map.put("Disgust", e5);

    Map.Entry<String, Double> maxEntry = null;

    // Now look for the variable with the maximum in the created HashMap
    double maxValueInMap = (Collections.max(map.values())); // This will return max value in the Hashmap
    map.entrySet().stream().filter((entry) -> (entry.getValue() == maxValueInMap)).forEachOrdered((entry) -> {
        // System.out.println(entry.getKey());     // Print the key with max value
        finalEmotion = (String) entry.getKey();
    }); // Itrate through HashMap to find the max
    return finalEmotion; // this the emotion with the largest relevance value
}

From source file:org.duracloud.id.generator.ldap.impl.LdapImpl.java

@Override
public int maxGroupId() {
    String base = "ou=groups";
    String filter = "objectClass=x-idp-group";
    List<Integer> ids = getIds(base, filter);
    return ids.isEmpty() ? 0 : Collections.max(ids);
}

From source file:org.springside.modules.utils.collection.CollectionUtil.java

/**
 * ???//w w  w .  j a  v a  2 s .c  om
 */
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
    return Collections.max(coll);
}

From source file:com.gmail.frogocomics.schematic.BiomeWorldV2Object.java

public static BiomeWorldV2Object load(File file) throws IOException {

    BufferedReader settingsReader;

    if (!file.exists()) {
        throw new FileNotFoundException();
    }//from   w  ww .  j  a  v  a2 s  .co m
    settingsReader = new BufferedReader(new FileReader(file));
    int lineNumber = 0;
    String thisLine;
    ArrayList<BiomeWorldObjectBlock> bo2Blocks = new ArrayList<>();

    while ((thisLine = settingsReader.readLine()) != null) {
        lineNumber++;
        if (Pattern.compile("[0-9]").matcher(thisLine.substring(0, 1)).matches()
                || thisLine.substring(0, 1).equalsIgnoreCase("-")) {
            //Example: -1,-1,5:18.4
            // x,z,y:id.data
            String[] location = thisLine.split(":")[0].split(",");
            String[] block = thisLine.split(":")[1].split("\\.");
            bo2Blocks.add(new BiomeWorldObjectBlock(Integer.parseInt(location[0]),
                    Integer.parseInt(location[2]), Integer.parseInt(location[1]), Short.parseShort(block[0]),
                    Byte.parseByte(block[1])));
        }
    }

    ArrayList<Integer> maxXMap = new ArrayList<>();
    ArrayList<Integer> maxYMap = new ArrayList<>();
    ArrayList<Integer> maxZMap = new ArrayList<>();
    for (BiomeWorldObjectBlock bo2 : bo2Blocks) {
        maxXMap.add(bo2.getX());
        maxYMap.add(bo2.getY());
        maxZMap.add(bo2.getZ());
    }

    int maxX = Collections.max(maxXMap);
    int maxY = Collections.max(maxYMap);
    int maxZ = Collections.max(maxZMap);
    int minX = Collections.min(maxXMap);
    int minY = Collections.min(maxYMap);
    int minZ = Collections.min(maxZMap);
    int differenceX = maxX - minX + 1;
    int differenceY = maxY - minY + 1;
    int differenceZ = maxZ - minZ + 1;

    HashMap<Integer, Set<BiomeWorldObjectBlock>> blocks = new HashMap<>();
    for (int i = 0; i < differenceY + 1; i++) {
        blocks.put(i, new HashSet<>());
    }

    for (BiomeWorldObjectBlock bo2 : bo2Blocks) {
        Set<BiomeWorldObjectBlock> a = blocks.get(bo2.getY() - minY);
        a.add(bo2);
        blocks.replace(bo2.getY(), a);
    }

    //System.out.println(differenceX + " " + differenceZ);
    SliceStack schematic = new SliceStack(differenceY, differenceX, differenceZ);

    for (Map.Entry<Integer, Set<BiomeWorldObjectBlock>> next : blocks.entrySet()) {
        Slice slice = new Slice(differenceX, differenceZ);
        for (BiomeWorldObjectBlock block : next.getValue()) {
            //System.out.println("Added block at " + String.valueOf(block.getX() - minX) + "," + String.valueOf(block.getZ() - minZ));
            slice.setBlock(block.getBlock(), block.getX() - minX, block.getZ() - minZ);
        }
        schematic.addSlice(slice);
    }
    //System.out.println(schematic.toString());

    return new BiomeWorldV2Object(schematic, FilenameUtils.getBaseName(file.getAbsolutePath()));
}