//How to split a data set in N parts? Assume N (in the code we call it ratio)=size of data set (S, in the code called size of data) diveded with max number of items in each subset (M, in the code called max) how to split the data set?
def max = 40
def data = [40,20,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,12,28,88,10,40,20,30,40,50,60,70,80,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,55,64,12,28,88,10,40,40,20,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,12,28,88,10,40,20,30,40,50,60,70,80,55,64,12,28,88,10,40,40,20,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,12,28,88,10,40,20,30,40,50,60,70,80,55,64,12,28,88,10,40,40,20,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,12,28,88,30,40,50,60,70,80,55,64,12,28,88,10,40,20,20,21,99,22,43,32,31,55,64,12,28,88,10,20,30,40,50,60,70,80,20,21,99,22,43,32,31,55,64,12,28,88,11,20,30,40,50,60,70,80,20,21,99,22,43,32,31,55,64,12,28,88,20,30,40,50,60,70,80,78] as ArrayList
Double ratio =data.size()/max
Double parts = Math.ceil(ratio)
int upperBound = max;
int lowerBound = 0;
for (Integer j = new Integer(1); j<=parts;j++)
{
if (lowerBound < data.size() && upperBound <=data.size())
{
println "printing elements: "+lowerBound+"-"+upperBound
def k = data.subList(lowerBound,upperBound)
println "Size of elements: "+ k.size()
lowerBound = upperBound;
upperBound = (int) (max * (j+1)<=data.size() ? max*(j+1):data.size());
}
}
måndag 9 februari 2015
Split Data in N parts
Universal Date Util in Java
package se.nap;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
public class UniversalDateUtil
{
private static final Map REGEX = null;
public static final String DEFAULT_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZZ";
private static final UniversalDateUtil instance = new UniversalDateUtil();
private UniversalDateUtil()
{
// Test Regex here; http://www.regexplanet.com/advanced/java/index.html before inserting new format
REGEX = new HashMap();
REGEX.put("^\\d{8}$", "yyyyMMdd");
REGEX.put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd");
REGEX.put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy");
REGEX.put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{3}\\s\\d{4}$", "dd MMM yyyy");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{4,}\\s\\d{4}$", "dd MMMM yyyy");
REGEX.put("^\\d{12}$", "yyyyMMddHHmm");
REGEX.put("^\\d{8}\\s\\d{4}$", "yyyyMMdd HHmm");
REGEX.put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}$", "dd-MM-yyyy HH:mm");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy-MM-dd HH:mm");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}Z$", "yyyy-MM-dd'T'HH:mm:ss'Z'");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}'T'\\d{1,2}:\\d{2}:\\d{2}Z$", "yyyy-MM-dd'T'HH:mm:ss'Z'");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}z$", "yyyy-MM-dd'T'HH:mm:ss'z'");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}z$", "yyyy-MM-dd'T'HH:mm:ss'z'");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}[A-Z]{0,3}$", "yyyy-MM-dd'T'HH:mm:ssz");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}[-|+]\\d{4}$","yyyy-MM-dd'T'HH:mm:ssZZZ");
REGEX.put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}$", "MM/dd/yyyy HH:mm");
REGEX.put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy/MM/dd HH:mm");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMM yyyy HH:mm");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMMM yyyy HH:mm");
REGEX.put("^\\d{14}$", "yyyyMMddHHmmss");
REGEX.put("^\\d{8}\\s\\d{6}$", "yyyyMMdd HHmmss");
REGEX.put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd-MM-yyyy HH:mm:ss");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm:ss");
REGEX.put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "MM/dd/yyyy HH:mm:ss");
REGEX.put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy/MM/dd HH:mm:ss");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMM yyyy HH:mm:ss");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMMM yyyy HH:mm:ss");
}
public static String determineDateFormat(String dateString)
{
for (String regexp : REGEX.keySet())
{
if (dateString.matches(regexp))
{
return REGEX.get(regexp);
}
}
return null; // Unknown format.
}
public static Collection getFormats()
{
return REGEX.values();
}
public static Date parseString(String dateTime) throws ParseException
{
return parseString(dateTime, null);
}
public static Date parseString(String dateTime, Locale locale) throws ParseException
{
String format = determineDateFormat(dateTime);
if (format != null)
{
return getFormatter(format,locale).parse(dateTime);
}
return null;
}
public static String toString(final Date date)
{
return toString(date, DEFAULT_FORMAT, null, null);
}
public static String toString(final Date date, Locale locale)
{
return toString(date, DEFAULT_FORMAT, locale, null);
}
public static String toString(final Date date, final String format, Locale locale, final String timezone)
{
DateFormat formatter = getFormatter(format, locale);
if(timezone!=null)
{
final TimeZone tz = TimeZone.getTimeZone(timezone);
formatter.setTimeZone(tz);
}
return formatter.format(date);
}
private static DateFormat getFormatter(String format, Locale locale)
{
if(locale==null)
{
locale = Locale.getDefault();
}
return instance.new ConcurrentDateFormatAccess(format, locale).dateformat.get();
}
class ConcurrentDateFormatAccess
{
private ThreadLocal dateformat;
public ConcurrentDateFormatAccess(final String pattern, final Locale locale)
{
dateformat = new ThreadLocal()
{
@Override
public DateFormat get()
{
return super.get();
}
@Override
protected DateFormat initialValue()
{
return new SimpleDateFormat(pattern, locale);
}
@Override
public void remove()
{
super.remove();
}
@Override
public void set(DateFormat value)
{
super.set(value);
}
};
}
}
}
Prenumerera på:
Inlägg (Atom)