All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.collection.CollectionUtil Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

//------------------------------------------------------------------------------
/**
 Collection utility functions.
 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class CollectionUtil
{
	//##########################################################################
	// PUBLIC METHODS
	//##########################################################################

   //--------------------------------------------------------------------------
   /**
    Returns whether or not the specified Collection contains values.
    @param inCollection the Collection to evaluate
    @return whether or not the specified collection contains values
    */
   public static boolean hasValues(Collection inCollection)
   {
      return (inCollection != null && inCollection.size() > 0);
   }

   //--------------------------------------------------------------------------
   /**
    Returns whether or not the specified Map contains values.
    @param inMap the Map to evaluate
    @return whether or not the specified Map contains values
    */
   public static boolean hasValues(Map inMap)
   {
      return (inMap != null && inMap.size() > 0);
   }

   //--------------------------------------------------------------------------
   /**
    Returns whether or not the specified Collection contains a single value.
    @param inCollection the Collection to evaluate
    @return whether or not the specified collection has a single value
    */
   public static boolean hasSingleValue(Collection inCollection)
   {
      return (inCollection != null && 1 == inCollection.size());
   }

   //--------------------------------------------------------------------------
   /**
    Returns the sum of the specified Collection.
    @param inCollection the Collection of numbers to sum
    @return the sum of the collection
    */
   public static float sum(Collection inCollection)
   {
      float sum = 0;
      if (hasValues(inCollection))
      {
         for (Number num : inCollection)
         {
            if (num != null)
            {
               sum += num.floatValue();
            }
         }
      }

      return sum;
   }

   //--------------------------------------------------------------------------
   public static  List> chunk(Collection inCollection, int inChunkSize)
   {
      List> chunks = new ArrayList<>();

      List currentSublist = null;
      int i = 0;
      for (T obj : inCollection)
      {
         i++;
         if (null == currentSublist
              || i > inChunkSize)
         {
            currentSublist = new ArrayList<>(inChunkSize);
            chunks.add(currentSublist);
            i = 1;
         }

         currentSublist.add(obj);
      }

      return chunks;
   }

   //--------------------------------------------------------------------------
   public static  List> chunkList(List inList, int inChunkSize)
   {
      List> chunks = new ArrayList<>();

      int startIdx = 0;
      int endIdx   = inChunkSize;

      while (startIdx < inList.size())
      {
         if (endIdx > inList.size())
         {
            endIdx = inList.size();
         }

         chunks.add(inList.subList(startIdx, endIdx));

         startIdx = endIdx;
         endIdx   = startIdx + inChunkSize;
      }

      return chunks;
   }

   //--------------------------------------------------------------------------
   public static > Map sortMapByValue(Map inMap)
   {
      Map result = new LinkedHashMap<>();

      Stream> entrySetStream = inMap.entrySet().stream();

      entrySetStream.sorted(Comparator.comparing(e -> e.getValue()))
            .forEach(e ->result.put(e.getKey(),e.getValue()));

      return result;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy