Sorting a set using collections sort - java

I am trying to sort and need to retain as a set.
So, I am trying convert my Set to List and use Collections.sort and again covert back to a set but facing this error:
The method sort(List) in the type Collections is not applicable for the arguments (ArrayList)
I tried to sort using TreeSet but it sorts lexically so trying to use Collections.sort.
Set<Object> set = [11, 13, 24, 14, 25, 15, 26, 16, 27, 28, 18, 19, 1, 2, 3, 6];
ArrayList<Object> list = new ArrayList<Object>(set);
Collections.sort(list);
Where I am going wrong?

I think that using TreeSet is going to be your best option, as per the constructor documentation the default ordering is the natural ordering of the elements. With your specific pseudo-code example (using Integers) this will work pretty much perfectly:
var set1 = new TreeSet<>(Arrays.asList(
11, 13, 24, 14, 25, 15, 26, 16,
27, 28, 18, 19, 1, 2, 3, 6
));
System.out.println(set1);
[1, 2, 3, 6, 11, 13, 14, 15, 16, 18, 19, 24, 25, 26, 27, 28]
If, however, you are using Strings, then lexical ordering is used, as that's the natural ordering for that type:
var set2 = new TreeSet<>(Arrays.asList(
"11", "13", "24", "14", "25", "15", "26", "16",
"27", "28", "18", "19", "1", "2", "3", "6"
));
System.out.println(set2);
[1, 11, 13, 14, 15, 16, 18, 19, 2, 24, 25, 26, 27, 28, 3, 6]
You have the option of providing a Comparator to one of the constructors to allow you to customise the ordering to your liking:
var set3 = new TreeSet<>((String a, String b) -> {
return Integer.valueOf(a).compareTo(Integer.valueOf(b));
});
set3.addAll(Arrays.asList(
"11", "13", "24", "14", "25", "15", "26", "16",
"27", "28", "18", "19", "1", "2", "3", "6"
));
System.out.println(set3);
[1, 2, 3, 6, 11, 13, 14, 15, 16, 18, 19, 24, 25, 26, 27, 28]

Related

default value on <form:select /> is completely wrong - JSP

I'm passing an attribute "hours" containing the following values:
ArrayList<Integer> hours = new ArrayList<>(Arrays.asList({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23}));
model.addAttribute("hours", hours);
model.addAttribute("dto", dto);
on my jsp:
...
<form:select id="to" path="to" class="form-control chosen-select"
items="${hours}" value="${dto.to}" />
...
I have tried outputting the values on my jsp and they're all correct, the only problem is that they're completely wrong. dto.to should be an Integer "3" but it shows 13 as default selected value. What's the problem here??

Deserialize(convert) a JsonObject to DateTime without POJO

I have the following jsonObject:
"dateCreated": {
"year": 2021,
"dayOfYear": 145,
"equalNow": false,
"weekyear": 2021,
"chronology": {
"zone": {
"ID": "UTC"
}
},
"weekOfWeekyear": 21,
"secondOfMinute": 10,
"millisOfDay": 10990000,
"monthOfYear": 5,
"dayOfWeek": 2,
"beforeNow": true,
"minuteOfDay": 183,
"dayOfMonth": 25,
"era": 1,
"zone": {
"ID": "UTC"
},
"yearOfCentury": 21,
"centuryOfEra": 20,
"hourOfDay": 3,
"secondOfDay": 10990,
"millis": 1621911790000,
"yearOfEra": 2021,
"minuteOfHour": 3,
"millisOfSecond": 0,
"afterNow": false
}
I need to convert it for this format:
2021-05-25T03:03:10.000Z
Since I don't have a pojo for that object, it's coming from a jsonArray, I'm struggling to get a Datetime from it.
I tried ObjectMapper, JSON(GSON) and still no success.
Does anyone have a clue how can I do it?
edit.: It's a list of Recordings that I converted to JsonArray (to manipulate, adding another field into the json). When I convert the list to jsonArray, that field (type DateTime) becomes serialized and I need it back to the correct format to pass it forward.
Recording.class;
private final DateTime dateCreated;
List<Recording> listRecordings;
JSONArray jsonArray = new JSONArray(listRecordings);
jsonArray ->
[
{
"offset": 179161383840,
"type": "AUDIO",
"duration": 232,
"codec": "OPUS",
"dateCreated": {
"year": 2021,
"dayOfYear": 145,
"equalNow": false,
"weekyear": 2021,
"chronology": {
"zone": {
"ID": "UTC"
}
},
"weekOfWeekyear": 21,
"secondOfMinute": 10,
"millisOfDay": 10990000,
"monthOfYear": 5,
"dayOfWeek": 2,
"beforeNow": true,
"minuteOfDay": 183,
"dayOfMonth": 25,
"era": 1,
"zone": {
"ID": "UTC"
},
"yearOfCentury": 21,
"centuryOfEra": 20,
"hourOfDay": 3,
"secondOfDay": 10990,
"millis": 1621911790000,
"yearOfEra": 2021,
"minuteOfHour": 3,
"millisOfSecond": 0,
"afterNow": false
},
"size": 521517,
"containerFormat": "MKA",
"status": "COMPLETED"
}
]
I need to revert back dateCreated for the original format: 2021-05-25T03:03:10.000Z
Edit3.:
My workaround:
for (Recording recording : listRecordings) {
Object jsonObject = JSONObject.wrap(recording);
((JSONObject) jsonObject).put("dateCreated", recording.getDateCreated());
jsonArray.put(jsonObject);
}
Best Regards,

Integer number too large in Long type variable [duplicate]

This question already has answers here:
Why is 09 "too large" of an integer number? [duplicate]
(5 answers)
Closed 7 years ago.
I tried to declare fixed array objects for testing, to remove the need to reenter objects on every run.
The array test objects are like this:
studentList[0].setStudent("Mr", "Jason", "Lee", 00001, 01, 01,2001, 85, 75, 8, 65, 72.5, "D");
studentList[1].setStudent("Miss", "Candice", "Teo", 00002, 02, 02, 2002, 95, 95, 9, 90, 92.0, "HD");
studentList[2].setStudent("Mrs", "Sophie", "Chua", 00003, 03, 03, 2003, 65, 78, 7, 92, 81.6, "HD");
studentList[3].setStudent("Mrs", "Brendon", "Low", 00004, 04, 04, 2004, 95, 98, 7, 69, 80.1, "HD");
studentList[4].setStudent("Mr", "Clarance", "Yeo", 00005, 05, 05, 2005, 80, 76, 5, 59, 65.7, "C");
studentList[5].setStudent("Mr", "Adrian", "Tan", 00006, 06, 06, 2006, 70, 60, 4, 20, 40.0, "ND");
studentList[6].setStudent("Ms", "Debbie", "Toh", 00007, 07, 07, 2007, 60, 60, 7, 50, 56.0, "P");
studentList[7].setStudent("Miss", "Sarah", "Ho", 00008, 08, 08, 2008, 59, 61, 7, 62, 62.0, "N");
studentList[8].setStudent("Mr", "Aloysius", "Lim", 00009, 09, 09, 2009, 62, 63, 8, 69, 67.5, "C");
studentList[9].setStudent("Mrs", "Peggy", "Lo", 00010, 10, 10, 2010, 65, 67, 9, 72, 71.4, "D");
They are an array object containing
studentTitle (String),
studentFirstName (String),
studentLastName (String),
studentID (Long),
and the rest are scores of (Int) and lastly gradeof (String).
However, an error was shown stating:
integer number too large: 00008
integer number too large: 08
integer number too large: 08
for both on studentList[7] and studentList[8].
May I know what should be corrected? Thank you
08 and 00008 are parsed as octal numbers, in which 8 and 9 are invalid digits. Remove the leading zeroes.

How do you turn an array into a list or how can you set up an array to be used in stickyheaders?

I have a set of data in an array and would like to turn it into a list or added the data to a list. The stickyheaders library uses lists and I need to change mine into a list since I'm having difficulty in using the stickyheaders as arrays it only shows a blank recyclerview.
This is my set of sample data, this is data is just used for creating a new event in a calendar. I retrieve the day and format it to show (e.g Monday). This is what the header will be.
If anyone can help turning this array into a list or how to add these data to a list, it would be appreciated, or if someone is familiar with stickyheaders and used arrays for it some sample code is appreciated.
public static final Event[] EventList = {
new Event("The Theory of Everything", "1:00 PM", "Cinema 1", 2015, 2, 5, 13, 0),
new Event("Kingsman the Secret Service", "3:00 PM", "Cinema 2", 2015, 2, 5, 15, 0),
new Event("Fifty Shades of Grey", "5:00 PM", "Cinema 3", 2015, 2, 5, 17, 0),
new Event("Focus", "7:00 PM", "Cinema 4", 2015, 2, 5, 19, 0),
new Event("Crazy Beautiful You", "9:00 PM", "Cinema 5", 2015, 2, 5, 21, 0),
new Event("The Theory of Everything", "1:00 PM", "Cinema 1", 2015, 2, 6, 13, 0),
new Event("Kingsman the Secret Service", "3:00 PM", "Cinema 2", 2015, 2, 6, 15, 0),
new Event("Fifty Shades of Grey", "5:00 PM", "Cinema 3", 2015, 2, 6, 17, 0),
new Event("Focus", "7:00 PM", "Cinema 4", 2015, 2, 6, 19, 0),
new Event("Crazy Beautiful You", "9:00 PM", "Cinema 5", 2015, 2, 6, 21, 0)
};
You're almost there. After that you can do:
List<Event> eventList = Arrays.asList(EventList);

Blank chart display when load Highchart data using ajax

I'm having problem with highchart.
I'm trying to populate data from struts action by using ajax.
Here is my code
Jquery
$.getJSON("<%=request.getContextPath()%>/c/MonthlyGraphAjax?method=monthlyGraph&selectionList=${selectionList}&reportDateMonth=${reportDateMonth}", function(data){
/*manual insert*/
dchart.xAxis.categories.push(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)
dchart.series.push({name:'John',data:[99,155,148,136,121,0,0,153,173,161,128,128,0,0,45,109,164,150,98,0,0,165,114,151,108,0,0,0,0,0]},{name:'Doe',data:[1,0,0,1,1,0,0,0,0,1,0,1,0,0,0,0,2,0,0,0,0,1,0,0,0,0,0,0,0,0]})
/*end manual insert*/
/*capture to input field*/
$('#categories').val(data.Graph.Categories)
$('#series').val(data.Graph.Series)
/*JSON from struts*/
//dchart.xAxis.categories.push(data.Graph.Categories)
//dchart.series.push(data.Graph.Series)
$('#graphContainer').highcharts(dchart);
})
Response from action class
PrintWriter out = response.getWriter();
out.print("{\"Graph\":");
out.print("{");
out.print("\"Series\":\""+series+"\",\"Categories\":\""+categories+"\"");
out.print("}");
out.print("}");
response.setContentType("application/json");
JSON structure retrieve from action class as below (from firebug console)
{"Graph":
{
"Series":
"{name:'John',data:[99,155,148,136,121,0,0,153,173,161,128,128,0,0,45,109,164,150,98,0,0,165,114,151,108,0,0,0,0,0]},
{name:'Doe',data:[1,0,0,1,1,0,0,0,0,1,0,1,0,0,0,0,2,0,0,0,0,1,0,0,0,0,0,0,0,0]}"
,
"Categories":"1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30"
}
}
The problem is when I manually insert the JSON it's working fine, but json from action class display blank chart. I also capture the json from action class in the input field, the value are exactly the same like I manually hard coded.
Can somebody help me, what am I miss in my code.

Categories

Resources