I have a seekbar and trying to set the value from 60 to 180 for one and 40 to 190 for the second one in step of 1.
sb1 = (SeekBar) findViewById(R.id.progresss);
sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//int inVal = Integer.parseInt(String.valueOf(seekBar.getProgress()));
//inVal =+ 70;
//Toast.makeText(getApplicationContext(), String.valueOf(inVal),Toast.LENGTH_LONG).show();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
progress =+ 70;
Toast.makeText(getApplicationContext(), String.valueOf(progress),Toast.LENGTH_LONG).show();
}
});
is not working. Any idea how to fix it?
You cannot set the min value of a SeekBar (always 0) and you cannot set the step value of a SeekBar (always 1).
To set the value from 60 to 180 with a step of 1:
int step = 1;
int max = 180;
int min = 60;
// Ex :
// If you want values from 3 to 5 with a step of 0.1 (3, 3.1, 3.2, ..., 5)
// this means that you have 21 possible values in the seekbar.
// So the range of the seek bar will be [0 ; (5-3)/0.1 = 20].
seekbar.setMax( (max - min) / step );
seekbar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser)
{
// Ex :
// And finally when you want to retrieve the value in the range you
// wanted in the first place -> [3-5]
//
// if progress = 13 -> value = 3 + (13 * 0.1) = 4.3
double value = min + (progress * step);
}
}
);
I put another example within the code so that you understand the math.
You can set max value for your seekbar by using this code:
sb1.setMax(100);
This will set the max value for your seekbar.
But you cannot set the minimum value but yes you can do some arithmetic to adjust value.
Use arithmetic to adjust your application-required value.
For example, suppose you have data values from -50 to 100 you want to
display on the SeekBar. Set the SeekBar's maximum to be 150 (100-(-50)),
then subtract 50 from the raw value to get the number you should use when setting
the bar position.
You can get more info via this link.
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int MIN = 5;
if (progress < MIN) {
value.setText(" Time Interval (" + seektime + " sec)");
} else {
seektime = progress;
}
value.setText(" Time Interval (" + seektime + " sec)");
}
});
The easiest way to set a min and max value to a seekbar for me: if you want values min=60 to max=180, this is equal to min=0 max=120. So in your seekbar xml set property:
android:max="120"
min will be always 0.
Now you only need to do what your are doing, add the amount to get your translated value in any change, in this case +60.
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int translatedProgress = progress + 60;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Be careful with the seekbar property android:progress, if you change the range you must recalculate your initial progress. If you want 50%, max/2, in my example 120/2 = 60;
Seek Bar has methods for setting max values but not for setting min value
here i write a code for setting minimum seek bar value
when we add this code then your seek bar values not less then mim value
try this its work fine for me
/* This methods call after seek bar value change */
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
/* Check the current seekbar value is greather than min value*/
if (progress < MIN_VALUE) {
/* if seek bar value is lesser than min value then set min value to seek bar */
seekBar.setProgress(MIN_VALUE);
}
}
If you are using the AndroidX libraries (import androidx.preference.*), this functionality exists without any hacky workarounds!
val seekbar = findPreference("your_seekbar") as SeekBarPreference
seekbar.min = 1
seekbar.max = 10
seekbar.seekBarIncrement = 1
For requirements like this I have created Utility to customize Seekbar progress like below code:
SeekBarUtil.class
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekBarUtil {
public static void setSeekBar(SeekBar mSeekbar, int minVal, int maxVal, int intervalVal, final TextView mTextView, String startPrefix, String endSuffix) {
int totalCount = (maxVal - minVal) / intervalVal;
mSeekbar.setMax(totalCount);
mSeekbar.setOnSeekBarChangeListener(new CustomSeekBarListener(minVal, maxVal, intervalVal) {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//progress = ((int)Math.round(progress/interval))*interval;
int val = min;
if (interval == totalCount) {
val = max;
} else {
val = min + (progress * interval);
}
seekBar.setProgress(progress);
mTextView.setText(startPrefix + val + endSuffix);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) { }
#Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
}
}
and
CustomSeekBarListener.class
import android.widget.SeekBar;
class CustomSeekBarListener implements SeekBar.OnSeekBarChangeListener {
int min=0,max=0,interval=1;
int totalCount;
public CustomSeekBarListener(int min, int max, int interval) {
this.min = min;
this.max = max;
this.interval = interval;
totalCount= (max - min) / interval;
}
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) { }
#Override
public void onStartTrackingTouch(SeekBar seekBar) { }
#Override
public void onStopTrackingTouch(SeekBar seekBar) { }
}
and you can use it like below code snippet
SeekBarUtil.setSeekBar(seekbarAmountNeeded,10000,200000,5000,textAmount,"$"," PA");
Another solution to handle this case is creating a customized Seekbar, to get ride of converting the real value and SeekBar progress every time:
import android.content.Context
import android.util.AttributeSet
import android.widget.SeekBar
//
// Require SeekBar with range [Min, Max] and INCREMENT value,
// However, Android Seekbar starts from 0 and increment is 1 by default, Android supports min attr on API 26,
// To make a increment & range Seekbar, we can do the following conversion:
//
// seekbar.setMax((Max - Min) / Increment)
// seekbar.setProgress((actualValue - Min) / Increment)
// seekbar.getProgress = Min + (progress * Increment)
//
// The RangeSeekBar is responsible for handling all these logic inside the class.
data class Range(val min: Int, val max: Int, private val defaultIncrement: Int) {
val increment = if ((max - min) < defaultIncrement) 1 else defaultIncrement
}
internal fun Range.toSeekbarMaximum(): Int = (max - min) / increment
class RangeSeekBar: SeekBar, SeekBar.OnSeekBarChangeListener {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
var range: Range = Range(0, 100, 1)
set(value) {
field = value
max = value.toSeekbarMaximum()
}
var value: Int = 0
get() = range.min + progress * range.increment
set(value) {
progress = (value - range.min) / range.increment
field = value
}
var onSeekBarChangeListenerDelegate: OnSeekBarChangeListener? = this
override fun setOnSeekBarChangeListener(l: OnSeekBarChangeListener?) {
onSeekBarChangeListenerDelegate = l
super.setOnSeekBarChangeListener(this)
}
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
onSeekBarChangeListenerDelegate?.onProgressChanged(seekBar, value, fromUser)
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
onSeekBarChangeListenerDelegate?.onStartTrackingTouch(seekBar)
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
onSeekBarChangeListenerDelegate?.onStopTrackingTouch(seekBar)
}
}
Then in your fragment,
// init
range_seekbar.range = Range(10, 110, 10)
range_seekbar.value = 20
// observe value changes
range_seekbar.userChanges().skipInitialValue().subscribe {
println("current value=$it")
}
Keywords: Kotlin, range SeekBar, Rx
You can use Material design sliders instead of seekbar
<com.google.android.material.slider.Slider
...
android:valueFrom="60"
android:valueTo="180"
android:stepSize="10.0" />'
for more check here https://material.io/components/sliders/android
Set seekbar max and min value
seekbar have method that setmax(int position) and setProgress(int position)
thanks
Min-value will always start at zero and its nothing you can do about it. But you can change its value when user start scrolling it around.
Here I set the max-value as 64.
This calculations are simple: I want the user to pick a time from 15min to 16 hours, and he picks one of every 15min to 16 hours, clear? I know, very simple :)
SeekBar seekBar = (SeekBar) dialog.findViewById(R.id.seekBar);
seekBar.setMax(64);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
float b;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float des = (float) progress / 4;
b = des;
hours.setText(des + " hours");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
hoursSelected = b;
}
});
private static final int MIN_METERS = 100;
private static final int JUMP_BY = 50;
metersText.setText(meters+"");
metersBar.setProgress((meters-MIN_METERS));
metersBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
progress = progress + MIN_METERS;
progress = progress / JUMP_BY;
progress = progress * JUMP_BY;
metersText.setText((progress)+"");
}
});
}
paySeekRange.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Debug.i(TAG, "onProgressChanged 1: " + progress);
int progressMin = (progress * (maxPayRange - minPayRange) / 100) + minPayRange;
Debug.i(TAG, "onProgressChanged 2: " + progress);
int progressMax = (progress * (maxPayRange) / 100);
progress = (progress * (progressMax - progressMin) / 100) + progressMin;
Debug.i(TAG, "onProgressChanged 3: " + progress);
txtWeeklyPay.setText("$".concat(String.valueOf(progress)));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Copy this class and use custom Seek Bar :
public class MinMaxSeekBar extends SeekBar implements SeekBar.OnSeekBarChangeListener {
private OnMinMaxSeekBarChangeListener onMinMaxSeekBarChangeListener = null;
private int intMaxValue = 100;
private int intPrgress = 0;
private int minPrgress = 0;
public int getIntMaxValue() {
return intMaxValue;
}
public void setIntMaxValue(int intMaxValue) {
this.intMaxValue = intMaxValue;
int middle = getMiddle(intMaxValue, minPrgress);
super.setMax(middle);
}
public int getIntPrgress() {
return intPrgress;
}
public void setIntPrgress(int intPrgress) {
this.intPrgress = intPrgress;
}
public int getMinPrgress() {
return minPrgress;
}
public void setMinPrgress(int minPrgress) {
this.minPrgress = minPrgress;
int middle = getMiddle(intMaxValue, minPrgress);
super.setMax(middle);
}
private int getMiddle(int floatMaxValue, int minPrgress) {
int v = floatMaxValue - minPrgress;
return v;
}
public MinMaxSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnSeekBarChangeListener(this);
}
public MinMaxSeekBar(Context context) {
super(context);
this.setOnSeekBarChangeListener(this);
}
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
intPrgress = minPrgress + i;
onMinMaxSeekBarChangeListener.onMinMaxSeekProgressChanged(seekBar, intPrgress, b);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
onMinMaxSeekBarChangeListener.onStartTrackingTouch(seekBar);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
onMinMaxSeekBarChangeListener.onStopTrackingTouch(seekBar);
}
public static interface OnMinMaxSeekBarChangeListener {
public void onMinMaxSeekProgressChanged(SeekBar seekBar, int i, boolean b);
public void onStartTrackingTouch(SeekBar seekBar);
public void onStopTrackingTouch(SeekBar seekBar);
}
public void setOnIntegerSeekBarChangeListener(OnMinMaxSeekBarChangeListener floatListener) {
this.onMinMaxSeekBarChangeListener = floatListener;
}
}
This class contains method public void setMin(int minPrgress) for setting minimum value of Seek Bar
This class contains method public void setMax(int maxPrgress) for setting maximum value of Seek Bar
There is no option to set a min or max value in seekbar , so you can use a formula here to scale your value.
Desired_value = ( progress * ( Max_value - Min_value) / 100 ) + Min_value
I have tested this formula in many examples. In your example,
if the progressBar is the middle(i.e. progress = 50 ) and your Min_val and Max_val are 60 and 180 respectively, then this formula will give you the Desired_value '120'.
I'm using the filter to check user rights.
Problem in comparing session value to param value is occurred and resolution load is applied using wrapper.
However, the following error message came out.
List<Map<String,Object>> loginInfo = (List<Map<String,Object>>)session.getAttribute("loginSession");
if loginInfo.get(0).get("user_type").equals("1") || loginInfo.get(0).get("user_type").equals("2"))
{
chain.doFilter(req, res);
}
else
{
RereadableRequestWrapper wrapperRequest = new RereadableRequestWrapper(request);
String requestBody= IOUtils.toString(wrapperRequest.getInputStream(), "UTF-8");
Enumeration<String> reqeustNames = request.getParameterNames();
if(requestBody == null) {
}
Map<String,Object> param_map = new ObjectMapper().readValue(requestBody, HashMap.class);
String userId_param = String.valueOf(param_map.get("customer_id"));
System.out.println(userId_param);
if( userId_param == null || userId_param.isEmpty()) {
logger.debug("error, customer_id error");
}
if (!loginInfo.get(0).get("customer_id").equals(userId_param))
{
logger.debug("error, customer_id error");
}
chain.doFilter(wrapperRequest, res);
}
/////////////////////////
here is my wrapper Code.
private boolean parametersParsed = false;
private final Charset encoding;
private final byte[] rawData;
private final Map<String, ArrayList<String>> parameters = new LinkedHashMap<String, ArrayList<String>>();
ByteChunk tmpName = new ByteChunk();
ByteChunk tmpValue = new ByteChunk();
private class ByteChunk {
private byte[] buff;
private int start = 0;
private int end;
public void setByteChunk(byte[] b, int off, int len) {
buff = b;
start = off;
end = start + len;
}
public byte[] getBytes() {
return buff;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
public void recycle() {
buff = null;
start = 0;
end = 0;
}
}
public RereadableRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
String characterEncoding = request.getCharacterEncoding();
if (StringUtils.isBlank(characterEncoding)) {
characterEncoding = StandardCharsets.UTF_8.name();
}
this.encoding = Charset.forName(characterEncoding);
// Convert InputStream data to byte array and store it to this wrapper instance.
try {
InputStream inputStream = request.getInputStream();
this.rawData = IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw e;
}
}
#Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.rawData);
ServletInputStream servletInputStream = new ServletInputStream() {
public int read() throws IOException {
return byteArrayInputStream.read();
}
#Override
public boolean isFinished() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isReady() {
// TODO Auto-generated method stub
return false;
}
#Override
public void setReadListener(ReadListener listener) {
// TODO Auto-generated method stub
}
};
return servletInputStream;
}
#Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream(), this.encoding));
}
#Override
public ServletRequest getRequest() {
return super.getRequest();
}
#Override
public String getParameter(String name) {
if (!parametersParsed) {
parseParameters();
}
ArrayList<String> values = this.parameters.get(name);
if (values == null || values.size() == 0)
return null;
return values.get(0);
}
public HashMap<String, String[]> getParameters() {
if (!parametersParsed) {
parseParameters();
}
HashMap<String, String[]> map = new HashMap<String, String[]>(this.parameters.size() * 2);
for (String name : this.parameters.keySet()) {
ArrayList<String> values = this.parameters.get(name);
map.put(name, values.toArray(new String[values.size()]));
}
return map;
}
#SuppressWarnings("rawtypes")
#Override
public Map getParameterMap() {
return getParameters();
}
#SuppressWarnings("rawtypes")
#Override
public Enumeration getParameterNames() {
return new Enumeration<String>() {
#SuppressWarnings("unchecked")
private String[] arr = (String[])(getParameterMap().keySet().toArray(new String[0]));
private int index = 0;
#Override
public boolean hasMoreElements() {
return index < arr.length;
}
#Override
public String nextElement() {
return arr[index++];
}
};
}
#Override
public String[] getParameterValues(String name) {
if (!parametersParsed) {
parseParameters();
}
ArrayList<String> values = this.parameters.get(name);
String[] arr = values.toArray(new String[values.size()]);
if (arr == null) {
return null;
}
return arr;
}
private void parseParameters() {
parametersParsed = true;
if (!("application/x-www-form-urlencoded".equalsIgnoreCase(super.getContentType()))) {
return;
}
int pos = 0;
int end = this.rawData.length;
while (pos < end) {
int nameStart = pos;
int nameEnd = -1;
int valueStart = -1;
int valueEnd = -1;
boolean parsingName = true;
boolean decodeName = false;
boolean decodeValue = false;
boolean parameterComplete = false;
do {
switch (this.rawData[pos]) {
case '=':
if (parsingName) {
// Name finished. Value starts from next character
nameEnd = pos;
parsingName = false;
valueStart = ++pos;
} else {
// Equals character in value
pos++;
}
break;
case '&':
if (parsingName) {
// Name finished. No value.
nameEnd = pos;
} else {
// Value finished
valueEnd = pos;
}
parameterComplete = true;
pos++;
break;
case '%':
case '+':
// Decoding required
if (parsingName) {
decodeName = true;
} else {
decodeValue = true;
}
pos++;
break;
default:
pos++;
break;
}
} while (!parameterComplete && pos < end);
if (pos == end) {
if (nameEnd == -1) {
nameEnd = pos;
} else if (valueStart > -1 && valueEnd == -1) {
valueEnd = pos;
}
}
if (nameEnd <= nameStart) {
continue;
// ignore invalid chunk
}
tmpName.setByteChunk(this.rawData, nameStart, nameEnd - nameStart);
if (valueStart >= 0) {
tmpValue.setByteChunk(this.rawData, valueStart, valueEnd - valueStart);
} else {
tmpValue.setByteChunk(this.rawData, 0, 0);
}
try {
String name;
String value;
if (decodeName) {
name = new String(URLCodec.decodeUrl(Arrays.copyOfRange(tmpName.getBytes(), tmpName.getStart(), tmpName.getEnd())), this.encoding);
} else {
name = new String(tmpName.getBytes(), tmpName.getStart(), tmpName.getEnd() - tmpName.getStart(), this.encoding);
}
if (valueStart >= 0) {
if (decodeValue) {
value = new String(URLCodec.decodeUrl(Arrays.copyOfRange(tmpValue.getBytes(), tmpValue.getStart(), tmpValue.getEnd())), this.encoding);
} else {
value = new String(tmpValue.getBytes(), tmpValue.getStart(), tmpValue.getEnd() - tmpValue.getStart(), this.encoding);
}
} else {
value = "";
}
if (StringUtils.isNotBlank(name)) {
ArrayList<String> values = this.parameters.get(name);
if (values == null) {
values = new ArrayList<String>(1);
this.parameters.put(name, values);
}
if (StringUtils.isNotBlank(value)) {
values.add(value);
}
}
} catch (DecoderException e) {
// ignore invalid chunk
}
tmpName.recycle();
tmpValue.recycle();
}
}
and Error Message is com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
I Don't know why this problem happened...
I'm trying to implement the logic for "Longest consecutive sequence in Binary tree". The logic I have implemented inside the method { longestConsecutivePath } is not working as expected for the tree structure. It is giving me the lognest path length as 5.
Output:
curLength : 5
BSTNode node = new BSTNode(1);
node.setRight(new BSTNode(2));
node.getRight().setRight(new BSTNode(3));
node.getRight().getRight().setRight(new BSTNode(4));
node.getRight().getRight().getRight().setRight(new BSTNode(5));
node.getRight().setLeft(new BSTNode(7));
node.getRight().getLeft().setLeft(new BSTNode(8));
node.getRight().getLeft().getLeft().setLeft(new BSTNode(9));
node.getRight().getLeft().getLeft().getLeft().setLeft(new BSTNode(10));
node.getRight().getLeft().getLeft().getLeft().getLeft().setLeft(new BSTNode(11));
node.getRight().getLeft().getLeft().getLeft().getLeft().getLeft().setLeft(new BSTNode(12));
Class implementing the Longest Consecutive sequence logic:
public class LongestConsecutivePath {
static BSTNode root = null;
public LongestConsecutivePath() {
root = createBinaryTree();
System.out.println("Before finding the longest consecutive path:");
inorder();
}
public void inorder() {
if (null == root) {
return;
}
inorder(root);
}
private void inorder(BSTNode node) {
if (null != node) {
inorder(node.getLeft());
System.out.print(node.getData() + " ");
inorder(node.getRight());
}
}
public BSTNode createBinaryTree() {
BSTNode node = new BSTNode(1);
node.setRight(new BSTNode(2));
node.getRight().setRight(new BSTNode(3));
node.getRight().getRight().setRight(new BSTNode(4));
node.getRight().getRight().getRight().setRight(new BSTNode(5));
node.getRight().setLeft(new BSTNode(7));
node.getRight().getLeft().setLeft(new BSTNode(8));
node.getRight().getLeft().getLeft().setLeft(new BSTNode(9));
node.getRight().getLeft().getLeft().getLeft().setLeft(new BSTNode(10));
node.getRight().getLeft().getLeft().getLeft().getLeft().setLeft(new BSTNode(11));
node.getRight().getLeft().getLeft().getLeft().getLeft().getLeft().setLeft(new BSTNode(12));
return node;
}
public int longestConsecutivePath() {
if (null == root) {
return 0;
}
return longestConsecutivePath(root, 0, root.getData() + 1);
}
public int longestConsecutivePath(BSTNode node, int curLength,
int targetLength) {
if (null == node) {
return curLength;
}
if (node.getData() == targetLength) {
System.out.println("\nnode data value: "+node.getData());
curLength += 1;
longestPath = curLength;
} else {
curLength = 1;
}
longestLeft = longestConsecutivePath(node.getLeft(), curLength,
node.getData() + 1);
longestRight = longestConsecutivePath(node.getRight(), curLength,
node.getData() + 1);
return Math.max(curLength, Math.max(longestLeft, longestRight));
}
public static void main(String[] args) {
LongestConsecutivePath consecutivePath = new LongestConsecutivePath();
int curLength = consecutivePath.longestConsecutivePath();
System.out.println("\ncurLength : " + curLength);
}
}
BSTNode.java
public class BSTNode {
BSTNode left, right;
int data;
/* Default constructor */
public BSTNode() {
left = null;
right = null;
data = 0;
}
/* Constructor */
public BSTNode(int data) {
left = null;
right = null;
this.data = data;
}
public BSTNode getLeft() {
return left;
}
public void setLeft(BSTNode left) {
this.left = left;
}
public BSTNode getRight() {
return right;
}
public void setRight(BSTNode right) {
this.right = right;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
I need a specialized queue-like data structure. It can be used by multiple consumers, but each item in queue must be removed from queue after k consumers read it.
Is there any production ready implementation? Or Should I implement a queue with read-counter in each item, and handle item removal myself?
Thanks in advance.
I think this is what you are looking for. Derived from the source code for BlockingQueue. Caveat emptor, not tested.
I tried to find a way to wrap Queue, but Queue doesn't expose its concurrency members, so you can't get the right semantics.
public class CountingQueue<E> {
private class Entry {
Entry(int count, E element) {
this.count = count;
this.element = element;
}
int count;
E element;
}
public CountingQueue(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException();
}
this.items = new Object[capacity];
this.lock = new ReentrantLock(false);
this.condition = this.lock.newCondition();
}
private final ReentrantLock lock;
private final Condition condition;
private final Object[] items;
private int takeIndex;
private int putIndex;
private int count;
final int inc(int i) {
return (++i == items.length) ? 0 : i;
}
final int dec(int i) {
return ((i == 0) ? items.length : i) - 1;
}
private static void checkNotNull(Object v) {
if (v == null)
throw new NullPointerException();
}
/**
* Inserts element at current put position, advances, and signals.
* Call only when holding lock.
*/
private void insert(int count, E x) {
items[putIndex] = new Entry(count, x);
putIndex = inc(putIndex);
if (count++ == 0) {
// empty to non-empty
condition.signal();
}
}
private E extract() {
Entry entry = (Entry)items[takeIndex];
if (--entry.count <= 0) {
items[takeIndex] = null;
takeIndex = inc(takeIndex);
if (count-- == items.length) {
// full to not-full
condition.signal();
}
}
return entry.element;
}
private boolean waitNotEmpty(long timeout, TimeUnit unit) throws InterruptedException {
long nanos = unit.toNanos(timeout);
while (count == 0) {
if (nanos <= 0) {
return false;
}
nanos = this.condition.awaitNanos(nanos);
}
return true;
}
private boolean waitNotFull(long timeout, TimeUnit unit) throws InterruptedException {
long nanos = unit.toNanos(timeout);
while (count == items.length) {
if (nanos <= 0)
return false;
nanos = condition.awaitNanos(nanos);
}
return true;
}
public boolean put(int count, E e) {
checkNotNull(e);
final ReentrantLock localLock = this.lock;
localLock.lock();
try {
if (count == items.length)
return false;
else {
insert(count, e);
return true;
}
} finally {
localLock.unlock();
}
}
public boolean put(int count, E e, long timeout, TimeUnit unit)
throws InterruptedException {
checkNotNull(e);
final ReentrantLock localLock = this.lock;
localLock.lockInterruptibly();
try {
if (!waitNotFull(timeout, unit)) {
return false;
}
insert(count, e);
return true;
} finally {
localLock.unlock();
}
}
public E get() {
final ReentrantLock localLock = this.lock;
localLock.lock();
try {
return (count == 0) ? null : extract();
} finally {
localLock.unlock();
}
}
public E get(long timeout, TimeUnit unit) throws InterruptedException {
final ReentrantLock localLock = this.lock;
localLock.lockInterruptibly();
try {
if (waitNotEmpty(timeout, unit)) {
return extract();
} else {
return null;
}
} finally {
localLock.unlock();
}
}
public int size() {
final ReentrantLock localLock = this.lock;
localLock.lock();
try {
return count;
} finally {
localLock.unlock();
}
}
public boolean isEmpty() {
final ReentrantLock localLock = this.lock;
localLock.lock();
try {
return count == 0;
} finally {
localLock.unlock();
}
}
public int remainingCapacity() {
final ReentrantLock lock= this.lock;
lock.lock();
try {
return items.length - count;
} finally {
lock.unlock();
}
}
public boolean isFull() {
final ReentrantLock localLock = this.lock;
localLock.lock();
try {
return items.length - count == 0;
} finally {
localLock.unlock();
}
}
public void clear() {
final ReentrantLock localLock = this.lock;
localLock.lock();
try {
for (int i = takeIndex, k = count; k > 0; i = inc(i), k--)
items[i] = null;
count = 0;
putIndex = 0;
takeIndex = 0;
condition.signalAll();
} finally {
localLock.unlock();
}
}
}
A memory efficient way that retains the info you need:
Each queue entry becomes a
Set<ConsumerID>
so that you ensure the k times are for k distinct consumers: your app logic checks if the
set.size()==k
and removes it from queue in that case.
In terms of storage: you will have tradeoffs of which Set implementation based on
size and type of the ConsumerID
speed of retrieval requirement
E.g if k is very small and your queue retrieval logic has access to a
Map<ID,ConsumerId>
then you could have simply an Int or even a Short or Byte depending on # distinct ConsumerID's and possibly store in an Array . This is slower than accessing a set since it would be traversed linearly - but for small K that may be reasonable.
I have a
ArrayList<Integer>
and i want to pass it to
AsyncTask<ArrayList<Integer>, void, void>.
But in
doInBackground(ArrayList<Integer>...params) function,
i can't receive arrayList, which i passed.
Inside doInBackground i use ArrayList<Integer> arr = params[0] then i log(arr.size()) is 0
My code:
class count extends AsyncTask<Void, Integer, ArrayList<Integer>>{
ArrayList<Integer> arr = new ArrayList<Integer>();
ArrayList<Integer> temp = new ArrayList<Integer>();
#SuppressWarnings("unchecked")
#Override
protected ArrayList<Integer> doInBackground(Void... params) {
// TODO Auto-generated method stub
for(int i = 1; i <= 100; i++){
SystemClock.sleep(200);
arr.add(i);
if(i % 10 == 0){
temp = arr;
//Log.d("DEBUG", "Length of temp = "+ temp.size());
arr.clear();
mean task1 = new mean();
task1.execute(temp);
}
publishProgress(i);
}
return arr;
}
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
tvNum.setText(values[0]+"");
}
}
class mean extends AsyncTask<ArrayList<Integer>, Integer, ArrayList<Integer>>{
#Override
protected ArrayList<Integer> doInBackground(
ArrayList<Integer>... params) {
// TODO Auto-generated method stub
ArrayList<Integer> arrL =new ArrayList<Integer>();
arrL= params[0];
Log.d("DEBUG","iNPUT Size = " + arrL.size());
return null;
}
}
Please help me,
Thanks.
If you pass the Arraylist in as the only parameter when you're calling execute(), it should be in params[0]. For example,
YourTask.execute(YourList);
And you would access it inside of the ASyncTask as so:
Arraylist<Integer> myList = params[0];
Easy Example for your understanding. such as
public class MainActivity extends Activity {
List<CalcPrimesTask> taskList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
taskList = new ArrayList<CalcPrimesTask>();
}
public void onClickStart(View view) {
EditText maximumEditText = (EditText) findViewById(R.id.maximumEditText);
int maxNum = Integer.parseInt(maximumEditText.getText().toString());
CalcPrimesTask task = new CalcPrimesTask(this);
taskList.add(task);
task.execute(maxNum);
Toast.makeText(getApplicationContext(), "New run queued.", Toast.LENGTH_SHORT).show();
}
public void onStopClick(View view) {
for (CalcPrimesTask task : taskList) {
task.cancel(true);
}
Toast.makeText(getApplicationContext(), "All runs cancelled.", Toast.LENGTH_SHORT).show();
}
}
and
public class CalcPrimesTask extends AsyncTask<Integer, String, List<Integer>> {
Activity activity;
public CalcPrimesTask(Activity mainActivity) {
activity = mainActivity;
}
#Override
protected List<Integer> doInBackground(Integer... params) {
int maxNum = params[0];
List<Integer> primeList = new ArrayList<Integer>();
for (int i = 2; i <= maxNum ; i++) {
int maxCalc = (int)Math.sqrt(i);
boolean isPrime = true;
for (int j = 2; j <= maxCalc ; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primeList.add(i);
publishProgress("Prime " + i + " found.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
return primeList;
}
#Override
protected void onProgressUpdate(String... values) {
TextView messageView = (TextView) activity.findViewById(R.id.messageText);
messageView.setText(values[0]);
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(List<Integer> result) {
TextView messageView = (TextView) activity.findViewById(R.id.messageText);
messageView.setText("Total of " + result.size() + " primes found.");
super.onPostExecute(result);
}
}
If your hand available time then read Android AsyncTask. Best of Luck!