Java List Union unionCreditList(List list)

Here you can find the source of unionCreditList(List list)

Description

Creates the union of a list of credit ranges, returning the min and max credits found.

License

Open Source License

Declaration


public static String unionCreditList(List<String> list) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**//from w w  w. j  a  v  a  2s .com
     * Creates the union of a list of credit ranges, returning the min and max credits found.
     * eg "1-2", "3-5" would return "1-5"
     */

    public static String unionCreditList(List<String> list) {
        if (list == null || list.isEmpty())
            return "";
        float minCredits = Integer.MAX_VALUE;
        float maxCredits = Integer.MIN_VALUE;
        for (String item : list) {
            String[] split = item.split("[ ,/-]");

            String first = split[0];
            float min = Float.parseFloat(first);
            minCredits = Math.min(min, minCredits);

            String last = split[split.length - 1];
            float max = Float.parseFloat(last);
            maxCredits = Math.max(max, maxCredits);
        }

        String credits = Float.toString(minCredits);

        if (minCredits != maxCredits) {
            credits = credits + "-" + Float.toString(maxCredits);
        }

        credits = credits.replace(".0", "");
        return credits;
    }
}

Related

  1. union(List byteList)
  2. union(List list1, List list2, boolean duplicate)
  3. union(List set1, List set2)
  4. union(List... lists)
  5. unionAdd(List vect, E obj)
  6. unionList( Collection col1, Collection col2)
  7. unionList(List la, List lb)
  8. unionList(List list1, List list2)
  9. unionOfListOfLists( final Collection> collectionOfCollection)