Condition for enum values in java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I wanted to know how to have an enum in java, containing values that could adjust depending on the condition.
Exemple :
Public enum values{
If (condition){
A("a", 1),
B("b",2);
} else {
C("c", 1);
}
Private string value;
Private int id;
values(string value, int id) {
this.value = value;
this.id = id;
}
}
Thank you for your help

That's not the purpose of Enum. From java docs
An enum type is a special data type that enables for a variable to be
a set of predefined constants. The variable must be equal to one of
the values that have been predefined for it.
You can add the logic in method,
if (condition)
value = values.A;
else
value = values.C;
You can also use filter on enum,
Arrays.stream(values.values())
.filter(condition)
.collect(toList());

Related

How to return a Map with an Integer key and Set<...>value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have got this class
public class Motorcycle{
private final Integer year;
private Integer km;
public Motorcycle(Integer year, Integer km) {
super();
this.year= year;
this.km = km;
}
public Integer getKm() {
return km;
}
public void setKm(Integer km) {
this.km = km;
}
public Integer getYear() {
return year;
}
}
And this class
public class Scooter extends Motorcycle{
public Scooter(Integer year, Integer km) {
super(year, km);
}
}
In another class I have got
private Set<Motorcycle>moto;
public Map<Integer, Set<Motorcycle>> getScooterByYear() {}
getScooterByYear is a method which returns a map with an entry whose key is an Integer (the year of registration of the
motorcycles) and by value the set of Scooter-type motorcycles contained in motorcycles whose
year is equal to the key of the corresponding entry.
I have to implement it without parameters.
I suggest that your method gets the base collection as input (that way it doesn't matter where it actually comes from). In that case, your method may also be static.
public static Map<Integer, Set<Motorcycle>> getScooterByYear(Collection<Motorcycle> motorcycles) {
return motorcycles.stream()
.filter(m -> m instanceof Scooter)
.collect(groupingBy(Motorcycle::getYear, toSet()));
}
It should be self-explanatory. First you filter for what you want and then you group the stuff by year and the second argument of groupingBy gives you a Set (where List would be the default).
Disclaimer: if this was some sort of homework, be advised that your teacher may not accept this answer, because it might be too "modern" for school stuff.

How can i create Enum in Java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I already created Enum. But i cannot create new Enum. I wanna create and compare 2 Enums actually. The X value is coming from backend. It's a dynamic value. I wanna make this;
EmployeeType type = new EmployeeType("X")
if (type == EmployeeType.X_PERSONALS) {
Log.i("SAMPLE_TAG", "Yesss!")
}
public enum EmployeeType {
X_PERSONALS("X"),
Y_PERSONALS("Y"),
Z_PERSONALS("Z");
private final String text;
EmployeeType(final String text) {
this.text = text;
}
public String getText() {
return text;
}
}
But i cannot create new Enum
This is literally the point of enums: you can't instantiate them.
You can refer to the existing elements e.g.
EmployeeType type = EmployeeType.X_PERSONALS;
So what you are actually trying to achieve is to find the enum constant that has the same text. You can to that like this:
public enum EmployeeType {
X_PERSONALS("X"),
///...and so on
public static EmployeeType getByText(String x) {
for(EmployeeType t:values()) {
if(t.text.equals(x)){
return t;
}
}
return null;
}
}
and then use the code like this:
EmployeeType type = EmployeeType.getByText("X");
if (type == EmployeeType.X_PERSONALS) {
//and so on

What is the easiest way to fetch the entity value in java giving other attributes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What would be the easiest way to fetch the value given from, to as arguments. Should I iterate and find or is there a more elegant way?
FOO.java (from, to, value)
public static FOO ONE = new FOO(6000000, 7000000, "60 - 70");
public static FOO TWO = new FOO(7000000, 8000000, "70 - 80");
public static FOO THREE = new FOO(8000000, 9000000, "80 - 90");
private static List<FOO> VALID_FOOS = new ArrayList<FOO>();
VALID_FOOS.add(ONE);
VALID_FOOS.add(TWO);
VALID_FOOS.add(THREE);
Nowadays one would use streams, I'd say (though this would be more suitable if from and to define a range and you search by a single value within the range):
List<String> wanted = VALID_FOOS.stream()
.filter(foo -> foo.from == from & foo.to == to)
.map(foo -> foo.value)
.collect(Collectors.toList())
If you want to use the map-approach you'd need to have a structure somewhat on that line (you can find more on Tuples here):
public class FOO {
public final Tuple<Integer, Integer> range;
public final String value;
}
Map<FOO, String> VALID_FOOS ...
and retrieve your wanted foo by
VALID_FOOS.get(new Tuple(from, to));

Java - refering to an object [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new in Java and I'm trying to create a social network. I have a array of objects "People" which their names, e-mails and so on. I would like to create relations between them (like 'friends') so that program would connect two object.
I though about doing another array inside of every "Person" object, so each person would have a name, e-mail and friend array with all friends inside of it. I don't know how to refer to an other object of type People inside of object of this type.
I don't know if it's clear..
Hope it is!
Thank You in advance!
Here's one way:
public class Person {
private String name;
private String email;
private List<Person> friends;
// Leave the rest for you.
public void addFriend(Person p) {
if (p != null) {
this.friends.add(p);
}
}
public void removeFriend(Person p) {
this.friends.remove(p);
}
public boolean isFriend(Person p) {
return this.friends.contains(p);
}
}
You'll want constructors and a way to add and remove Person from your friend List.

which class should I use to collect and store different data types? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My Sqlite Database has several columns with different data types. Now, I want to retrieve a row which has different data types and store it in one object.
Which data type or class should I use?
You can make a list of objects:
int numberOfObjects = 10;
ArrayList<Obj> lst = new ArrayList<Obj>(numberOfObjects);
That depends on how you are going to use it later.. The
resultset
can hold all the datatypes.Later you can retreive every coloumn value from it as a string.
Cursor and a Custom Object is just fine.
class YourDataClass {
int id;
String data;
byte[] rawdata;
public String getData() { return data; }
public String setData(String data) { this.data = data; }
// .... other getters and setters
}
Cursor yourCursor = database.rawQuery("select * from blub");
if (yourCursor != null & yourCursor.size() > 0 && yourCursor.moveToFirst) {
YourDataClass yourObject = new YourObject;
yourObject.setId(cursor.getInt(cursor.getColumnIndex("id"));
yourObject.setData(cursor.getString(cursor.getColumnIndex("data"));
... whatever.
}

Categories

Resources