Compare dates and add them to a TreeSet [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 8 years ago.
Improve this question
I want to implement a Comparator for two Strings representing 2 dates that compares the dates and adds them to a tree TreeSet accordingly.Thanks.

This is one posibility to compare the date after formating the string :
import java.util.Date
public class ComparatorExample {
private static class DateComparator implements Comparator<Date> {
#Override
public int compare(String s1, String s2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse(s1);
Date date2 = sdf.parse(s2);
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(date2)>0){
return 1;
}else if(date1.compareTo(date2)<0){
return -1;
}else if(date1.compareTo(date2)==0){
return 0;
}
}
}
}

Related

How to swap two elements (generics) [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 7 days ago.
Improve this question
[![enter image description here][1]][1]
Hello, everyone. I am a little bit confused in swapping. Plaese correct my swap method. I have tried to solve it in my ownself
I tried to not use temp
Swap<Integer, String> integerStringSwap = new Swap<>();
----------------------------------------------------
public class Swap<A,B> {
private A first;
private B second;
public Swap(A first, B second) {
this.second=second;
this.first=first;
}
void setFirst(A first){
this.first=first;
}
void setSecond(B second){
this.second=second;
}
A getFirst(){
return first;
}
B getSecond(){
return second;
}
void swap(){
}
}````
[1]: https://i.stack.imgur.com/yN3tu.png

How to get field from Object class? [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
I have a object like below:
public void getField(List<?> list) {
int lastIndex = list.size()-1;
Object ob = list.get(lastIndex);
}
How can I return a specific field from this ob like e.q. status?
Possible is the solution for your case:
public <T> T getField(List<T> list) {
int lastIndex = list.size()-1;
T ob = list.get(lastIndex);
return ob;
}

Easiest way to find the element is present in ENUM or not 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 3 years ago.
Improve this question
I need to find the specific string element is present in ENUM or not at runtime using java and based on that take action. What is the efficient way to do so.
A very minimal example given your requirements. Here is some documentation for Enums that you should look at as well. Enum info
Given the enum below:
enum TestEnum {
FOO,
BAR
}
You could simply call valueOf to determine if the String is a valid for that enum.
public boolean exampleTest(final String value) {
try {
TestEnum.valueOf(value);
return true;
} catch (final IllegalArgumentException | NullPointerException) {
// Log if desired
return false;
}
}
Note: Enum#valueOf is case sensitive so the String argument must match exactly.
exampleTest(“FOO”); // true
exampleTest(null); // false
exampleTest(“BAR”); // true
exampleTest(“bar”); // false
You can do like below -
public enum Direction {
NORTH("North"),
EAST("East"),
SOUTH("South"),
WEST("West")
private String dir;
Direction(String dir) {
this.dir = dir;
}
public String getDir() {
return dir;
}
}
to check with specific string you can do like below -
for(Direction dir : Direction.values())
{
//check your condition here
System.out.println(dir.name() + " :: "+ env.getDir());
}

Java 8 Stream | Add a value of the stream to another List/Collection without using foreach or any terminal Operation [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 3 years ago.
Improve this question
So far i tried to add values from a stream to a list with peek() but later I found out that peek() is only used "to support debugging, where you want to see the elements as they flow past a certain point in a pipeline".
Now my question is whats the coding-convention here ?
Do I map it in a second stream or can I map it one String like my Code with Peek() ?
final int range = 9;
List <String> help = new ArrayList<String>();
//random numbers to fill help
for(int i = 5;i< range;i++)
{
help.add(String.valueOf(i+(i*2)+(i*(i+2))) );
}
List<Test> others = new LinkedList<>();
List<Test> tests = help.stream().map(s-> new Test(s,(int) Integer.valueOf("10")))
.peek(t->System.out.println(t.getText()))
.peek(t-> others.add(t)).collect(Collectors.toList());
The class Test looks like this:
public class Test
{
String text;
int id;
public Test(String text, int id) {
this.text = text;
this.id = id;
}
public String getText() {
return text;
}
public int getId() {
return id;
}
}
You could add that functionality to the lambda in the mapping part:
List<Test> tests = help.stream().map(
s-> {
Test t = new Test(s,(int) Integer.valueOf("10")));
System.out.println(t.getText());
others.add(t)
return t;
}
.collect(Collectors.toList());
That simply moves your extra steps into an existing step, avoiding any further loops etc

Using the Generic Type [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 4 years ago.
Improve this question
My Instructor for my Data structures class has told me that there is a better way to implement using a Generic Data type in this method instead of casting everything to E. I am unable to figure out how this better way is implemented or exactly what she means. I know this method I wrote works but if there is a better way I would like to know.
public class GenericSortedArrayBag<E extends Comparable> implements Cloneable,Iterable<E> {
public int numPresents;
public int maxPresents;
private Object[] data;
public void delete(E k) {
boolean found = false;
for(int i=0; i <numPresents; i++) {
if(((E)data[i]).equals(k)) {
found = true;
}
if(found && i<numPresents - 1) {
data[i] = data[i+1];
}
else if(found) {
data[i] = null;
}
}
numPresents--;
}
Instead of
private Object[] data;
you can use
private E[] data;
That way you save the cast
if((data[i]).equals(k))

Categories

Resources