How to get the title of the value from yaml to java - java

fruits:
apple:
color: red
banana:
color: yellow
graps:
color: purple
is it posible to get a String[] with value of {"apple", "banana", "grapes"} in java
i have tried many way but i still cant figure it out

Map<String, Object> obj = yaml.load(inputStream);
obj.values();
see also: https://www.baeldung.com/java-snake-yaml

Related

String to color

I have VARCHAR in the database for colors. The format is rrr,ggg,bbb (example 225,225,0).
How can I convert that String into java.awt.Color and visualize it in a JSF page?
Do this :
1. Read you color column value
2. Split your string values with , or any other separator you have used
3. Parse each value to Integer value
4. Passed this value into java.awt.color constructors.
Sample Example :
int red,green,blue;
String colorStr = "225,225,0"; //Value from DB
String[] ar_color = colorStr.split(",");
red = Integer.parseInt(ar_color[0]);
green = Integer.parseInt(ar_color[1]);
blue = Integer.parseInt(ar_color[2]);
Color myColor = new Color(red, green, blue);
I would parse the database entry and cast the values to integers, then you can use one of the constructors for a java.awt.color.
However I am not sure that is what you want. You want to use this color value in one of your JSF pages.
You might be setting a color on a component like this :
this.myComponent.setStyle("color:'225,225,0'");
So you could dynamically then change the color like this from the DB :
this.myComponent.setStyle("color:" + myColorStringFromDB);
No need to convert to a java color.
Create a Color object, using the values you have
Color color = new Color(int rrr, int ggg, int bbb)
Example
Color color = new Color(255, 255, 0)

How to read vector in java

I have used "combinatoricslib" to Generate combination from a object array. But the result is displayed as a vector. I want to know how to read only one value.
Here is the code.
// Create the initial vector
ICombinatoricsVector<String> initialVector = Factory.createVector(
new String[] { "red", "black", "white", "green", "blue" } );
// Create a simple combination generator to generate 3-combinations of the initial vector
Generator<String> gen = Factory.createSimpleCombinationGenerator(initialVector, 3);
// Print all possible combinations
for (ICombinatoricsVector<String> combination : gen) {
System.out.println(combination);
}
This is the result.
CombinatoricsVector=([red, black, white], size=3)
CombinatoricsVector=([red, black, green], size=3)
CombinatoricsVector=([red, black, blue], size=3)
CombinatoricsVector=([red, white, green], size=3)
CombinatoricsVector=([red, white, blue], size=3)
CombinatoricsVector=([red, green, blue], size=3)
CombinatoricsVector=([black, white, green], size=3)
CombinatoricsVector=([black, white, blue], size=3)
CombinatoricsVector=([black, green, blue], size=3)
CombinatoricsVector=([white, green, blue], size=3)
But it has both combination array and size. But i want to get only the array. how to get it.
Please help me. I am new to java.
Thanks in advance.
You simply need to read the javadoc. It took me 5 seconds to google it and find it: http://combinatoricslib.googlecode.com/svn/tags/release21/doc/org/paukov/combinatorics/ICombinatoricsVector.html
java.util.List<T> getVector()
Returns vector as a list of elements
I understand that what you're using here is an instance of combinatorics.CombinatoricsVector
It has a getVector method, which returns a List of all the elements in a vector like this (in this case, all the colours) and a getValue(int index) method, which allows you to retrieve an object at a specific index.
You can try this:
Generator<String> gen = Factory.createSimpleCombinationGenerator(initialVector, 3);
// Print all possible combinations
for (ICombinatoricsVector<String> combination : gen) {
System.out.println(combination.getValue(0)); // This gets the first value from the vector
System.out.println(combination.iterator().next()); // This is another way to do it
}
Check the Javadoc for details.
Perhaps this will work well for you:
// Print all possible combinations
for (ICombinatoricsVector<String> combination : gen) {
System.out.println(Arrays.toString(combination.toArray()));
}

String array in java random

String [] rnum = {"Black", "Red", "Black", "Red", "Black", "Red", "Black","Red",
"Black", "Red", "Black", "Red", "Black", "Red", "Black", "Red","Black", "Red", "Green"};
int A = rnum.length;
//the "Math.random() method will get you a random color
int random = (int) (Math.random() * A);
//randomize the strings
String Color = rnum[random];
How do i say "if color = black then do this" or same for green or same for red"
You mean...
if(Color.equals("Black")) {
// then do this
} else if(Color.equals("Red"){
// then do this
}
or even (In Java >= 1.7)
switch(Color) {
case "Black":
// then do this
break;
case "Red":
// then do this
break;
}
Color should not be capitalized, since that can be a Java class name.
For this purpose you can use a ternary operator (shortened if-else):
String color = ( rnum[random].compareTo("Black") == 0 ? ** do something here ** : ** do something if the color is not "Black" ** );
or:
String color = "";
if(rnum[random].compareTo("Black") == 0) {
// do stuff
}
else {
// it's not black. do other stuff.
}
With red and green, just replace "Black" with "Red", or "Green".
Using java equals method for each color is the simplest way.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals%28java.lang.Object%29
Others said the method to decide equality, I would add up something about the algorithm.
Consider adding weight of colours, I see Red and Black appear 9 times while Green appears once. I would add an object containing the name of the colour and the weight you want to apply. Like {"Green", 1}, {"Red", 9}, {"Black", 9}.
Sum up the weights and order the colours in some way and decide where the random number fell.
It would make more clean code and nicer solution.

Implement graph using jQplot

My need is to implement following graph
I know about stacked chart, but here there are axis points labels (In my need there is no any axes point labels), and this is from Y-axis.
I am confused how can I implement it.
Can you please help me? Any link will also be helpful.
Update :
I am trying a code snippet
<script type="text/javascript" language="javascript">
$(document).ready(function(){
var line1 = [14];
var line2 = [77];
var plot4 = $.jqplot('test2', [line1, line2], {
title: '1 Mobility Test Graph',
animate: !$.jqplot.use_excanvas,
stackSeries: true,
seriesDefaults: {
renderer: $.jqplot.BarRenderer,
rendererOptions:{barPadding : 0, barMargin : 5, barDirection: 'horizontal'},
pointLabels:{location : 'e', edgeTolerance : 0, hideZeros: true, show : true},
shadowAngle : 135,
lineWidth : 0,
showLine: true
},
axesDefaults : {
show : false,
tickOptions : {
show : false
}
},
axes: {
yaxis:{renderer:$.jqplot.CategoryAxisRenderer}
},
grid:{
borderWidth:0,
shadow:false
},
legend: {
renderer: $.jqplot.EnhancedLegendRenderer,
show:true,
rendererOptions:{
numberRows:1,
numberColumns: 3,
disableIEFading: false
},
location: 'n',
placement : 'outsideGrid',
marginTop: '5px',
showSwatch:false
}
});
$(".jqplot-xaxis-tick").hide();
$(".jqplot-yaxis-tick").hide();
});
</script>
But this doesn't give me any output. When I remove barDirection: 'horizontal' its working fine. Please help me, what is wrong...
Here I got jsfiddle , but it works for two graphs..... how can I make for one.
Are you after something like this? You have a single stacked horizontal bar there.
As per examples in doc with horizontal bar chart you must give the chart an array of series where each one has values as two values arrays ([x,y]) where x is value and y is series id.
Or another option would be to give the chart a single array of single value arrays, as shown here.

One to Many search using AND condition

I have the following product which contain many colors.
I wish to find the product which contain at least RED and GREEN.
Product class
String id;
List<Color> colors{};
Color class
id
color
kindly ignore the syntax error.
I'm able to use the following to search OR condition.
Criteria criteria = createCriteria();
criteria.createAlias("colors","colors");
List<String> colorsList = new LinkedList();
colorsList.add("GREEN");
colorsList.add("RED");
criteria.add(Restriction.in("colors.color",colorsList);
The above will give me products which has red or green in their colors BUT not products which contain at least RED AND GREEN.
Example
Product: RED GREEN - PASS
Product: RED GREEN YELLOW - PASS
Product: RED YELLOW - FAIL
Thanks in advance.
the idea is we select all products with the colors and count each product, then products with both colors should have a count of 2 as the number of colors
DetachedCriteria colorCrit = DetachedCriteria.For(Product.class)
.createAlias("colors","color")
.add(Restriction.eq("color.color", "RED")
.add(Restriction.eq("color.color", "GREEN")
.SetProjection(Projections.Group("id"))
.add(Restriction.eq(Projections.rowCount(), 2));
Criteria criteria = createCriteria()
.add(Subqueries.in("id", colorCrit)
.list();
Update:
there is an issue for hibernate for exactly this. the last comment describes how to use.

Categories